Java Programming Tutorials

Java programming tutorials with many code examples!

Java List types and their methods

Problem:

What are Java List types? What are their methods and what are their properties?

Solution:

Here are java.util.List implementations and their properties:

  1. ArrayList: fast iteration, random access
  2. LinkedList: insertion order, fast insertion/deletion
  3. Vector: fast iteration, random access, synchronized

Methods common for List implementations:

  • boolean add(E): adds element to the list
  • boolean add(int index, E): insert element E to the specified position
  • boolean contains(Object): checks whether the list contains specified element
  • E get(int index): returns an element from the specified position on list
  • int indexOf(Object): returns index of first occurrence of given element or -1 if not found
  • Iterator<E> iterator(): returns list iterator in correct order
  • E remove(int index): removes element from specified position on list and returns it
  • boolean remove(Object): removes the first occurrence of given object
  • int size(): returns a number of elements in the list
  • Object[] toArray(): returns an array containing all list’s elements in proper order (from the first to the last)
  • <T> T[] toArray(T[]): returns an array containing all list’s elements in proper order, with type T equal the formal type parameter.

It’s important to know the implementations and these methods if you want to take Oracle Certified Programmer exam.

Share with the World!