Home  >  JDK 8

Java 8 Concat Streams, Lists, Sets, Arrays Example

By Arvind Rai, September 06, 2017
On this page we will provide Java 8 concat Streams, Lists, Sets, Arrays example. Stream provides concat() method to concatenate two streams and will return a stream. To concatenate Lists, Sets and Arrays we will convert them into stream first and using concat() we will combine them. The output stream can be converted into List, Set etc using methods of Collectors such as toList(), toSet() etc.
Now find the syntax of Stream.concat() from Java doc.
static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) 
concat() creates lazily concatenated stream. This stream contains all elements of first stream followed by all elements of second stream. The resulting stream will be ordered if both input streams are ordered. The resulting stream will be parallel if any of the two streams is parallel.
Now find the examples to use concat().

Concat Streams

Here we will concatenate two stream instances using concat().
ConcatStreamsDemo.java
package com.concretepage;
import java.util.stream.Stream;
public class ConcatStreamsDemo {
    public static void main(String[] args) {
        Stream<String> s1 = Stream.of("AA", "BB", "CC");
        Stream<String> s2 = Stream.of("AA", "BB", "DD");
        Stream<String> s = Stream.concat(s1, s2);
        s.forEach(e->System.out.print(e+" "));

        //Remove duplicates using distinct()
        s1 = Stream.of("AA", "BB", "CC");
        s2 = Stream.of("AA", "BB", "DD");
        System.out.println("\nRemove duplicates using distinct()");
        s = Stream.concat(s1, s2).distinct();
        s.forEach(e->System.out.print(e+" "));
    }
}
Output
AA BB CC AA BB DD 
Remove duplicates using distinct()
AA BB CC DD  

Concat Lists

In this example we have two lists. These lists will be converted into streams then they will be concatenated using concat() method. The output stream will be converted into List using Collectors.toList(). To get distinct element, we need to use Stream.distinct() .
ConcatListsDemo.java
package com.concretepage;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ConcatListsDemo {
    public static void main(String[] args) {
        List<Book> list1 = new ArrayList<>();
        List<Book> list2 = new ArrayList<>();
        {
           list1.add(new Book("Core Java", 200));
           list1.add(new Book("Spring MVC", 300));
           list1.add(new Book("Learning Freemarker", 150));  
           
           list2.add(new Book("Core Java", 200));
           list2.add(new Book("Spring MVC", 300));
           list2.add(new Book("Learning Hibernate", 400));            
        }
        List<Book> list = Stream.concat(list1.stream(), list2.stream()).collect(Collectors.toList());
        list.forEach(b->System.out.println(b.getName()+", "+ b.getPrice()));
        
        //Remove duplicates using distinct()
        System.out.println("--Remove duplicates using distinct()--");
        list = Stream.concat(list1.stream(), list2.stream()).distinct().collect(Collectors.toList());
        list.forEach(b->System.out.println(b.getName()+", "+ b.getPrice()));        
    }
} 
Book.java
package com.concretepage;
public class Book {
    private String name;
    private int price;
    public Book(String name, int price) {
	this.name = name;
	this.price = price;
    }
    public String getName() {
	return name;
    }
    public int getPrice() {
	return price;
    }
    @Override
    public boolean equals(final Object obj) {
      if (obj == null) {
         return false;
      }
      final Book book = (Book) obj;
      if (this == book) {
         return true;
      } else {
         return (this.name.equals(book.name) && this.price == book.price);
      }
    }
    @Override
    public int hashCode() {
      int hashno = 7;
      hashno = 13 * hashno + (name == null ? 0 : name.hashCode());
      return hashno;
    }
}
Output
Core Java, 200
Spring MVC, 300
Learning Freemarker, 150
Core Java, 200
Spring MVC, 300
Learning Hibernate, 400
--Remove duplicates using distinct()--
Core Java, 200
Spring MVC, 300
Learning Freemarker, 150
Learning Hibernate, 400 

Concat Arrays

In this example we have two arrays. These arrays will be converted into streams then they will be concatenated using concat() method. The output stream will be converted into array using Stream.toArray(). To get distinct element, we need to use Stream.distinct().
ConcatArraysDemo.java
package com.concretepage;
import java.util.stream.Stream;
public class ConcatArraysDemo {
    public static void main(String[] args) {
        Book[] bk1 = new Book[3];
        Book[] bk2 = new Book[3];        
        {
           bk1[0] = new Book("Core Java", 200);
           bk1[1] = new Book("Spring MVC", 300);
           bk1[2] = new Book("Learning Freemarker", 150);        	
           bk2[0] = new Book("Core Java", 200);
           bk2[1] = new Book("Spring MVC", 300);
           bk2[2] = new Book("Learning Hibernate", 400);           
        }
        Book[] bks = (Book[]) Stream.concat(Stream.of(bk1), Stream.of(bk2)).toArray(b -> new Book[b]);
        for(Book b : bks) {
        	System.out.println(b.getName()+", "+ b.getPrice());
        }
		
        //Remove duplicates using distinct()
        System.out.println("--Remove duplicates using distinct()--");
        bks = (Book[]) Stream.concat(Stream.of(bk1), Stream.of(bk2)).distinct().toArray(b -> new Book[b]);
        for(Book b : bks) {
        	System.out.println(b.getName()+", "+ b.getPrice());
        }
    }
}
Output
Core Java, 200
Spring MVC, 300
Learning Freemarker, 150
Core Java, 200
Spring MVC, 300
Learning Hibernate, 400
--Remove duplicates using distinct()--
Core Java, 200
Spring MVC, 300
Learning Freemarker, 150
Learning Hibernate, 400 

Concat Sets

In this example we have two sets. These sets will be converted into streams then they will be concatenated using concat() method. The output stream will be converted into Set using Collectors.toSet().
ConcatSetsDemo.java
package com.concretepage;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ConcatSetsDemo {
    public static void main(String[] args) {
        Set<Book> set1 = new HashSet<>();
        Set<Book> set2 = new HashSet<>();
        {
           set1.add(new Book("Core Java", 200));
           set1.add(new Book("Spring MVC", 300));
           set1.add(new Book("Learning Freemarker", 150));  
           
           set2.add(new Book("Core Java", 200));
           set2.add(new Book("Spring MVC", 300));
           set2.add(new Book("Learning Hibernate", 400));           
        }
        Set<Book> set = Stream.concat(set1.stream(), set2.stream()).collect(Collectors.toSet());
        set.forEach(b->System.out.println(b.getName()+", "+ b.getPrice()));
    }
} 
Output
Spring MVC, 300
Learning Freemarker, 150
Learning Hibernate, 400
Core Java, 200 

Reference

Java Doc - Interface Stream

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS



Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us