How to concatenate lists in java?

Java 8Object Oriented ProgrammingProgramming

The addAll(Collection<? extends E> c) method of the class java.util.ArrayList appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. Using this method you can concatenate two lists.

Example:

import java.util.ArrayList;

public class Sample {
   public static void main(String[] args) {
      ArrayList<String> list = new ArrayList<String>();
      list.add("JavaFx");
      list.add("Java");
      list.add("WebGL");
      list.add("OpenCV");
      System.out.println(list);
      ArrayList<String> newList = new ArrayList<String>();
      list.add("HBase");
      list.add("Neo4j");
      list.add("MangoDB");
      list.add("Cassandra");
      list.addAll(newList);
      System.out.println(list);
   }
}

Output:

[JavaFx, Java, WebGL, OpenCV]
[JavaFx, Java, WebGL, OpenCV, HBase, Neo4j, MangoDB, Cassandra]
raja
Published on 17-Jan-2018 05:21:28
Advertisements