collection and collections in java code example

Example 1: collection vs collections

Collection is an interface in Java.
But Collections is a class in Java.
Collection is a base interface. 
Collections is a utility class in Java.
Collection defines methods that are
used for data structures that contain the objects. 
Collections define the methods that are used 
for operations like access, find, etc. on a Collection.
Collection Interface has only abstract
methods but Collections consists of 
only static methods that are used to
operate on objects of type Collection.

Example 2: collections in java

LIST: Can store duplicate values,
      Keeps the insertion order. 
      It allows multiple null values, 
      Also we can read a certain value by index.
- ArrayList not syncronized, array based class 
- LinkedList not synchronized, doubly linked
- Vector is synchronized, thread safe

SET: Can only store unique values, 
     And does not maintain order
- HashSet can have null, order is not guaranteed
- LinkedHashSet can have null and keeps the order 
- TreeSet sorts the order and don't accept null 

QUQUE : Accepts duplicates, 
        Doesn't have index num,
        First in first our order.  
       
MAP : is a (key-value format) 
      and keys are always unique, 
      and value can be duplicated. 
- HashTable don't have null key, sychronized(thread-safe)
- LinkedHashMap can have null key, keeps order
- HasHMap can have null key, order is not guaranteed
- TreeMap doesn't have null key and keys are sorted

Example 3: how to define a collection in java

List<String> list = Arrays.asList("Lars", "Simon");

Tags:

Java Example