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.
Related
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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
I have 2 Maps,
Map<String, Object> map1 = new HashMap<String, Object>();
Map<String, Object> map2 = new HashMap<String, Object>();
map1 has {SD="BBBBBB", DNN="Internet", SST=2}
map2 has {SD="BBBBBB", DNN="Internet", SST=2}
map1.equals(map2) => false
I need the output as true. What I need to use for it ?
I need the output as true. What I need to use for it ?
Well, equal maps.
According to the Javadoc for Map.equals(Object):
Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()).
HashMap has a correct equals implementation; so the conclusion you can draw is that the entry sets actually aren't equal.
It's easy for the string representation of a Java object to look the same as another (e.g. "0", Integer.valueOf(0) and Long.valueOf(0) all print as 0); it's also possible for Strings to contain non-printable characters, e.g. "0\0" and "0" will print the same, but are not equal. And, of course, if you're using custom objects, you need to ensure that equals and hashCode are implemented correctly.
Check both keys and values in both sets, to see which one causes them not to be equal.
For example:
Set<Map.Entry<?, ?>> entrySet1 = new HashSet<>(map1.entrySet());
entrySet1.removeAll(map2.entrySet());
if (!entrySet1.isEmpty()) {
System.out.println("Entries only in map1: " + entrySet1);
}
Set<Map.Entry<?, ?>> entrySet2 = new HashSet<>(map2.entrySet());
entrySet2.removeAll(map1.entrySet());
if (!entrySet2.isEmpty()) {
System.out.println("Entries only in map2: " + entrySet2);
}
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 5 years ago.
Improve this question
My Student class has only three attributes - age, sex and name.
I have a Map like Map<String, Student> where key is an UUID string that acts as identifier for the student.
Now i want to convert this Map to another map of pattern - Map<String,List<String>>. In this map, key can be sex of the student and value would be list of UUIDs corresponding to that sex.
I can achieve this using pre-Java8 syntax, but i am trying to do this by Java8 stream API and lambda expressions. Please help with this.
What I have tried -
Map<String, Student> map;
map.entrySet().stream().collect(e -> e.getValue().getSex(), ???how to get list of keys here???)
I am able to set the key of the target map correctly, but i am struggling to set the value.
map.entrySet().stream()
.collect(Collectors.groupingBy(entry -> entry.getValue().getSex(),
Collectors.mapping(entry -> entry.getKey(), Collectors.toList())));
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 6 years ago.
Improve this question
With my code, the new value overwrites the value stored previously against the same key.
This is my code:
HashMap<String, String> meMap = new HashMap<String, String>();
meMap.put(p.getName(), selState);
If the key is same for all then you should map key to list of values: Map<String, List<String>>
And then to update list of values mapped to a specific key:
List<String> values = map.get(key);
values.add("new");
map.put(key, values);
The HashMap is a pair key/value object. It can be only one value for a given key.
If you try to put the same key that already exists, you will be replacing the existing value for that key.
If you want to add a new value, you have to give a new key to that value.
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