Java Streams Sources with Examples

In this article, we will learn Java Streams Sources such as List, Set, Array, Map, and Stream.of(), etc. I will share 5 different examples to show the details of stream sources as much as I can. Let’s start!

Java Streams Sources

Creating a Java Stream with Stream.of()

We can create a stream with stream.of() method. I will show how can we do it in the example.

@Test
@Order(1)
public void streamOfTest() {
    Stream.of(4, 2, 3, 5, 9, 6, 1, 8, 7)
        .sorted()
        .forEach(System.out::println);
}

Output

java stream sources

List as a Stream Source

We can do stream operations on Lists. The below example shows using a list as a stream source.

@Test
@Order(2)
public void streamListTest() {
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(4);
    list.add(3);
    list.add(2);

    list.stream()
        .sorted()
        .forEach(System.out::println);
}

Output

List as a stream source in java

Set as a Stream Source

We can do stream operations on Sets. One note I want to share is in Java Sets cannot contain duplicate elements and in the example below, you can see how a set can be used as a stream source.

@Test
@Order(3)
public void streamSetTest() {
    Set<Integer> set = new HashSet<>();
    set.add(1);
    set.add(1); //Set does not allow duplicate elements.
    set.add(4);
    set.add(3);
    set.add(2);

    set.stream()
        .sorted()
        .forEach(System.out::println);
}

Output

Set as a stream source in java

Array as a Stream Source

We can use Arrays also a stream source. The example below shows how can we use arrays as a stream source.

@Test
@Order(4)
public void streamArrayTest() {
    int[] array = { 1, 4, 3, 2 };

    Arrays.stream(array)
        .sorted()
        .forEach(System.out::println);
}

Output

Array as a stream source in java

Map as a Stream Source

We can use Map a stream source and in the example below, you can find three different implementations. We can use maps as a source with entrySet(), ketSet(), and values() methods.

@Test
    @Order(5)
    public void streamMapTest() {
        Map<String, Integer> map = new HashMap<>();
        map.put("Ronaldo", 36);
        map.put("Messi", 33);
        map.put("Ozil", 32);
        map.put("Xavi", 41);

        System.out.println("--EntrySet Test--");
        map.entrySet()
            .stream()
            .filter(footballer -> footballer.getValue() > 32)
            .forEach(System.out::println);

        System.out.println("\n--KeySet Test--");
        map.keySet()
            .stream()
            .filter(footballerName -> footballerName.contains("z"))
            .forEach(System.out::println);

        System.out.println("\n--Values Test--");
        map.values()
            .stream()
            .filter(footballerAge -> footballerAge > 33)
            .forEach(System.out::println);
    }

Output

Map as a stream source in java

GitHub Project

StreamSources.java

In this article, I shared different stream source examples with examples. Hope to see you in the next articles.

Thanks for reading,
Onur Baskirt

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.