Get list from stream java

5 ways to Convert Java 8 Stream to List — Example, Tutorial

One of the common problems while working with Stream API in Java 8 is how to convert a Stream to List in Java because there is no toList() method present in the Stream class. When you are processing a List using Stream’s map and filter method, you ideally want your result in some collection so that you can pass it to other parts of the program. Though java.util.stream.Stream class has toArray() method to convert Stream to Array, but there is no similar method to convert Stream to List or Set. Java has a design philosophy of providing conversion methods between new and old API classes e.g. when they introduced Path class in JDK 7, which is similar to java.io.File , they provided a toPath() method to File class.

Similarly, they could have provided convenient methods like toList() , toSet() into Stream class, but unfortunately, they have not done that. Anyway, It seems they did think about this and provided a class called Collector and Collectors to collect the result of stream operations into different containers or Collection classes.

Here you will find methods like toList() , which can be used to convert Java 8 Stream to List. You can also use any List implementation class e.g. ArrayList or LinkedList to get the contents of that Stream. I would have preferred having those methods at least the common ones directly into Stream but nevertheless, there is something you can use to convert a Java 8 Stream to List.

Читайте также:  Всплывающее вертикальное меню html

By the way, my research for this task also reveals several other ways to achieve the same result, which I have summarized in this tutorial. I would suggest preferring a standard way, which is by using Stream.collect() and Collectors class, but it’s good to know other ways, just in case if you need it.

And, If you are serious about improving Java functional programming skills then I highly recommend you check out the Learn Java Functional Programming with Lambdas & Streams by Rang Rao Karnam on Udemy, which explains both Functional Programming and Java Stream fundamentals in good detail

Java 8 Stream to List conversion — 5 examples

Here are 5 simple ways to convert a Stream in Java 8 to List e.g. converting a Stream of String to a List of String, or converting a Stream of Integer to List of Integer, and so on.

1. Using Collectors.toList() method

This is the standard way to collect the result of the stream in a container like a List, Set, or any Collection. Stream class has a collect() method which accepts a Collector and you can use Collectors.toList() method to collect the result in a List.

List listOfStream = streamOfString.collect(Collectors.toList());

2. Using Collectors.toCollection() method

This is actually a generalization of the previous method, here instead of creating a List, you can collect elements of Stream in any Collection, including ArrayList , LinkedList, or any other List . In this example, we are collecting Stream elements into ArrayList.

The toColection() method returns a Collector that accumulates the input elements into a new Collection, in encounter order. The Collection is created by the provided Supplier instance, in this case, we are using ArrayList::new , a constructor reference to collect them into ArrayList.

List listOfString = streamOfString .collect(Collectors.toCollection(ArrayList::new));

You can also use lambda expression in place of method reference here, but method reference results in much more readable and concise code.

3. Using forEach() method

You can also use the forEach() method to go through all elements of Stream one by one and add them into a new List or ArrayList. This is a simple, pragmatic approach, which beginners can use to learn.

Stream streamOfLetters = Stream.of("abc", "cde", "efg", "jkd", "res"); ArrayList list = new ArrayList<>(); streamOfLetters.forEach(list::add);

4. Using forEachOrdered method

This is an extension of the previous example, if Stream is parallel then elements may be processed out of order but if you want to add them into the same order they were present in Stream, you can use the forEachOrdered() method. By the way, If you are not comfortable with forEach, I suggest looking at this Stream tutorial to understand more.

Stream streamOfNumbers = Stream.of("one", "two", "three", "four", "five"); ArrayList myList = new ArrayList<>(); streamOfNumbers.parallel().forEachOrdered(myList::add);

5. Using toArray() method

Stream provides a direct method to convert Stream to the array, toArray() . The method which accepts no argument returns an object array as shown in our sample program, but you can still get which type of array you want by using an overloaded version of that method. Just like we have done in the following example to create String array :

Stream streamOfShapes = Stream.of("Rectangle", "Square", "Circle", "Oval"); String[] arrayOfShapes = streamOfShapes.toArray(String[]::new); List listOfShapes = Arrays.asList(arrayOfShapes);

Sample Program to convert Stream to List in Java 8

Here is the complete Java program to demonstrate all these five methods of converting Java 8 Streams to List. You can directly run them if you have installed JDK 8 on your machine.

Java 8 Stream to List Example

BTW, You should be using Netbeans with JDK 8 to code, compile and run Java programs. The IDE has got excellent support and will help you to learn Java 8 quickly.

package test; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Java Program to convert Stream to List in Java 8 * * @author Javin Paul */ public class Java8StreamToList< public static void main(String args[]) throws IOException < StreamString> streamOfString = Stream.of("Java", "C++", "JavaScript", "Scala", "Python"); // converting Stream to List using Collectors.toList() method streamOfString = Stream.of("code", "logic", "program", "review", "skill"); ListString> listOfStream = streamOfString.collect(Collectors.toList()); System.out.println("Java 8 Stream to List, 1st example : " + listOfStream); // Java 8 Stream to ArrayList using Collectors.toCollection method streamOfString = Stream.of("one", "two", "three", "four", "five"); listOfStream = streamOfString.collect( Collectors.toCollection(ArrayList::new)); System.out.println("Java 8 Stream to List, 2nd Way : " + listOfStream); // 3rd way to convert Stream to List in Java 8 streamOfString = Stream.of("abc", "cde", "efg", "jkd", "res"); ArrayListString> list = new ArrayList<>(); streamOfString.forEach(list::add); System.out.println("Java 8 Stream to List, 3rd Way : " + list); // 4th way to convert Parallel Stream to List streamOfString = Stream.of("one", "two", "three", "four", "five"); ArrayListString> myList = new ArrayList<>(); streamOfString.parallel().forEachOrdered(myList::add); System.out.println("Java 8 Stream to List, 4th Way : " + myList); // 5th way of creating List from Stream in Java // but unfortunately this creates array of Objects // as opposed to array of String StreamString> streamOfNames = Stream.of("James", "Jarry", "Jasmine", "Janeth"); Object[] arrayOfString = streamOfNames.toArray(); ListObject> listOfNames = Arrays.asList(arrayOfString); System.out.println("5th example of Stream to List in Java 8 : " + listOfNames); // can we convert the above method to String[] instead of // Object[], yes by using overloaded version of toArray() // as shown below : StreamString> streamOfShapes = Stream.of("Rectangle", "Square", "Circle", "Oval"); String[] arrayOfShapes = streamOfShapes.toArray(String[]::new); ListString> listOfShapes = Arrays.asList(arrayOfShapes); System.out.println("modified version of last example : " + listOfShapes); > > Output : Java 8 Stream to List, 1st example : [code, logic, program, review, skill] Java 8 Stream to List, 2nd Way : [one, two, three, four, five] Java 8 Stream to List, 3rd Way : [abc, cde, efg, jkd, res] Java 8 Stream to List, 4th Way : [one, two, three, four, five] 5th example of Stream to List in Java 8 : [James, Jarry, Jasmine, Janeth] modified version of last example : [Rectangle, Square, Circle, Oval]

That’s all about how to convert Stream to List in Java 8. You have seen there are several ways to perform the conversion but I would suggest you stick with the standard approach i.e. by using the Stream.collect(Collectors.toList()) method.

  • Java 8 — 20 Date and Time Examples for beginners (see tutorial)
  • 5 FREE Java 8 tutorials and Books (resources)
  • How to Read File in Java 8 in one line? (example)
  • Simple Java 8 Comparator Example (example)
  • Top 10 tutorials to Learn Java 8 (tutorial)
  • How to use Map function in Java 8 (example)
  • Thinking of Java 8 Certification? (read more)
  • How to read/write RandomAccessFile in Java? (solution)
  • How to use Lambda Expression in Place of Anonymous class (solution)
  • 10 Examples of Stream API in Java (examples)
  • How to filter Collection in Java 8 using Predicates? (solution)
  • How to use the Default method in Java 8. (see here)
  • 10 Java 7 Feature to revisit before you start with Java 8 (read more)

Thanks for reading this article so far. If you like the Java 8 Stream and List tutorial and example then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

Источник

How do I convert a Java 8 IntStream to a List?

@KarlRichter The other question doesn’t give you a typed list. Also, this question was from four years ago, and has an answer with 300+ upvotes. Why are we trying to merge it now?

5 Answers 5

IntStream::boxed

theIntStream.boxed().collect(Collectors.toList()) 

The boxed method converts the int primitive values of an IntStream into a stream of Integer objects. The word «boxing» names the int ⬌ Integer conversion process. See Oracle Tutorial.

Java 16 and later

Java 16 brought the shorter toList method. Produces an unmodifiable list. Discussed here.

@skiwi I mean, that all the other answers are unneeded after this one as they would be not so natural.

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()) .

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.

Was tearing my hair out about what was wrong with what I had tried, thank you for pointing out the boxed() part

Источник

Get back list of string from an stream

I have an stream that contains Strings and list of Strings and I want to get out all values as List of Strings. Anyway to do this with some stream operation ?

Stream stream = Stream.of("v1", Arrays.asList("v2, v3")); 

3 Answers 3

Normally you don’t mix up such different types in a list, but you can get the result you want from this; each single string can be converted to a stream of one string, and each list of strings can be converted to a stream of multiple strings; then flatMap will flatten all the streams to one single stream of strings.

List strings = l.stream() .flatMap(o -> < if (o instanceof List) < return ((List) o).stream(); > return Stream.of((String) o); >) .collect(Collectors.toList()); 

You’ll get an unchecked cast warning, but that’s what you get for mixing up different types in one container.

@Nathan No, the warning is from casting to (List) , because Java won’t be able to verify the generic type. An instanceof will have no effect.

@poyger The way to avoid the instanceof is to not mix different types of object inside one container. As long as they’re mixed up, you’ll need something like instanceof to separate them.

You can get rid of the “unchecked” warning, but of course, there’s no way to get rid of the instanceof …

If you want to avoid “unchecked” warnings, you have to cast each element, which works best when you perform it as a subsequent per-element operation, after the flatMap step, when the single String elements and List instances are already treated uniformly:

List list = new ArrayList<>(); list.add("v1"); list.add(Arrays.asList("v2","v3")); List strings = list.stream() .flatMap(o -> o instanceof List? ((List)o).stream(): Stream.of(o)) .map(String.class::cast) .collect(Collectors.toList()); 

But, as already said by others, not having such a mixed type list in the first place, is the better option.

Источник

Оцените статью