How to make a new List in Java

0 votes

We create a Set as:

Set myset = new HashSet()

How do we create a List in Java?

Dec 28, 2020 in Java by anonymous
• 8,910 points
97 views

2 answers to this question.

0 votes
List myList = new ArrayList();

or with generics (Java 7 or later)

List<MyType> myList = new ArrayList<>();

or with generics (Old java versions)

List<MyType> myList = new ArrayList<MyType>();
answered Dec 28, 2020 by Gitika
• 65,970 points
0 votes

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(); 

Examples:

filter_none

edit

play_arrow

brightness_4

import java.util.*;

import java.util.function.Supplier;

  

public class GFG {

    public static void main(String args[])

    {

  

        // For ArrayList

        List<Integer> list = new ArrayList<Integer>();

        list.add(1);

        list.add(3);

        System.out.println("ArrayList : " + list.toString());

  

        // For LinkedList

        List<Integer> llist = new LinkedList<Integer>();

        llist.add(2);

        llist.add(4);

        System.out.println("LinkedList : " + llist.toString());

  

        // For Stack

        List<Integer> stack = new Stack<Integer>();

        stack.add(3);

        stack.add(1);

        System.out.println("Stack : " + stack.toString());

    }

}

Output:

ArrayList : [1, 3]
LinkedList : [2, 4]
Stack : [3, 1]
answered Dec 28, 2020 by Thomas Walenta

Related Questions In Java

+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,170 points
295 views
0 votes
1 answer

How to get a platform-dependent new line character in Java?

If you are using Java 1.5 or ...READ MORE

answered Jul 3, 2018 in Java by Akrati
• 3,170 points
243 views
0 votes
1 answer

How do I make a list with checkboxes in Java Swing?

Hii @kartik, Create a custom ListCellRenderer and asign it to ...READ MORE

answered May 8, 2020 in Java by Niroj
• 82,840 points
959 views
0 votes
1 answer

How to convert a Set to List in Java?

Hi@MD, The most straightforward way to convert a ...READ MORE

answered Dec 30, 2020 in Java by akhtar
• 38,210 points
109 views
0 votes
1 answer

what are the ways in which a list can be iterated

  There are 5 ways to iterate over ...READ MORE

answered Apr 23, 2018 in Java by sharth
• 3,350 points
319 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
2,137 views
0 votes
2 answers

Performing Iteration over each entry in a 'Map'

In Java 8 you can do it ...READ MORE

answered Oct 24, 2018 in Java by Sushmita
• 6,900 points
274 views
0 votes
2 answers
0 votes
1 answer

How to round a number to n decimal places in Java?

Use setRoundingMode, set the RoundingMode explicitly to handle your issue ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,970 points
492 views
0 votes
3 answers

How to check if a String is numeric in Java?

Check if a string is numeric public class ...READ MORE

answered Dec 29, 2020 in Java by Carlos
191 views