Convert List<Students> to Map<K, List<V>> [closed] - java

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())));

Related

Java 8 stream - sum list of numbers for every map key [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Is there a simple/elegant way of summing numbers in a list for every key in the map, e.g. from Map<String, List<BigDecimal>> I want to get Map<String, BigDecimal>? I couldn't find it/figure it out...
To change from Map<String, List<BigDecimal>> to Map<String, BigDecimal> there are two steps :
First step : use Collectors.toMap to take the keys of the old map to the new map.
Second step : you have to sum all the elements of your List<BigDecimal> there are many ways one of them is to use e.getValue().stream().reduce(BigDecimal.ZERO, BigDecimal::add)
In the end your code can look like this :
Map<String, BigDecimal> result = map.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().stream().reduce(BigDecimal.ZERO, BigDecimal::add))
);
Ideone example
map.entrySet()
.stream()
.collect(Collectors.toMap(
Entry::getKey,
x -> x.getValue().stream()
.reduce(BigDecimal::add)
.orElse(BigDecimal.ZERO)));

How do I stop HashMap from overwriting previous value for a key? [closed]

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.

Count number of occurrences in various lists Java [closed]

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
I've got in Java a map of this type
Map<Group, List<Person>>
that is a set of groups with the whole list of members.
I want to find the Person that is in the largest number of groups using streams and lambda expressions, I tried something but it wasn't successful.
Can you help me please? Thanks
What you need is .flatMap() followed by a .collect() which finds the frequency of each person in the overall Map.
Something like this:
Person socialButterfly = groupMap.values()
.stream()
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
.max(Map.Entry.comparingByValue())
.get().getKey();
Ideone Tested

Map<String, Integer> foo - how do I get the value of the integer - JAVA [closed]

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 6 years ago.
Improve this question
I have a method:
public void updateBoard (Map<String, Integer> foo)
How do I find out the value of the Integer? I'm trying foo.get() but it just gives me the key.
Thanks!
Here is how map works
Map <String,Integer> myMap = new HashMap<String,Integer>();
myMap.put("manikant",123);
// Many more put..
System.out.println(myMap.get("manikant"));
// In case you are using java 8. you can also try this.
myMap.forEach( (k,v) -> System.out.println("Key: " + k + ": Value: " + v));
output
123
in your case you can use foo.get(/*enter your key*/);
For more Information see how map works in java
You get all Integer values of the Map with:
foo.values();
Because a Map is a key->value construct.
With
foo.keySet();
you get all keys.
And with
foo.get("key1");
you get the appropriate value for the key key1.
If you are trying to get all the values in the map, use foo.values().
If you are trying to get the value to a specific key in the map, use foo.get(<key>).
Hope it helps!

Java Collections Map to Set [closed]

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.

Categories