Explain about the different lists available in the collection.
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:
- Fast iteration and fast Random Access.
- It is an ordered collection (by index) and not sorted.
- It implements Random Access Interface.
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.
- Vector methods are synchronized.
- Thread safety.
- It also implements the Random Access.
- Thread safety usually causes a performance hit.