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
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.
Improve this question
I read through several threads about operating on the nested java collections using Lambda but none addressed my specific situation, although this one came close and then took off to a different direction (flatMap). Please show me how to write the following code in Lambda.
for(AppUser user : users){
List<CustomerOrder> orders = user.getOrders();
for(CustomerOrder order : orders){
order.setConsumer(user);
List<LineItem> items = order.getLineItems();
for (LineItem item : items){
item.setOrder(order);
}
}
}
Thanks
If all you want to do is iterate, the forEach method on either the Stream.java or the Iterable.java should be enough. Here is the "streamy" way of doing what you are trying to do with the for loops
users.stream().forEach( user ->
user.getOrders().stream().forEach( order -> {
order.setConsumer(user);
order.getLineItems().stream().forEach(
lineItem -> lineItem.setOrder(order)
);
}
));
Although you don't really need to convert the iterable returned by by those getters to streams.
This is a solution I came up with but I see that Smarth Ktyal already posted an answer.
users
.stream()
.forEach(e->e.getOrders()
.stream()
.forEach(k->{
k.setConsumer(e);
k.getLineItems()
.stream()
.forEach(l->l.setOrder(k));
}
)
);
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 3 years ago.
Improve this question
It's probably sth simple, but I can't find answers why I can't start stream straight after creating List using Arrays.asList. 'list' is working fine, but 'list2' doesn't, no help from IntelliJ
List<Book> list = Arrays.asList(lalka,dziady,chlopi,jutrzenka);
list.stream()
.map((Book var)-> var.getAuthor().getName())
.forEach(var-> System.out.println(var));
List<Book> list2 = Arrays.asList(lalka, dziady).stream()
.map((Book var) -> var.getAuthor().getName())
.forEach(var-> System.out.println(var));
Your stream pipeline doesn't return anything (the terminal operation forEach has a void return type) so you can't assign it to a List variable.
You can write:
Arrays.asList(lalka, dziady)
.stream()
.map((Book var) -> var.getAuthor().getName())
.forEach(var-> System.out.println(var));
You're trying to assign result of .forEach to a List<Book> list2 variable.
.forEach(...) is of type void.
Remove this unnecessary assignment.
The problem is with the assignment to List list2.
You'll probably be getting an error that you cannot convert void to list
as .forEach() does not return anything.
Remove the assignment to list2 and this compiles and runs just fine.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to filter as HashMap of String/Int by they key, matching on a RegEx
Map<String, Integer> files = new HashMap<>();
files.put("TEST_SALES1212312.zip", 1212312);
files.put("TEST_SALES9846545.zip", 9846545);
files.put("TEST_INVENTORY2153516.zip", 2153516);
files.put("TEST_INVENTORY3651321.zip", 3651321);
String regex = "(TEST_SALES|.)+(.zip)";
List<Map.Entry<String, Integer>> matches = files.entrySet().stream()
.filter(x -> x.getKey().matches(regex))
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toList());
matches.forEach(x -> System.out.println(x.getKey()));
I tested on RegEx Tester and it correctly filtered the inventory items out. However, the above code does not filter any items.
How do I solve this problem?
This RegEx might help you to solve your problem. It creates a target group () for numeric values of your zip files, which you can simply get using $1:
TEST_[A-Z]+([0-9]+).zip
You can escape language specific metachars using \, if necessary, such as ..
If your inputs are only limited to your exampled codes, you may reduce the regex boundaries, if you wish, such as this RegEx, and it might still match:
[A-Z_]+([0-9]+).zip
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 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)