JAVA FULL STACK
DEVELOPMENT PROGRAM
      Java SE Collection
                 OUTLINE
•   Collection
•   Map
•   Ordering
•   Java 8
HIERARCHY
                           COLLECTION
•   Group of objects
•   It is not speci ed whether they are
     •   Ordered / not ordered
     •   Duplicated / not duplicated
•   Following constructors are common to all classes implementing Collection
     •   T()
     •   T(Collection c)
               fi
COLLECTION
                        LIST
•   Can contain duplicate elements
•   Homogeneous
•   Insertion order is preserved
•   User can de ne insertion point
•   Elements can be accessed by position
            fi
LIST ADDITIONAL METHOD
LIST IMPLEMENTATION
[1,2,3,4,5]   1->2->3->4->5
[1,3,4,5]     1->3->4->5
                                    QUEUE
•    Collection whose elements have an order
     •   FIFO: First In First Out
•    De nes a head position where is the rst element that can be
     accessed
     •   offer()
     •   peek()
     •   poll()
fi
                                      fi
     QUEUE IMPLEMENTATION
•   LinkedList
•   PriorityQueue
                         SET
•   Contains no methods other than those inherited
    from Collection
•   add() has restriction that no duplicate elements
    are allowed
                  SET IMPLEMENTATION
•   HashSet
      •   Insertion order is not preserved
•   LinkedHashSet
      •   Insertion order is preserved
                                    •
                 OUTLINE
•   Collection
•   Map
•   Ordering
•   Java 8
HIERARCHY
                        MAP
•   An object that associates keys to values
•   Keys and values must be objects(Wrapper Class)
•   Keys must be unique
•   Only one value per key
MAP INTERFACE
MAP
                  EQUALS(). ==
•   ==
      •   Address comparison
      •   If point to the same memory location
•   Equals:
      •   Values in the object
                              EQUALS()
•   It is re exive: x.equals(x) == true
•   It is symmetric: x.equals(y) == y.equals(x)
•   It is transitive: for any reference values x, y, and z, if x.equals(y) == true
    AND y.equals(z) == true => x.equals(z) == true
•   It is consistent: for any reference values x and y, multiple invocations of
    x.equals(y) consistently return true (or false), provided that no
    information used in equals comparisons on the object is modi ed.
•   x.equals(null) == false
     fl
                                                                   fi
            HASHCODE()
•   Return Type: int
•   Object.hashCode()
                         HASHCODE()
•   The hashCode() method must consistently return the same int, if information
    used in equals() to compare the objects is not modi ed.
•   The hashCode contract in Java 8:
     •   If two objects are equal for equals() method, then calling the hashCode()
         method on the two objects must produce the same integer result.
     •   If two objects are unequal for equals() method, then calling the hashCode()
         method on the two objects MAY produce distinct integer results.
     •   Producing distinct int results for unequal objects may improve the
         performance of hashmap
                                                    fi
                         MAP
•   Get/set takes constant time (in case of no collisions)
•   Implementations
    •   HashMap implements Map
    •   LinkedHashMap extends HashMap
    •   TreeMap implements SortedMap
SORTED MAP
                 OUTLINE
•   Collection
•   Map
•   Ordering
•   Java 8
              OBJECT ODERRING
•   How to sort collections
    •   Bubble sort
    •   Selection sort
    •   Quick sort
    •   Merge sort
•   Take the advantage of Collection
                 COLLECTIONS
•   Static methods of java.util.Collections class
    •   sort() - Tim sort, n log(n)
    •   binarySearch() – requires ordered sequence
    •   reverse() - requires ordered sequence
    •   min(), max() – in a Collection
    DEFAULT IMPLEMENTATION
•   The interface is implemented by language common
    types in packages java.lang and java.util
    •   String objects are lexicographically ordered
    •   Date objects are chronologically ordered
    •   Number and sub-classes are ordered numerically
         CUSTOM ORDERING
•   Comparable<T> interface
•   Comparator<T> interface
         COMPARABLE INTERFACE
•   Compares the receiving object with the speci ed object
•   Return value must be:
     •   <0, if this precedes obj
     •   ==0, if this has the same order as obj
     •   >0, if this follows obj
                                              fi
        COMPARATOR INTERFACE
•   Compares its two arguments
•   Return value must be
    •   <0, if o1 precedes o2
    •   ==0, if o1 has the same ordering as o2
    •   >0, if o1 follows o2
                 OUTLINE
•   Collection
•   Map
•   Ordering
•   Java 8
        JAVA 8 NEW FEATURES
•   Lambda Expression
•   Functional Interface
               LAMBDA EXPRESSION
•   Simpli es development
•   Before Java 8, there is anonymous class
•   Syntax
    •        parameter -> expression
        fi
FUNCTIONAL INTERFACES
        FUNCTIONAL INTERFACE
•   Interface that contains ONLY ONE abstract
    method
    •   Comparator is a functional interface
            LAMBDA EXPRESSION
                    +
           FUNCTIONAL INTERFACE
•   Lambda Expression and Functional Interface works
    together to simplify code
     CONSUMER INTERFACE
• Consumer is a functional interface which
  represents an operation that accepts a single
  input argument and returns no result
• Methods:
      • accept()
      • andThen()
       SUPPLIER INTERFACE
• Supplier is a functional interface which
  represents an operation that takes no argument
  and returns a result
• Methods:
      • get()
       FUNCTION INTERFACE
• Function is a functional interface which represents an
  operation that takes an argument and returns a result
• The argument and result can be different types
•   Methods:
         • apply()
         • andThen()
         • compose()
         • identity()
     PREDICATE INTERFACE
• Predicate is a functional interface which
  represents an operation that takes an argument
  and returns a boolean result
• Methods:
      • test()
      • and()
      • or()
      • negate()
QUESTIONS?