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

Hope this helps!

Check out Java certification course to become certified expert.

Thanks!

answered Dec 28, 2020 by Gitika
• 65,890 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

0 votes
0 answers

How to make a new List in Java

We create a Set as: Set myset = new HashSet() How ...READ MORE

Apr 14, 2022 in Java by Rahul
• 3,380 points
360 views
+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,190 points
1,114 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,190 points
875 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,880 points
2,384 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,370 points
1,085 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
5,245 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,920 points
1,074 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,890 points

edited Jul 6, 2023 by Khan Sarfaraz 1,759 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
2,803 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP