Discussion

Sets And Maps

Learning objectives

  • To store unordered,non duplicate element using set
  • Explore HashSet,LinkedHashSet,TreeSet
  • Understand Map

Sets

  • Sets are collections of distinct elements. There are no duplicates. It allows null elements
  • Set is an interface.It is abstract data type
  • You can create a set using one of its three concrete classes
  • HashSet, LinkedHashSet, or TreeSet
  • A set is an unordered collection
  • Github code Sets

                            

List VS Set

List Set
Positional Order Allow positional access to elements using get() Does not allow positional access method()
Duplicate objects Allow duplicate elements Does not allow duplicate elements
Insertion Order Maintain Insertion order Does not maintain insertion order

HashSet

  • Initial capacity will increase based on data default capacity is 16
  • Does not allow duplicate elements
  • Allow only one null values
  • It is not order
  • Elements are stored basis of hashcode
  • Best choice to perform search operation
  • Github code HashSet

                            

LinkedHashSet

  • LinkedHashSet class implements Set interface and extends the HashSet class
  • Does not allow duplicate values
  • Allow only one null object
  • It is Ordered collection
  • It's not Synchronized multiple thread can access concurrently.Modification made by one thread will impact other thread
  • Github code LinkedHashSet

                            

Treeset

  • Stores unique values
  • Maintain elements in ascending order
  • Elements are stored in sorted order
  • It's not Synchronized multiple thread can access concurrently.Modification made by one thread will impact other thread
  • Only allow object of the same type
  • Allow null values till java 6
  • Github code Treeset

                            

Hashset VS LinkedHashSet VS TreeSet

Hashset LinkedHashSet TreeSet
Working Uses Hashmap for storing objects uses LinkedHashSet for storing objects Uses TreeMap for storing objects
Usage Does not maintain insertion order/store unique objects Maintain insertion order/store unique objects Stores elements in asc or desc order/store unique objects
Order Does not maintain insertion order Maintain insertion order Stores elements in asc order
Null Value Allow only one null element Allow one null object Won't allow null object.(Allow 1 null value till java 6)

Map

  • A map stores association between keys and values.
  • Each key links to exactly on value from a key value pair known as entry.
  • Retrieving values by key is fast
  • Does not extend Collection interface
  • MAP Key (K),Value(v)
  • Github code Map

                            

Map Methods

  • Map is similar to dictionary. It has word and at least one definition.
  • V put(K key,V value)
  • Creates a new map entry for the provided key-value pair, if it does not exist
  • If key exists the old value is overwritten
  • Returns null if the map does not contain key
  • V get(Object key)
  • Returns null if the map does not contain key
  • Removes the map entry fo the provided key
  • V remove(Object key)
  • Github code Map

                            

HashMap

  • Hash table based implementation of the Map interface
  • This will not preserve the order in which these data were inserted.
  • Store items in "key/value" pairs
  • Github code HashMap

                            

LinkedHashMap

  • Java LinkedHashMap contains values based on the key.
  • Java LinkedHashMap contains unique elements.
  • Java LinkedHashMap maintains insertion order.
  • K: It is the type of keys maintained by this map.
  • V: It is the type of mapped values.
  • Github code LinkedHashMap

                            

TreeMap

  • It provides an efficient means of storing key-value pairs in sorted order.
  • Java TreeMap cannot have a null key but can have multiple null values.
  • Java TreeMap maintains ascending order.
  • K: It is the type of keys maintained by this map.
  • V: It is the type of mapped values.
  • Github code TreeMap

                            

Java Docs

  • Java Docs Guidelines
  • Sample code
  •                         
                            part 1 : description 
                            part 2 : tag list
                            How to run javadoc
                            cmd line: javadoc -d doc src* or javadoc -d doc src* -author -version
                            From Intellij = Tools => Generate javaDocs => select Whole project => ok
                            
                            
Group Project Task 4

Thank you