Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to put and get the below values in/from HashMap as Key and values
Key Value
1 "a"
1 "b"
1 "C"
2 "x"
2 "y"
2 "z"
Well the general idea would be to put the String values in your List<String>. The add method is good for that.
Later you should put the key (1 or 2 according to your example) with the List<String> in your map. The put method is good for that.
It expects a key (your integer number) and a value (your list of strings).
try this..
HashMap<Integer, List<String>> map = new HashMap<>();
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
map.put(1, list);
If you need some direction here is a link with all you need to know about HashMap.
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am looking to take a Hashmap that has many key/value pairs where each value has a list. I want to take an index, and make a list based on that index.
Essentially I am looking to take this code and turn it into a stream.
HashMap < Integer , List< Object >> map = new HashMap();
//name //age
map.put("1", new List("Bob",20));
map.put("2", new List("Jim",37));
map.put("3", new List("Dan",30));
map.put("3", new List("Rick",40));
List < Integer > s = new ArrayList();
map.values().forEach(e - >
{
s.add(( Integer ) e.get(1)); //looking to extract all of the ages into
}); // a new list.
In my use case, each index of the list is a different type of object, so in this case I tried to use a String and an Integer. I mention this in case there is a way to select an item based on an object's type to put into the new list. I had found this example that mentioned "groupingBy" as a Collector's option, but It doesn't seem to work for my use-case
Shortcut for adding to List in a HashMap
Thank you for any help
Make a stream of the map's values collection, use Stream.map to extract the values you care about, and then Stream.collect to make a new collection.
List<Integer> ages = map.values().stream()
.map(list -> (Integer) list.get(1))
.collect(Collectors.toList());
I agree with the commenter on the question who said you should really make these into actual POJO objects - the code will be a lot clearer and less error prone if you do. If you go that route then you can use a method reference to get the ages:
.map(Person::getAge)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This is my code
I have a HashMap which is <String, JLabel>
I want to loop through the HashMap and set the labels that are not in the ArrayList to visible(false). I have tried many things nothing seems to work.
Thanks a lot
HashMap<String,JLabel> map = ...
ArrayList<JLabel> list = ...
for (JLabel label : map.values())
if (!list.contains(label))
label.setVisible(false);
Relevant methods:
Map.values()
Collection.contains(object)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
All I need to know is how to find the length of the string inside of the array. I also would like to know how to find which string comes first in alphabetical order in the array.
myArr[0].length()
This will access the string at location 0, then get the length of it.
Also, for sorting alphabetically, you can use
Arrays.sort(myArr);
You can simply use .length method on any string array element to get its length,
And to sort that array alphabetically you ca use .sort method -
For example -
String[] strings = { "Hello ", "This ", "is", "Sorting", "Example" };
Arrays.sort(strings);
So after sorting strings[0] would be Example and string[0].length() would be 7
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am wanting to convert a HashMap to a Set.I am trying to find common elements between two maps by first putting that to a set and use retainAll.How to convert a Map to a Set.
If you want a set containing the keys use:
Set<KEY_TYPE> set = map.keySet();
If you want a set containing the values use:
Set<VALUE_TYPE> set = new HashSet<VALUE_TYPE>(map.values());
if you want a set containing both elements use:
Set<Map.Entry<KEY_TYPE, VALUE_TYPE>> set = map.entrySet();
You access the elements of an Entry using getKey() and getValue()
HashMap has a key set and a value set, to keep the associativity, HashMap has a method called
entrySet()
you can find more info about it here
As I see from the comments, you want key-value pairs. This you can easily get from the map. Here is an example:
Map<Integer, String> myMap = new HashMap<Integer, String>();
// ... put values into your map
Set<Entry<Integer, String>> entrySet = myMap.entrySet();
Although from your question I'm not sure this is all you want. Maybe you should rephrase your question and post your code what you did so far, so that we can understand where exactly you need help.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string name called label,which is dynamically having values.I want to add this every time generated values into array list.
List<String> container = new ArrayList<String>(Arrays.asList(label));
I tried this way but its printing only the last value, where I'm doing wrong, any suggestion?
In this every time the dynamically changing value of label will be added into the container.
List<String> container = new ArrayList<String>();
String label = "Sample1";
container.add(label);
label = "Sample2";
container.add(label);
label = "Sample3";
container.add(label);
System.out.println("container : "+container);
But you are creating new list every time. using the label variable
List<String> container = new ArrayList<String>(Arrays.asList(label));
I think everytime you are resetting your container variable by the declaration:
List<String> container = new ArrayList<String>(Arrays.asList(label));
Ideally you should do it only at first time, after that simply add the newly created label:
container.add(label);