java-stream

How to iterate nested for loops referring to parent elements using Java 8 streams?

别说谁变了你拦得住时间么 提交于 2019-12-04 11:19:28
问题 I want to iterate nested lists using java8 streams , and extract some results of the lists on first match. Unfortunately I have to also get a values from the parent content if a child element matches the filter. How could I do this? java7 Result result = new Result(); //find first match and pupulate the result object. for (FirstNode first : response.getFirstNodes()) { for (SndNode snd : first.getSndNodes()) { if (snd.isValid()) { result.setKey(first.getKey()); result.setContent(snd.getContent

Java stream merge or reduce duplicate objects

非 Y 不嫁゛ 提交于 2019-12-04 11:14:30
问题 I need to generate a unique friend list from a list that can have duplicates by merging all duplicate entries into one object Example - Friends are fetched from different social feeds and put into 1 big list 1. Friend - [name: "Johnny Depp", dob: "1970-11-10", source: "FB", fbAttribute: ".."] 2. Friend - [name: "Christian Bale", dob: "1970-01-01", source: "LI", liAttribute: ".."] 3. Friend - [name: "Johnny Depp", dob: "1970-11-10", source: "Twitter", twitterAttribute: ".."] 4. Friend - [name:

Why can't we throw exception inside Java 8 stream?

落花浮王杯 提交于 2019-12-04 10:40:05
E.g: Person result = persons.stream() .filter(x -> { if ("test".equals(x.getName() ) ) { throw new IOException("not possible inside stream y ?"); //any checked exception } //code here }) M looking for reason why it is not being allowed ? Even if method in which code is declared throws IOException Thierry your code is working perfectly, apart from a missing semi colon at the end of the throw new ... line, and from a missing a return statement that may be hidden in // code here . What you can't do is throw a checked exception (which RuntimeException is not) because checked exception are part of

Recursively Flatten values of nested maps in Java 8

对着背影说爱祢 提交于 2019-12-04 10:29:38
问题 Given a Map<String, Object> , where the values are either a String or another Map<String, Object> , how would one, using Java 8, flatten the maps to a single list of values? Example: Map - "key1" -> "value1" - "key2" -> "value2" - "key3" -> Map - "key3.1" -> "value3.1" - "key3.2" -> "value3.2" - "key3.3" -> Map - "key3.3.1" -> "value3.3.1" - "key3.3.2" -> "value3.3.2" For the above example, I would like the following list: value1 value2 value3.1 value3.2 value3.3.1 value3.3.2 I know it can be

Java .parallelStream() with spring annotated methods

若如初见. 提交于 2019-12-04 10:23:28
问题 I try using the parallelStream() in DAO with Spring @Transactional annotations and get so problem: @Transactional public void processCollection(Collection<Object> objects) { objects.parallelStream() .forEach(this::processOne); //throw exception } @Transactional public void processOne(Object o) { ... } Works correct: @Transactional public void processCollection(Collection<Object> objects) { objects.stream() .forEach(this::processOne); //work correctly } @Transactional public void processOne

How to choose one of the largest elements in a collection

混江龙づ霸主 提交于 2019-12-04 10:23:04
I have a list of tuples, and I want to find the tuples with the largest x value. In the case where there are multiple largest x values, I want to choose one at random. I can't figure out how to implement this random selection functionality. Below is the code I have so far: public void testSelectRandomFromLargestVals() { List<Tuple<Integer, String>> list = new ArrayList<>(); list.add(new Tuple<>(5, "five-1")); list.add(new Tuple<>(2, "two")); list.add(new Tuple<>(3, "three")); list.add(new Tuple<>(5, "five-2")); list.add(new Tuple<>(5, "five-3")); Optional<Tuple<Integer, String>> largestTuple =

Populate a map conditionally using streams - Java 8

我与影子孤独终老i 提交于 2019-12-04 10:11:59
I'm trying to translate this (simplified) code to use Java-8 streams: Map<String, String> files = new ConcurrentHashMap<String, String>(); while(((line = reader.readLine()) != null) { if(content != null) files.put("not null"+line, "not null"+line); else files.put("its null"+line, "its null"+line); } reader.close(); Here is what I've tried: files = reader.lines().parallel().collect((content != null)? (Collectors.toConcurrentMap(line->"notnull"+line, line->line+"notnull")) : (Collectors.toConcurrentMap(line->line+"null", line->line+"null"))); But the above gives a "cyclic inference" message for

Java 8: How to convert String to Map<String,String>?

怎甘沉沦 提交于 2019-12-04 10:11:35
I have a Map: Map<String, String> utilMap = new HashMap(); utilMap.put("1","1"); utilMap.put("2","2"); utilMap.put("3","3"); utilMap.put("4","4"); I converted it to a String: String utilMapString = utilMap .entrySet() .stream() .map(e -> e.toString()).collect(Collectors.joining(",")); Out put: 1=1,2=2,3=3,4=4,5=5 How to convert utilMapString to Map in Java8? Who can help me with? Split the string by , to get individual map entries. Then split them by = to get the key and the value. Map<String, String> reconstructedUtilMap = Arrays.stream(utilMapString.split(",")) .map(s -> s.split("="))

Performing more than one reduction in a single pass

廉价感情. 提交于 2019-12-04 10:02:56
What is the idiom for performing more than one reduction in a single pass of a stream? Is it just to have one big reducer class, even if this violates SRP if more than one type of reduction computation is required? Stuart Marks Presumably you want to avoid making multiple passes, as the pipeline stages might be expensive. Or you want to avoid collecting the intermediate values in order to run them through multiple collectors, since the cost of storing all the values might be too high. As Brian Goetz noted , Collectors.summarizingInt will collect int values and perform multiple reductions on

java8 stream style for retrieving a inner part of map through a field list?

房东的猫 提交于 2019-12-04 09:53:56
For example, given a map like below: { "k1": { "k2": { "k3": { "k4": "v" } } } } and a field list ["k1","k2","k3"] , I need to retrieve the part {"k4": "v"} . Below is my java7-style code: // Ignore the map building code. Map map1 = new HashMap(); Map map2 = new HashMap(); Map map3 = new HashMap(); Map map4 = new HashMap(); map4.put("k4", "v"); map3.put("k3", map4); map2.put("k2", map3); map1.put("k1", map2); Map map = map1; System.out.println(map); //=> {k1={k2={k3={k4=v}}}} // Code to be transformed to java8 style List<String> fields = Arrays.asList("k1", "k2", "k3"); for(String field: