How To Get ArrayList From Stream in Java 8 | Code Factory

package com.codeFactory;import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author code.factory
*
*/
public class ArrayListFromStream {
public static void main(String... args) {
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);

ArrayList<Integer> arrayList = getArrayListFromStream(stream);

System.out.println("ArrayList : " + arrayList);
}

private static <T> ArrayList<T> getArrayListFromStream(Stream<T> stream) {
return new ArrayList<T>(stream.collect(Collectors.toList()));
}
}
ArrayList : [1, 2, 3, 4, 5]
package com.codeFactory;import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author code.factory
*
*/
public class ArrayListFromStream {
public static void main(String... args) {
Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);

ArrayList<Integer> arrayList = getArrayListFromStream(stream);

System.out.println("ArrayList : " + arrayList);
}

private static <T> ArrayList<T> getArrayListFromStream(Stream<T> stream) {
return new ArrayList<T>(stream.collect(Collectors.toCollection(ArrayList::new)));
}
}
ArrayList : [1, 2, 3, 4, 5]

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The team is re-issuing $Boxch tokens with the preparation of a smart contract for cross platform…

How to actually bypass MOE’s DMA on chromebook

The data directory contains an unreadable postmaster.pid file on PostgreSQL 12 | Mac OS

Picturing ourselves on the other side can actually lead to a lot more happiness

HTML Tutorial #1

Testnet Wayru

Snowflake Admin Roles and Responsibilities

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Useless Java: Day 10

Summing Java Streams Api

summing java streams api using java 8 streams features

Java: Synchronizing ints

Java OOPS Concepts