This question has already been answered:

from Collection collection = new ArrayList ();

Reported as a duplicate by participants Alexey Shimansky , Community Spirit ♦ October 31 '16 at 6:38 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 1ArrayList ... ArrayList (); 2Collection ... ArrayList (); - Anton Sorokin
  • mmm .... that is, do not take generics into account? - Alexey Shimansky
  • Yes, I meant what is the difference in writing at the beginning of arrayList or Collection - Anton Sorokin

1 answer 1

Comparing

ArrayList ArList = new ArrayList(); 

and

 Collection<String> collection = new ArrayList<String>(); 

we will find small differences:

  • new ArrayList() is the same as new ArrayList<Object>()
  • For ArList, the reference type will be ArrayList.
  • And for collection, the type of reference will be the Collection interface, from which all collections are created: lists, associative arrays, sets.

In more detail you learn from the documentation .

  • Those. using a collection just gives us more methods? - Anton Sorokin
  • one
    It gives only methods common to all collections, and those specific (for a list, arrays, etc.) will not be available for reference. If you need to call the list methods, you will have to cast collection to the ArrayList type - gil9red