java-stream

3 ways to flatten a list of lists. Is there a reason to prefer one of them?

本小妞迷上赌 提交于 2020-01-01 02:01:06
问题 Assume we have a list as follows. CoreResult has a field of type List<Double> . final List<CoreResult> list = new LinkedList<>(SOME_DATA); The objective is to flatten the list after extracting that specific field from each CoreResult object. Here are 3 possible options. Is any one of them preferable to the others? Option 1 : extract a field via map() and flatten inside collector final List<Double> A = list.stream().map(CoreResult::getField) .collect(ArrayList::new, ArrayList::addAll,

Java 8: More efficient way of comparing lists of different types?

人走茶凉 提交于 2019-12-31 21:12:53
问题 In a unit test, I want to verify that two lists contain the same elements. The list to test is build of a list of Person objects, where one field of type String is extracted. The other list contains String literals. One often finds the following code snippet to accomplish this task (see this answer): List<Person> people = getPeopleFromDatabasePseudoMethod(); List<String> expectedValues = Arrays.asList("john", "joe", "bill"); assertTrue(people.stream().map(person -> person.getName()).collect

Java 8: More efficient way of comparing lists of different types?

筅森魡賤 提交于 2019-12-31 21:12:36
问题 In a unit test, I want to verify that two lists contain the same elements. The list to test is build of a list of Person objects, where one field of type String is extracted. The other list contains String literals. One often finds the following code snippet to accomplish this task (see this answer): List<Person> people = getPeopleFromDatabasePseudoMethod(); List<String> expectedValues = Arrays.asList("john", "joe", "bill"); assertTrue(people.stream().map(person -> person.getName()).collect

Java 8 Stream Collecting Set

南笙酒味 提交于 2019-12-31 08:42:27
问题 To better understand the new stream API I'm trying to convert some old code, but I'm stuck on this one. public Collection<? extends File> asDestSet() { HashMap<IFileSourceInfo, Set<File>> map = new HashMap<IFileSourceInfo, Set<File>>(); //... Set<File> result = new HashSet<File>(); for (Set<File> v : map.values()) { result.addAll(v); } return result; } I can't seem to create a valid Collector for it: public Collection<? extends File> asDestSet() { HashMap<IFileSourceInfo, Set<File>> map = new

Java 8 Stream Collecting Set

﹥>﹥吖頭↗ 提交于 2019-12-31 08:42:08
问题 To better understand the new stream API I'm trying to convert some old code, but I'm stuck on this one. public Collection<? extends File> asDestSet() { HashMap<IFileSourceInfo, Set<File>> map = new HashMap<IFileSourceInfo, Set<File>>(); //... Set<File> result = new HashSet<File>(); for (Set<File> v : map.values()) { result.addAll(v); } return result; } I can't seem to create a valid Collector for it: public Collection<? extends File> asDestSet() { HashMap<IFileSourceInfo, Set<File>> map = new

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

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 * -