Collections in Java
Colletion Framework :
Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating groups of data as a single unit, a collectionIt provides a convenient API to many of the ADTs like maps, sets, lists, trees, arrays, hash tables, and other collections.
All the classes and interfaces of collection API are available in a java.util package.
Collections were added by the initial release of Java 2.
Collection Hierarchy :

Class Description :
java.util.ArrayList :- Java ArrayList class uses a dynamic array for storing the elements.
- It inherits AbstractList class and implements List interface.
- Java ArrayList class can contain duplicate elements.
- Java ArrayList class maintains insertion order.
- Java Vector is legacy class uses a dynamic array for storing the elements.
- It inherits AbstractList class and implements List interface.
- Java Vector class can contain duplicate elements.
- Java Vector class maintains insertion order.
- Java Vector class is synchronized.
- Java LinkedList class uses doubly linked list to store the elements.
- It inherits the AbstractList class and implements List and Deque interfaces.
- Java LinkedList class can contain duplicate elements.
- Java LinkedList class maintains insertion order.
- Java HashSet class is used to create a collection that uses a hash table for storage.
- It inherits the AbstractSet class and implements Set interface.
- HashSet stores the elements by using a mechanism called hashing.
- HashSet class does not maintains insertion order.
- TreeSet class implements the Set interface that uses a tree for storage.
- It inherits AbstractSet class and implements NavigableSet interface.
- Contains unique elements only like HashSet.
- TreeSet class stores elements in sorted order.
- LinkedHashSet class is a Hash table and Linked list implementation of the set interface.
- It inherits HashSet class and implements Set interface.
- Contains unique elements only like HashSet.
- LinkedHashSet class maintains insertion order.
Collection Methods :
boolean add(Object) : used to insert an element in the collection. boolean remove(Object) : used to remove an element from the collection. boolean isEmpty() : used to check whether collection is empty or not. int size() : used to get current size of the collection. boolean contains(Object) : used to search an element in collection. Object[] toArray() : used to converts collection into object type array.BULK OPERATIONS
boolean addAll(Collection) : used to insert the specified collection elements in the invoking collection. boolean removeAll() : used to delete all the elements of specified collection from the invoking collection. boolean retainAll() : used to delete all the elements of invoking collection except the specified collection. boolean containsAll() : used to converts collection into object type array. void clear() : removes the total no of element from the collection.
import java.util.*; class Test { public static void main(String[] a) { List<Integer> aob; aob = new ArrayList<>(); aob.add(22); aob.add(11); aob.add(44); aob.add(22); aob.add(33); aob.add(55); List<Integer> vob; vob = new Vector<>(); vob.addAll(aob); Set<Integer> hob; hob = new HashSet<>(); hob.addAll(aob); Set<Integer> tob; tob = new TreeSet<>(); tob.addAll(aob); System.out.println("ARR-LIST:"+aob); System.out.println("VECTOR :"+vob); System.out.println("HASHSET :"+hob); System.out.println("TREESET :"+tob); } }OUTPUT
ARR-LIST:[22, 11, 44, 22, 33, 55] VECTOR :[22, 11, 44, 22, 33, 55] HASHSET :[33, 22, 55, 11, 44] TREESET :[11, 22, 33, 44, 55]
Map Collection :
Java Map collection represent non-linear form of data. Map collection contains values on in format of key and value pair. Each key and value pair is known as an entry.It stores data in key-value pair.
A map cannot contain duplicate keys.
Each key canmap to at most one value.
Map Hierarchy :

Class Description :
java.util.HashTable :- A Hashtable is an array of list. Each list is known as a bucket.
- It contains only unique elements.
- It may have not have any null key or value.
- It is synchronized.
- A HashMap contains values based on the key.
- It contains only unique elements.
- It may have one null key and multiple null values.
- It maintains no order.
- A LinkedHashMap contains values based on the key.
- It may have one null key and multiple null values.
- It is same as HashMap instead maintains insertion order.
- A TreeMap contains values based on the key.
- It cannot have null key but can have multiple null values.
- It store data in key-wise ascending order.
Map Methods :
boolean put(Object K, Object V) : used to insert an entry(Key-Value) in the map. boolean remove(Object K) : used to remove an entry from the map. boolean isEmpty() : used to check whether map is empty or not. int size() : used to get current size of the map. boolean containsKey(Object K) : used to search a key in map. boolean containsValue(Object V) : used to search a value in map. Collection values() : returns the collection of values from map. Collection keySet() : returns the set of keys from map. Object V get(Object K) : returns the value of specific key. Set entrySet() : returns the set of map entries(key-value) in form of Map.Entry.BULK OPERATIONS
boolean putAll(Map) : used to insert the specified map elements in the invoking map object. void clear() : removes the total no of entries from the map.
import java.util.*; class Test { public static void main(String[] a) { Map m = new TreeMap(); m.put(1001,"Ajay"); m.put(1005,"Ayaan"); m.put(1003,"Bob"); m.put(1004,"Peter"); m.put(1002,"Tony"); Set<Map.Entry> s = m.entrySet(); for(Map.Entry e : s){ System.out.print("K : "+e.getKey()); System.out.print("V : "+e.getValue()); System.out.println("------------"); } } }OUTPUT
K : 1001 V : Ajay ------------- K : 1002 V : Tony ------------- K : 1003 V : Bob ------------- K : 1004 V : Peter ------------- K : 1005 V : Ayaan -------------
Next topic is utility-classes
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in