List in Collections of Java

Introduction:-

  • List is ordered collection of objects (Integer, String etc).
  • Duplicate values are allowed.
  • List preserve insertion order of elements i.e it is ordered by default.
  • List allows add, remove, get and set operations based on numerical positions of elements
  • List is a interface which extends collections interface.

Possible ways to access list are:-

List a = new ArrayList();
List b = new LinkedList();
List c = new Vector(); 
List d = new Stack();

Methods:-

  • void add(int index,Object O): adds given element at specified index.
  • boolean addAll(int index, Collection c):  adds all elements from specified collection to list.
  • Object remove(int index): removes an element from the specified index.
  • Object get(int index): returns element at the specified index.
  • Object set(int index, Object new): replaces element at given index with new element.

Did you notice?
Return type of remove, get and set function is object.

Leave a Reply

Your email address will not be published. Required fields are marked *