java-stream

Collectors.groupby for Map<String,List<String>

孤街浪徒 提交于 2021-01-15 02:50:19
问题 Forgive me if the solution is very obvious but I can't seem to figure out how to do this public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("b1", "a1"); map.put("b2", "a2"); map.put("b3", "a1"); Map<String, List<String>> mm = map.values().stream().collect(Collectors.groupingBy(m -> m)); System.out.println(mm); } I want to group by based on values in hashmap. I want the output to be {a1=[b1, b3], a2=[b2]} but it is currently coming as {a1=[a1, a1], a2=

Collectors.groupby for Map<String,List<String>

倖福魔咒の 提交于 2021-01-15 02:42:25
问题 Forgive me if the solution is very obvious but I can't seem to figure out how to do this public static void main(String[] args) { Map<String, String> map = new HashMap<>(); map.put("b1", "a1"); map.put("b2", "a2"); map.put("b3", "a1"); Map<String, List<String>> mm = map.values().stream().collect(Collectors.groupingBy(m -> m)); System.out.println(mm); } I want to group by based on values in hashmap. I want the output to be {a1=[b1, b3], a2=[b2]} but it is currently coming as {a1=[a1, a1], a2=

How do you use java stream api to convert list of objects into a nested map based on information stored inside object?

房东的猫 提交于 2021-01-03 07:59:25
问题 I have a list of objects List<SingleDay> where SingleDay is class SingleDay{ private Date date; private String County; // otherstuff } Im looking to convert this list into a Map<Date, Map<String, SingleDay>> . That is, I want a map from Date to a map of Counties back to the original object. For example: 02/12/2020 : { "Rockbridge": {SingleDayObject}} I have not been able to get anything to work and everything I found online if from a list of objects to a map, not a list of objects to a nested

Transform a flat list to domain objects with child objects using java streams

烈酒焚心 提交于 2021-01-02 07:50:31
问题 I have incoming objects with a flat de-normalized structure which I instantiated from a JDBC resultset. The incoming objects mirror the resultset, there's loads of repeated data so I want to convert the data into a list of parent objects with nested child collections, i.e. an object graph, or normalized list. The incoming object's class looks like this: class IncomingFlatItem { String clientCode; String clientName; String emailAddress; boolean emailHtml; String reportCode; String

Transform a flat list to domain objects with child objects using java streams

有些话、适合烂在心里 提交于 2021-01-02 07:49:07
问题 I have incoming objects with a flat de-normalized structure which I instantiated from a JDBC resultset. The incoming objects mirror the resultset, there's loads of repeated data so I want to convert the data into a list of parent objects with nested child collections, i.e. an object graph, or normalized list. The incoming object's class looks like this: class IncomingFlatItem { String clientCode; String clientName; String emailAddress; boolean emailHtml; String reportCode; String

how save document to MongoDb with com.mongodb.reactivestreams.client

两盒软妹~` 提交于 2021-01-01 06:45:01
问题 I am trying to insert a document in MongoDb throw reactive driver: com.mongodb.reactivestreams.client According to official guide "... Only when a Publisher is subscribed to and data requested will the operation happen... Once the document has been inserted the onNext method will be called " and provided an example which I tried bellow without success. By debugging I see that onSubscribe is called but onNext bellow is never called and I checked in MongoDb the document isn't inserted also.

how save document to MongoDb with com.mongodb.reactivestreams.client

喜欢而已 提交于 2021-01-01 06:44:14
问题 I am trying to insert a document in MongoDb throw reactive driver: com.mongodb.reactivestreams.client According to official guide "... Only when a Publisher is subscribed to and data requested will the operation happen... Once the document has been inserted the onNext method will be called " and provided an example which I tried bellow without success. By debugging I see that onSubscribe is called but onNext bellow is never called and I checked in MongoDb the document isn't inserted also.

Sum of values from hashmap using streams

偶尔善良 提交于 2021-01-01 04:51:25
问题 I have an hashmap of huge size (around 10^60). I am putting values in each entry one by one. Problem is to get the sum of values from hashmap for given range of keys. eg: Putting it in simple Hashmap has entries from 0 to 1000 as a key and every key has an value(BigInteger). Now problem is to get sum of values from range (say) 37 to 95. I have tried with iterator but when we go for huge map of size 10^60, it is time taking operation for large range of indices. I am trying it with streams but

Sum of values from hashmap using streams

*爱你&永不变心* 提交于 2021-01-01 04:49:04
问题 I have an hashmap of huge size (around 10^60). I am putting values in each entry one by one. Problem is to get the sum of values from hashmap for given range of keys. eg: Putting it in simple Hashmap has entries from 0 to 1000 as a key and every key has an value(BigInteger). Now problem is to get sum of values from range (say) 37 to 95. I have tried with iterator but when we go for huge map of size 10^60, it is time taking operation for large range of indices. I am trying it with streams but

How to check if two streams are disjoint?

走远了吗. 提交于 2021-01-01 04:16:50
问题 I would like to compare to streams, and check if they have 1 or more elements in common (finding 1 is sufficient to stop looking for more). I want to be able to apply this to Streams containing a custom-created class. For illustration, let's say I have a class that looks like: public class Point { public final int row; public final int col; public Point(int row, int col) { this.row = row; this.col = col; } @Override public boolean equals(Object obj) { if (obj == null) return false; if (obj