Collections
Collections
DEVELOPMENT PROGRAM
Java SE Collection
OUTLINE
• Collection
• Map
• Ordering
• Java 8
HIERARCHY
COLLECTION
• Group of objects
• T()
• T(Collection c)
fi
COLLECTION
LIST
• Can contain duplicate elements
• Homogeneous
[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
• offer()
• peek()
• poll()
fi
fi
QUEUE IMPLEMENTATION
• LinkedList
• PriorityQueue
SET
• LinkedHashSet
•
OUTLINE
• Collection
• Map
• Ordering
• Java 8
HIERARCHY
MAP
• Address comparison
• Equals:
• 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.
• 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
• Collection
• Map
• Ordering
• Java 8
OBJECT ODERRING
• How to sort collections
• Bubble sort
• Selection sort
• Quick sort
• Merge sort
• Comparable<T> interface
• Comparator<T> interface
COMPARABLE INTERFACE
• <0, if o1 precedes 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
• Syntax
• Methods:
• accept()
• andThen()
SUPPLIER INTERFACE
• 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?