First, notice this in Java:

List<Integer> list1 = new ArrayList<>();
list1.getLast(); // no this method, Arraylist does not have this method

List<Integer> list2 = new LinkedList<>();
list2.getLast(); // no this method, though method exists in LinkedList

LinkedList<Integer> list3 = new LinkedList<>();
list3.getLast(); // method exists for list3
Copy the code

On top of all the questions of those comparisons, basic background of those names are needed:

  • In Java Collection, there are some data types to group variables for array is too limited in many situations.

  • Three main Interfaces:

    • Set: collection that doesn’t allow duplicates and also doesn’t allow accessing elements by index. Instead provides methods that check if element or elements exists.

      • EumSet: pecialized class to work with enum types
      • HashSet: keeps unordered list of elements (order is unpredictable). Implementation based on HashMap
      • LinkedHashSet: keeps ordered list of elements. Implementation based on LinkedHashMap
      • TreeSet
      • SortedSet
    • List: collection that allow duplicates and behave like array (index elements by integer) but is more flexible. First element has index = 0, last one has index = length-1

      • ArrayList: keeps unordered list of elements using array
      • LinkedList: keeps ordered list of elements using doubly-linked list
      • Vector: mostly the same as ArrayList but it is thread safe
    • Map: collection that allow duplicates and is similar to list except that index elements by key (key can be any object) Map can be assumed as associative array

      • HashMap: keeps unordered list of elements using array
      • LinkedHashMap: keeps ordered list of elements using doubly-linked list
      • TreeMap
      • Hashtable: keeps unordered list of elements as HashMap, but is synchronized. This class is obsolete.
      • EnumMap
      • Properties
  • Three subtypes of Collection:

    • Stack: Elements are added (pushed) and removed (popped) from top of collection. This principle is called LIFO (Last In, First Out)
    • Queue: lements are added (pushed) and removed (popped) in order they where added. This principle is called FIFO (First In, First Out)
    • Deque(pronounced “Deck”, Double Ended Queue): Elements are added and removed from both sides

So let’s get back to the original confusion:

List<Integer> list1 = new ArrayList<>();
list1.getLast(); // no this method, Arraylist does not have this method
Copy the code

here, list1 is a variable using List interface with ArrayList class implementation, it can only access List methods

List<Integer> list2 = new LinkedList<>();
list2.getLast(); // no this method, though method exists in LinkedList
Copy the code

here, list2 is also a variable using List interface but with LinkedList class implementation, List methods only.

LinkedList<Integer> list3 = new LinkedList<>();
list3.getLast(); // method exists for list3
Copy the code

now, list3 is simply a LinkedList variable, for .getLast() method only exists in LinkedList class, this is accessible.

References:

www.programiz.com/java-progra…

Szakuljarzuk.wordpress.com/2015/07/28/…