java-stream

Create List of Employees with dynamic values using Java streams

拟墨画扇 提交于 2020-06-27 06:31:43
问题 I have use case where I have to create List of default employees with incrementing id, List<Employee> employeeList = new ArrayList<>(); int count = 0; while (count++ <= 100){ Employee employee = new Employee(count, "a"+count); employeeList.add(employee); } I don't have any collection on which I could use stream. Can we do it in functional way? 回答1: You can use IntStream with rangeClosed(int startInclusive, int endInclusive) to generate the count List<Employee> employeeList = IntStream

How to unflatten a flattened hierarchy with Streams API

丶灬走出姿态 提交于 2020-06-26 04:03:11
问题 If I have a hierarchical structure that is still sorted, but flattened - how can I create a parent/child structure with Java Streams API? An example: How do I go from -,A,Foo A,A1,Alpha1 A,A2,Alpha2 -,B,Bar B,B1,Bravo1 B,B2,Bravo2 to - A A1,Alpha1 A2,Alpha2 B B1,Bravo1 B2,Bravo2 A simple non-stream way would be to keep track of the parent column and see if it has changed. I have tried various ways with Collectors and groupingBy, but have not yet found how to do. List<Row> list = new ArrayList

Collectors.reducing to List

可紊 提交于 2020-06-25 10:15:07
问题 Consider this class: @Data @AllArgsConstructor @NoArgsConstructor class User { String name; String languages; } I have a List<User> and I would like to reduce on languages. Input: List<User> list = new ArrayList<>(); list.add(new User("sam", "java")); list.add(new User("sam", "js")); list.add(new User("apollo", "html")); Expected output: [User(name=apollo, languages=html), User(name=sam, languages=java, js)] I can achieve this using following code: List<User> l = list.stream() .collect

How can Java stream reduce accumulator parameter be associative

一笑奈何 提交于 2020-06-25 05:54:36
问题 The question is about java.util.stream.Stream.reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner) method. From the API docs: accumulator – an associative , non-interfering, stateless function for incorporating an additional element into a result Now, the definition of associative is: (a op b) op c = a op (b op c) for all a, b, c As we can see, the above definition requires a BinaryOperator, not only a BiFunction, because a op b requires b of type T while b

Convert a nested for loop that returns boolean to java 8 stream?

孤者浪人 提交于 2020-06-25 05:46:06
问题 I have three class i.e. Engine , Wheel and AutoMobile . The contents of these classes are as follows:- class Engine { String modelId; } class Wheel { int numSpokes; } class AutoMobile { String make; String id; } I have a List<Engine> , a List<Wheel> and a List<Automobile> which I have to iterate through and check for a particular condition. If there is one entity that satisfies this condition, I have to return true; otherwise the function returns false. The function is as follows: Boolean

Why Double::compareTo can be used as an argument of Stream.max(Comparator<? super T> comparator)

◇◆丶佛笑我妖孽 提交于 2020-06-25 04:42:13
问题 The api for Stream.max requires an argument of type Comparator<? super T> , and for Comparator , the only abstract method is int compare(T o1, T o2) but Double::compareTo , the compareTo api is public int compareTo(Double anotherDouble) why just provide one argument, so why can Double::compareTo use as argument of Stream Optional<T> max(Comparator<? super T> comparator) 回答1: The below MyComparator implement a Comparator . It takes two arguments. It's the same as the lambda expression (d1,d2)

How to implement Stream<E> without a resource leak warning in Java

会有一股神秘感。 提交于 2020-06-24 14:15:09
问题 I wish to implement the Stream<E> interface (I admit, it's the unnecessarily large one) and add a builder method foo() . public MyStream<E> implements Stream<E>, ExtendedStream<E> { private final Stream<E> delegate; public MyStream(final Stream<E> stream) { this.delegate = stream; } // a sample Stream<E> method implementation @Override public <R> MyStream<R> map(Function<? super E, ? extends R> mapper) { return new MyStream<>(this.delegate.map(mapper)); } // the rest in the same way (skipped)

Is there a simple way in Java to get the difference between two collections using a custom equals function without overriding the equals?

被刻印的时光 ゝ 提交于 2020-06-24 14:00:11
问题 I'm open to use a lib. I just want something simple to diff two collections on a different criteria than the normal equals function. Right now I use something like : collection1.stream() .filter(element -> !collection2.stream() .anyMatch(element2 -> element2.equalsWithoutSomeField(element))) .collect(Collectors.toSet()); and I would like something like : Collections.diff(collection1, collection2, Foo::equalsWithoutSomeField); (edit) More context: Should of mentioned that I'm looking for

Do sorted and distinct immediately process the stream?

拥有回忆 提交于 2020-06-24 08:31:14
问题 Imagine I have something that looks like this: Stream<Integer> stream = Stream.of(2,1,3,5,6,7,9,11,10) .distinct() .sorted(); The javadocs for both distinct() and sorted() say that they are "stateful intermediate operation". Does that mean that internally the stream will do something like create a hash set, add all the stream values, then seeing sorted() will throw those values into a sorted list or sorted set? Or is it smarter than that? In other words, does .distinct().sorted() cause java

Java - Merge objects of list given a condition

泄露秘密 提交于 2020-06-23 08:11:16
问题 I uttlerly convinced that my question its quite simple but im unable to do it with streams (if there is a way to do it without stream will be helpfull too) Suppose that we have this list of users public class Users { String firstName; String lastName; double accountBalance; String type; String extraField; } and suppose that we have the following data in my List < Users > "Users": [{ "firstName": "Scott", "lastName": "Salisbury", "accountBalance": "100", "type" : "A" }, { "firstName": "John",