493

I was playing around with Java 8 lambdas to easily filter collections. But I did not find a concise way to retrieve the result as a new list within the same statement. Here is my most concise approach so far:

List<Long> sourceLongList = Arrays.asList(1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L);
List<Long> targetLongList = new ArrayList<>();
sourceLongList.stream().filter(l -> l > 100).forEach(targetLongList::add);

Examples on the net did not answer my question because they stop without generating a new result list. There must be a more concise way. I would have expected, that the Stream class has methods as toList(), toSet(), …

Is there a way that the variables targetLongList can be directly be assigned by the third line?

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
Daniel K.
  • 5,747
  • 3
  • 19
  • 22
  • 7
    In case you don’t need the `sourceLongList` afterwards there’s [`Collection.removeIf(…)`](http://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeIf-java.util.function.Predicate-) for convenience. – Holger May 08 '14 at 10:39
  • 3
    How about this? `List targetLongList = sourceLongList.stream().collect(Collectors.toList());` – leeyuiwah Jul 25 '18 at 22:33

15 Answers15

664

What you are doing may be the simplest way, provided your stream stays sequential—otherwise you will have to put a call to sequential() before forEach.

[later edit: the reason the call to sequential() is necessary is that the code as it stands (forEach(targetLongList::add)) would be racy if the stream was parallel. Even then, it will not achieve the effect intended, as forEach is explicitly nondeterministic—even in a sequential stream the order of element processing is not guaranteed. You would have to use forEachOrdered to ensure correct ordering. The intention of the Stream API designers is that you will use collector in this situation, as below.]

An alternative is

targetLongList = sourceLongList.stream()
    .filter(l -> l > 100)
    .collect(Collectors.toList());
Maurice Naftalin
  • 10,354
  • 2
  • 24
  • 15
  • 11
    Addition: I think this codes gets a little shorter, clearer and prettier if you use a static import of `toList`. This is done by placing the following among the imports of the file: `static import java.util.stream.Collectors.toList;`. Then the collect call reads just `.collect(toList())`. – Lii Jun 29 '16 at 08:54
  • 4
    In Eclipse it is possible to make the IDE add a static import for methods. This is done by adding the `Collectors` class in *Preferences* -> *Java* -> *Editor* -> *Content Assist* -> *Favorites*. After this, you only have to type `toLi` at hit *Ctr+Space* to have the IDE fill in `toList` and add the static import. – Lii Jun 29 '16 at 08:58
  • 5
    One thing to keep in mind is that `IntStream` and some other almost-but-not-quite-`Stream`s do not have the `collect(Collector)` method and you will have to call `IntStream.boxed()` to convert them to a regular `Stream` first. Then again, maybe you just want `toArray()` . – Mutant Bob Sep 12 '17 at 19:09
  • why we have to use `sequential()` before `forEach` or use 'forEachOrdered` – amarnath harish Sep 18 '18 at 07:17
  • 1
    @amarnathharish Because forEach doesn't guarantee the order of operation execution for a parallel stream. The JavaDoc says "The behavior of this operation is explicitly nondeterministic. For parallel stream pipelines, this operation does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism." (The first sentence of this quote actually means that order is not guaranteed for sequential streams either, although in practice it is preserved.) – Maurice Naftalin Sep 19 '18 at 08:10
  • why cannot it be as simple as C# syntax: `(some ienumerable).ToList()`? – Lei Yang Jul 01 '20 at 01:33
195

Updated:

Another approach is to use Collectors.toList:

targetLongList = 
    sourceLongList.stream().
    filter(l -> l > 100).
    collect(Collectors.toList());

Previous Solution:

Another approach is to use Collectors.toCollection:

targetLongList = 
    sourceLongList.stream().
    filter(l -> l > 100).
    collect(Collectors.toCollection(ArrayList::new));
MohamedSanaulla
  • 6,112
  • 5
  • 28
  • 45
  • 69
    This is, however, useful if you want a particular List implementation. – orbfish Jun 14 '15 at 01:03
  • 1
    Despite beeing recommended to code against interfaces, there are clear cases (one of them being GWT) when you have to code against concrete implementations (unless you want all List implementations compiled and delivered as javascript). – Eduard Korenschi Dec 01 '15 at 14:09
  • 4
    Another pro for this method, from the `Collectors::toList` javadoc: "There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned; if more control over the returned List is required, use `toCollection(Supplier)`." – Charles Wood Nov 07 '17 at 16:21
13

I like to use a util method that returns a collector for ArrayList when that is what I want.

I think the solution using Collectors.toCollection(ArrayList::new) is a little too noisy for such a common operation.

Example:

ArrayList<Long> result = sourceLongList.stream()
    .filter(l -> l > 100)
    .collect(toArrayList());

public static <T> Collector<T, ?, ArrayList<T>> toArrayList() {
    return Collectors.toCollection(ArrayList::new);
}

With this answer I also want to demonstrate how simple it is to create and use custom collectors, which is very useful generally.

Lii
  • 11,553
  • 8
  • 64
  • 88
  • If you declare result as List you don't need to use this util method. Collectors.toList will do. Also, using specific classes instead of interfaces is a code smell. – Lluis Martinez Feb 17 '17 at 21:04
  • 2
    @LluisMartinez: *"Collectors.toList will do."*: No, not in many situations. Because it's not a good idea to use `toList` if you for example want to modify the list later in the program. The [`toList` documentation](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Collectors.html#toList--) says this: *"There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned; if more control over the returned List is required, use `toCollection`."*. My answer demonstrates a way to make it more convenient to do that in a common case. – Lii Feb 18 '17 at 10:00
  • If you want to specifically create an ArrayList then it's ok. – Lluis Martinez Feb 20 '17 at 16:34
12

There is a new method Stream.toList() in Java 16:

List<Long> targetLongList = sourceLongList
         .stream()
         .filter(l -> l > 100)
         .toList();
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
6
collect(Collectors.toList());

This is the call which you can use to convert any Stream to List.

more concretely:

    List<String> myList = stream.collect(Collectors.toList()); 

from:

https://www.geeksforgeeks.org/collectors-tolist-method-in-java-with-examples/

Thufir
  • 8,216
  • 28
  • 125
  • 273
Pramod
  • 387
  • 1
  • 7
  • 19
5

If you have an array of primitives, you can use the primitive collections available in Eclipse Collections.

LongList sourceLongList = LongLists.mutable.of(1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L);
LongList targetLongList = sourceLongList.select(l -> l > 100);

If you can't change the sourceLongList from List:

List<Long> sourceLongList = Arrays.asList(1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L);
List<Long> targetLongList = 
    ListAdapter.adapt(sourceLongList).select(l -> l > 100, new ArrayList<>());

If you want to use LongStream:

long[] sourceLongs = new long[]{1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L};
LongList targetList = 
    LongStream.of(sourceLongs)
    .filter(l -> l > 100)
    .collect(LongArrayList::new, LongArrayList::add, LongArrayList::addAll);

Note: I am a contributor to Eclipse Collections.

Nikhil Nanivadekar
  • 1,152
  • 11
  • 10
5

A little more efficient way (avoid the creating the source List and the auto-unboxing by the filter):

List<Long> targetLongList = LongStream.of(1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L)
    .filter(l -> l > 100)
    .boxed()
    .collect(Collectors.toList());
msayag
  • 8,407
  • 4
  • 30
  • 29
1

If you don't mind using 3rd party libraries, AOL's cyclops-react lib (disclosure I am a contributor) has extensions for all JDK Collection types, including List. The ListX interface extends java.util.List and adds a large number of useful operators, including filter.

You can simply write-

ListX<Long> sourceLongList = ListX.of(1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L);
ListX<Long> targetLongList = sourceLongList.filter(l -> l > 100);

ListX also can be created from an existing List (via ListX.fromIterable)

John McClean
  • 5,225
  • 1
  • 22
  • 30
1

There is an another variant of collect method provided by LongStream class and similarly by IntStream and DoubleStream classes too .

<R> R collect(Supplier<R> supplier,
              ObjLongConsumer<R> accumulator,
              BiConsumer<R,R> combiner)

Performs a mutable reduction operation on the elements of this stream. A mutable reduction is one in which the reduced value is a mutable result container, such as an ArrayList, and elements are incorporated by updating the state of the result rather than by replacing the result. This produces a result equivalent to:

R result = supplier.get();
  for (long element : this stream)
       accumulator.accept(result, element);
  return result;

Like reduce(long, LongBinaryOperator), collect operations can be parallelized without requiring additional synchronization. This is a terminal operation.

And answer to your question with this collect method is as below :

    LongStream.of(1L, 2L, 3L, 3L).filter(i -> i > 2)
    .collect(ArrayList::new, (list, value) -> list.add(value)
    , (list1, list2) -> list1.addAll(list2));

Below is the method reference variant which is quite smart but some what tricky to understand :

     LongStream.of(1L, 2L, 3L, 3L).filter(i -> i > 2)
    .collect(ArrayList::new, List::add , List::addAll);

Below will be the HashSet variant :

     LongStream.of(1L, 2L, 3L, 3).filter(i -> i > 2)
     .collect(HashSet::new, HashSet::add, HashSet::addAll);

Similarly LinkedList variant is like this :

     LongStream.of(1L, 2L, 3L, 3L)
     .filter(i -> i > 2)
     .collect(LinkedList::new, LinkedList::add, LinkedList::addAll);
Vaneet Kataria
  • 575
  • 5
  • 14
1

To collect in a mutable list:

targetList = sourceList.stream()
                       .filter(i -> i > 100) //apply filter
                       .collect(Collectors.toList());

To collect in a immutable list:

targetList = sourceList.stream()
                       .filter(i -> i > 100) //apply filter
                       .collect(Collectors.toUnmodifiableList());

Explanation of collect from the JavaDoc:

Performs a mutable reduction operation on the elements of this stream using a Collector. A Collector encapsulates the functions used as arguments to collect(Supplier, BiConsumer, BiConsumer), allowing for reuse of collection strategies and composition of collect operations such as multiple-level grouping or partitioning. If the stream is parallel, and the Collector is concurrent, and either the stream is unordered or the collector is unordered, then a concurrent reduction will be performed (see Collector for details on concurrent reduction.)

This is a terminal operation.

When executed in parallel, multiple intermediate results may be instantiated, populated, and merged so as to maintain isolation of mutable data structures. Therefore, even when executed in parallel with non-thread-safe data structures (such as ArrayList), no additional synchronization is needed for a parallel reduction.

Saikat
  • 14,222
  • 20
  • 104
  • 125
0

In case someone (like me) out there is looking for ways deal with Objects instead of primitive types then use mapToObj()

String ss = "An alternative way is to insert the following VM option before "
        + "the -vmargs option in the Eclipse shortcut properties(edit the "
        + "field Target inside the Shortcut tab):";

List<Character> ll = ss
                        .chars()
                        .mapToObj(c -> new Character((char) c))
                        .collect(Collectors.toList());

System.out.println("List type: " + ll.getClass());
System.out.println("Elem type: " + ll.get(0).getClass());
ll.stream().limit(50).forEach(System.out::print);

prints:

List type: class java.util.ArrayList
Elem type: class java.lang.Character
An alternative way is to insert the following VM o
Kashyap
  • 15,354
  • 13
  • 64
  • 103
0

Here is code by abacus-common

LongStream.of(1, 10, 50, 80, 100, 120, 133, 333).filter(e -> e > 100).toList();

Disclosure: I'm the developer of abacus-common.

user_3380739
  • 1
  • 14
  • 14
0
String joined = 
                Stream.of(isRead?"read":"", isFlagged?"flagged":"", isActionRequired?"action":"", isHide?"hide":"")
                      .filter(s -> s != null && !s.isEmpty())
                      .collect(Collectors.joining(","));
Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40
sharath
  • 215
  • 3
  • 4
0

You can rewrite code as below :

List<Long> sourceLongList = Arrays.asList(1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L);
List<Long> targetLongList = sourceLongList.stream().filter(l -> l > 100).collect(Collectors.toList());
B--rian
  • 5,578
  • 10
  • 38
  • 89
  • Thanks for your input. However, please explain what you changed and in how far it relates to the question. – B--rian Jul 26 '19 at 12:34
  • Here firstly I converted my ArrayList to steam them using filter I filter out required data. Finally I have used collect method of java 8 stream to collect data in new list called as targetLongList. – Pratik Pawar Jul 30 '19 at 12:53
-3

If you don't use parallel() this will work

List<Long> sourceLongList = Arrays.asList(1L, 10L, 50L, 80L, 100L, 120L, 133L, 333L);

List<Long> targetLongList =  new ArrayList<Long>();

sourceLongList.stream().peek(i->targetLongList.add(i)).collect(Collectors.toList());
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Debapriya Biswas
  • 1,079
  • 11
  • 23
  • I don't like that the collect() is only used to drive the stream so that the peek() hook is called on each item. The result of the terminal operation is discarded. – Daniel K. Aug 12 '16 at 07:24
  • It is very weird to call `collect` and then not save the return value. In that case you could use `forEach` instead. But that is still a poor solution. – Lii Aug 23 '16 at 07:23
  • 1
    Using peek() in this way is an antipattern. – Grzegorz Piwowarek Jul 25 '17 at 17:28
  • As per Stream Java docs peek method must be used for debugging purposes only .It should not be used for any processing other than debugging . – Vaneet Kataria Jan 25 '19 at 11:23