Can you add a list to a list Java?

Initializing a List in Java

The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.

List is an interface, and the instances of List can be created in the following ways:

List a = new ArrayList[]; List b = new LinkedList[]; List c = new Vector[]; List d = new Stack[];

Below are the following ways to initialize a list:

  1. Using List.add[] method

    Since list is an interface, one cant directly instantiate it. However, one can create objects of those classes which have implemented this interface and instantiate them.



    Few classes which have implemented the List interface are Stack, ArrayList, LinkedList, Vector etc.

    Syntax:

    List list=new ArrayList[]; List llist=new LinkedList[]; List stack=new Stack[];

    Examples:




    import java.util.*;
    import java.util.function.Supplier;
    public class GFG {
    public static void main[String args[]]
    {
    // For ArrayList
    List list = new ArrayList[];
    list.add[1];
    list.add[3];
    System.out.println["ArrayList : " + list.toString[]];
    // For LinkedList
    List llist = new LinkedList[];
    llist.add[2];
    llist.add[4];
    System.out.println["LinkedList : " + llist.toString[]];
    // For Stack
    List stack = new Stack[];
    stack.add[3];
    stack.add[1];
    System.out.println["Stack : " + stack.toString[]];
    }
    }
    Output: ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]

    Double Brace Initialization can also be used to do the above work.

    Syntax:

    List list=new ArrayList[]{{ add[1]; add[2]; add[3]; }};

    Examples:




    import java.util.*;
    public class GFG {
    public static void main[String args[]]
    {
    // For ArrayList
    List list = new ArrayList[] {{
    add[1];
    add[3];
    } };
    System.out.println["ArrayList : " + list.toString[]];
    // For LinkedList
    List llist = new LinkedList[] {{
    add[2];
    add[4];
    } };
    System.out.println["LinkedList : " + llist.toString[]];
    // For Stack
    List stack = new Stack[] {{
    add[3];
    add[1];
    } };
    System.out.println["Stack : " + stack.toString[]];
    }
    }
    Output:

    ArrayList : [1, 3] LinkedList : [2, 4] Stack : [3, 1]
  2. Using Arrays.asList[]

    • Creating Immutable List

      Arrays.asList[] creates an immutable list from an array. Hence it can be used to instantiate a list with an array.

      Syntax:

      List list=Arrays.asList[1, 2, 3];

      Examples:




      import java.util.Arrays;
      import java.util.List;
      public class GFG {
      public static void main[String args[]]
      {
      // Instantiating List using Arrays.asList[]
      List list = Arrays.asList[1, 2, 3];
      // Print the list
      System.out.println["List : " + list.toString[]];
      }
      }
      Output: List : [1, 2, 3]
    • Creating Mutable List

      Syntax:

      List list=new ArrayList[Arrays.asList[1, 2, 3]];

      Examples:




      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      public class GFG {
      public static void main[String args[]]
      {
      // Creating a mutable list using Arrays.asList[]
      List list = new ArrayList[
      Arrays.asList[1, 2, 3]];
      // Print the list
      System.out.println["List : " + list.toString[]];
      list.add[5];
      // Print the list
      System.out.println["Modified list : " + list.toString[]];
      }
      }
      Output: List : [1, 2, 3] Modified list : [1, 2, 3, 5]
  3. Using Collections class methods

    There are various methods in Collections class that can be used to instantiate a list. They are:

    1. Using Collections.addAll[]

      Collections class has a static method addAll[] which can be used to initialize a list. Collections.addAll[] take in any number of elements after it is specified with the Collection in which the elements are to be inserted.

      Syntax:



      List list = Collections.EMPTY_LIST; Collections.addAll[list = new ArrayList[], 1, 2, 3, 4];

      Examples:




      import java.util.*;
      public class GFG {
      public static void main[String args[]]
      {
      // Create an empty list
      List list = new ArrayList[];
      // Instantiating list using Collections.addAll[]
      Collections.addAll[list, 1, 2, 3, 4];
      // Print the list
      System.out.println["List : " + list.toString[]];
      }
      }
      Output: List : [1, 2, 3, 4]
    2. Using Collections.unmodifiableList[]

      Collections.unmodifiableList[] returns a list which cant be altered i.e. it can neither add or delete an element. Any attempt to modify the list will result in an UnsupportedOperationExample.

      Syntax:

      List list = Collections .unmodifiableList[Arrays.asList[1, 2, 3]];

      Example 1:




      import java.util.*;
      public class GFG {
      public static void main[String args[]]
      {
      // Creating the list
      List list = Collections.unmodifiableList[
      Arrays.asList[1, 2, 3]];
      // Print the list
      System.out.println["List : " + list.toString[]];
      }
      }
      Output: List : [1, 2, 3]

      Example 2:




      import java.util.*;
      public class GFG {
      public static void main[String args[]]
      {
      try {
      // Creating the list
      List list = Collections.unmodifiableList[
      Arrays.asList[1, 2, 3]];
      // Print the list
      System.out.println["List : " + list.toString[]];
      // Trying to modify the list
      System.out.println["Trying to modify the list"];
      list.set[0, list.get[0]];
      }
      catch [Exception e] {
      System.out.println["Exception : " + e];
      }
      }
      }
      Output: List : [1, 2, 3] Trying to modify the list Exception : java.lang.UnsupportedOperationException
    3. Using Collections.singletonList[]

      Collections.singletonList[] returns an immutable list consisting of one element only.

      Syntax:



      List list = Collections.singletonList[2];

      Example 1:




      import java.util.*;
      public class GFG {
      public static void main[String args[]]
      {
      // Creating the list
      List list = Collections.singletonList[2];
      // Print the list
      System.out.println["List : " + list.toString[]];
      }
      }
      Output: List : [2]
  4. Using Java 8 Stream

    With the introduction of Stream and functional programming in Java 8, now one can construct any stream of objects and then collect them as a list.

    Syntax:

    1. List list = Stream.of[1, 2, 3] .collect[Collectors.toList[]]; 2. List list = Stream.of[1, 2, 3] .collect[Collectors.toCollection[ArrayList::new]]; 3. List list = Stream.of[1, 2, 3, 4] .collect[Collectors.collectingAndThen[Collectors.toList[], Collections::unmodifiableList]];

    Examples:




    import java.util.*;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    public class GFG {
    public static void main[String args[]]
    {
    // Creating a List using Syntax 1
    List list1 = Stream.of[1, 2, 3]
    .collect[Collectors.toList[]];
    // Printing the list
    System.out.println["List using Syntax 1: "
    + list1.toString[]];
    // Creating a List using Syntax 2
    List list2 = Stream
    .of[3, 2, 1]
    .collect[
    Collectors
    .toCollection[ArrayList::new]];
    // Printing the list
    System.out.println["List using Syntax 2: "
    + list2.toString[]];
    // Creating a List using Syntax 3
    List list3 = Stream
    .of[1, 2, 3, 4]
    .collect[
    Collectors
    .collectingAndThen[
    Collectors.toList[],
    Collections::unmodifiableList]];
    // Printing the list
    System.out.println["List using Syntax 3: "
    + list3.toString[]];
    }
    }
    Output: List using Syntax 1: [1, 2, 3] List using Syntax 2: [3, 2, 1] List using Syntax 3: [1, 2, 3, 4]
  5. Using Java 9 List.of[]

    Java 9 introduced List.of[] method which takes in any number of arguments and constructs a compact and unmodifiable list out of them.

    Syntax:

    List unmodifiableList = List.of[1, 2, 3];

    Examples:




    import java.util.List;
    public class GFG {
    public static void main[String args[]]
    {
    // Creating a list using List.of[]
    List unmodifiableList = List.of[1, 2, 3];
    // Printing the List
    System.out.println["List : "
    + unmodifiableList.toString[]];
    }
    }

    OUTPUT:

    [1, 2, 3]

Attention reader! Dont stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.




Article Tags :
Java
Java Programs
Technical Scripter
Java - util package
Java 8
java-basics
java-list
Java-List-Programs
Practice Tags :
Java
Read Full Article

Video liên quan

Chủ Đề