java-8

How create a new map from the values in an existing map

*爱你&永不变心* 提交于 2019-12-30 15:02:07
问题 Having the next original map: G1=[7,8,45,6,9] G2=[3,9,34,2,1,65] G3=[6,5,9,1,67,5] Where G1, G2 and G3 are groups of people's ages, How can I create a new map like this: 45=[7,8,45,6,9] 65=[3,9,34,2,1,65] 67=[6,5,9,1,67,5] Where the new keys are the max people's age in each group. I have tried this: Map<Integer, List<Integer>> newMap = originalMap.entrySet().stream() .collect(Collectors.toMap(Collections.max(x -> x.getValue()), x -> x.getValue())); But the compiler say me: "The target type of

What is the use of inheriting object class methods in functional interface eg- toString, equals

回眸只為那壹抹淺笑 提交于 2019-12-30 12:47:05
问题 I found following code, What is use of inherited equals() and toString() method. @FunctionalInterface public interface FunInterface<T> { // An abstract method declared in the functional interface int test(T o1, T o2); // Re-declaration of the equals() method in the Object class boolean equals(Object obj); String toString(); } 回答1: The main reason to (re)declare such a method, is to extend and document the contract. In case of Comparator.equals(…), it’s not that obvious, as it doesn’t specify

Does a good use case exist for skip() on parallel streams?

不羁的心 提交于 2019-12-30 12:34:51
问题 EDITED on September, 2015 When I initially asked this question on February, 2015, the behaviour reported in the linked question was counter-intuitive, though kind of allowed by the specification (despite some little inconsistencies in the docs). However, Tagir Valeev asked a new question on June, 2015, where I think he clearly demonstrated that the behaviour reported in this question was actually a bug . Brain Goetz answered his question, and admitted that it was a bug to not stop the back

Is it possible to check whether all Java 8 stream elements satify one of given predicates?

二次信任 提交于 2019-12-30 11:09:40
问题 With stream API I could easily check whether all elements satify a given condition, using allMatch(e -> predicate(e)) method. I could also check if any of multiple conditions is satified allMatch(e -> predicateA(e) || predicateB(e) || predicateC(e)) . But is it possible to check if all elements satisfy one of those predicates (either one)? In the previous case it is possible that some elements satisfy A and some of them not, but they satisfy B or C (and vice versa). I could perform allMatch

Java 8 stream replace object in one array from another

ⅰ亾dé卋堺 提交于 2019-12-30 11:05:21
问题 I have two arrays of objects. I want to update one array with updated objects from the second array if the object matches a certain criteria. For example, I have this: public class Foobar { private String name; // Other methods here... public String getName() { return this.name; } } Foobar [] original = new Foobar[8]; // Instantiate them here and set their field values Foobar [] updated = new Foobar[8]; // Instantiate them here and set their field values /* Use Java8 stream operation here * -

Java 8 stream replace object in one array from another

 ̄綄美尐妖づ 提交于 2019-12-30 11:05:06
问题 I have two arrays of objects. I want to update one array with updated objects from the second array if the object matches a certain criteria. For example, I have this: public class Foobar { private String name; // Other methods here... public String getName() { return this.name; } } Foobar [] original = new Foobar[8]; // Instantiate them here and set their field values Foobar [] updated = new Foobar[8]; // Instantiate them here and set their field values /* Use Java8 stream operation here * -

Java: making List from primitive array using Stream API

心不动则不痛 提交于 2019-12-30 10:51:57
问题 I'm trying to make a List from a primitive array int[] values={4,5,2,3,42,60,20}; List<Integer> greaterThan4 = Arrays.stream(values) .filter(value -> value > 4) .collect(Collectors.toList()); But the last function collect gives me an error because it wants other arguments. It wants 3 arguments Supplier, ObjIntConsumer and BiConsumer. I don't understand why it wants 3 arguments when I have seen different examples that just use collect(Collectors.toList()); and get the list. What I'm doing

Java: making List from primitive array using Stream API

守給你的承諾、 提交于 2019-12-30 10:51:53
问题 I'm trying to make a List from a primitive array int[] values={4,5,2,3,42,60,20}; List<Integer> greaterThan4 = Arrays.stream(values) .filter(value -> value > 4) .collect(Collectors.toList()); But the last function collect gives me an error because it wants other arguments. It wants 3 arguments Supplier, ObjIntConsumer and BiConsumer. I don't understand why it wants 3 arguments when I have seen different examples that just use collect(Collectors.toList()); and get the list. What I'm doing

How to get all the distinct values by key inside stream java8

别等时光非礼了梦想. 提交于 2019-12-30 10:49:10
问题 I am currently learning a bit about streams. I have the following JSONArray, and I want to be able to retrieve all the distinct xvalues. datasets: { ds1: { xvalues: [ "(empty)", "x1", "x2" ] }, ds2: { xvalues: [ "(empty)", "x1", "x2", "x3" ] } } I am trying the following code but it doesn't seem quite right.... List<String> xvalues = arrayToStream(datasets) .map(JSONObject.class::cast) .map(dataset -> { try { return dataset.getJSONArray("xvalues"); } catch (JSONException ex) { } return; })

Merging lists under same objects in a list using Java streams

孤街醉人 提交于 2019-12-30 10:45:10
问题 I have two objects like following: public class A { private Integer id; private String name; private List<B> list; public A(Integer id, String name, List<B> list) { this.id = id; this.name = name; this.list = list; } //getters and setters } and public class B { private Integer id; private String name; public B(Integer id, String name) { this.id = id; this.name = name; } //getters and setters } So, A holds a list of B and there is a list of A populated as follows: List<A> list = new ArrayList<