Collections

Collection Framework:

what are Collections.?
-It is a framework which provides the architecture to store and manipulate the group of objects.
- The collection is a container in which group of elements are combined together to form a single       unit.

Collection in Java classified into 3 categories:

  1. Interface: It is an abstract data-type(ADT) which represents the collection.
  2. Implementation: It is a concrete implementation of the interface.
  3. Algorithms: It contains methods which perform useful computations like searching and sorting.
The following ways collections are classified:


Two interface trees, one starting with Collection and including Set, SortedSet, List, and Queue, and the other starting with Map and including SortedMap.


  1. Set: It is special kind of interface which does not allow duplicates.
  2. List: It allows duplicates and maintains the order
  3. Map: It is key, value pair.

SET in Detail : (java.util )

  • It is special kind of interface which does not allow duplicates.
  • Set is an interface which extends Collection. 
  • It is an unordered collection of objects in which duplicate values cannot be stored.
Syntax: Set <Object> someName= new HashSet<Object>();
Example: Set <String> s= new HashSet<String>();


METHODS: 

boolean add(Object o)--->It is used to add the specified element to this set if it is not already present.

Eg: s.add("Nihal");
       s.add("Kurre"); 

boolean remove(Object o) ---> It is used to remove the specified element from this set if it is present.
Eg: s.remove("Nihal");

void clear()---> It is used to remove all of the elements from this set.
Eg: s.clear();

int size()
---> It is used to return the number of elements in this set.
Eg: s.size();


boolean contains(Object o)---> It is used to return true if this set contains the specified element.
Eg: s.contains("Nihal");

boolean isEmpty()---> It is used to return true if this set contains no elements.
Eg: s.isEmpty();

Object clone()---> It is used to return a shallow copy of this HashSet instance: the elements themselves are not cloned.

Iterator iterator()---> It is used to return an iterator over the elements in this set.



Set Implementations


HashSet:  Does not sort.can contain null values.

LinkedHashSet:  Takes the insertion order. It can have Null values

TreeSet: It is sorted and null value depends

ConcurrentSkip List set: It is sorted and weakly consistent. It does not have null values.




List In Detail : 

  • It is special kind of interface which does not allow duplicates.
  • Set is an interface which extends Collection. 
  • It is an unordered collection of objects in which duplicate values cannot be stored.
Syntax: List<Object> someName= new HashTable<Object>();
Example: List<String> l= new HashTable<String>();

METHODS: 

void add(int index,Object element)--->It is used to insert an element into the invoking list at the index passed in the index.
Eg: l.add("Nihal");

boolean addAll(int index,Collection c)--->It is used to insert all elements of c into the invoking list at the index passed in the index.


object get(int index)--->It is used to return the object stored at the specified index within the invoking collection.

object set(int index,Object element)---> It is used to assign an element to the location specified by index within the invoking list.

object remove(int index)---> It is used to remove the element at position index from the invoking list and return the deleted element.

ListIterator listIterator()---> It is used to return an iterator to the start of the invoking list.
ListIterator listIterator(int index)---> It is used to return an iterator to the invoking list that begins at the specified index
.Related image



Map In Detail: 


  • The java.util.Map interface represents a mapping between a key and a value.
  • A Map cannot contain duplicate keys and each key can map to at most one value.
  •  Some implementations allow null key and the null value (HashMap and LinkedHashMap) but some do not (TreeMap).
Why Map: 

A map of zip codes and cities.
A map of managers and employees. Each manager (key) is associated with a list of deatils of employees (value) he manages.
A map of classes and students. Each class (key) is associated with a list of students values.


Best: Map is best for searching.
It implements a serializable and clonable interface.

Syntax: Map<Object, Object>someName= new HashMap<Object, Object>();
Eg: Map<Integer, Integer>map= new HashMap<Integer, Integer>();

METHODS: 

Object put(Object key, Object value)--->It is used to insert an entry in this map.

void putAll(Map map)---> It is used to insert the specified map in this map.

Object remove(Object key)---> It is used to delete an entry for the specified key.

Object get(Object key)---> It is used to return the value for the specified key.

boolean containsKey(Object key)---> It is used to search the specified key from this map.

Set keySet()---> It is used to return the Set view containing all the keys.

Set entrySet()---> It is used to return the Set view containing all the keys and values.



Image result for map implementations



No comments:

Post a Comment