Values added to the list is based on the index position and it is ordered by index position. Duplicates are allowed.
Types of Lists are:
Array List:
Example:
public class Fruits {
public static void main(String[] args) {
ArrayList < String > names = new ArrayList < String > ();
names.add(“apple”);
names.add(“cherry”);
names.add(“kiwi”);
names.add(“banana”);
names.add(“cherry”);
System.out.println(names);
}
}
Output:
[Apple, cherry, kiwi, banana, cherry]
From the output, Array List maintains the insertion order and it accepts the duplicates. But not sorted.
Vector:
It is same as Array List.
Example:
public class Fruit {
public static void main(String[] args) {
Vector < String > names = new Vector < String > ();
names.add(“cherry”);
names.add(“apple”);
names.add(“banana”);
names.add(“kiwi”);
names.add(“apple”);
System.out.println(“names”);
}
}
Output:
[cherry,apple,banana,kiwi,apple]
Vector also maintains the insertion order and accepts the duplicates.
Linked List:
Example:
Output
[ banana,cherry,apple,kiwi,banana]
Maintains the insertion order and accepts the duplicates.
We covered nearly 50 + primary Java interview questions in this tutorial for fresh and experienced candidates. Q.1- What is… Read More