0% found this document useful (0 votes)
54 views13 pages

Hash Set

The document provides an introduction to the HashSet class in Java. It discusses the Collections Framework and key collection interfaces like Collection, List, Set, and Queue. It explains that a Map is not a true collection. It also covers important methods of collection interfaces like add(), remove(), contains(), and iterator(). Finally, it discusses some common collection and map classes like HashSet, LinkedList, HashMap, and TreeMap. It notes that to properly use HashMap, both the equals() and hashCode() methods must be overridden together.

Uploaded by

dhanushsaikode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views13 pages

Hash Set

The document provides an introduction to the HashSet class in Java. It discusses the Collections Framework and key collection interfaces like Collection, List, Set, and Queue. It explains that a Map is not a true collection. It also covers important methods of collection interfaces like add(), remove(), contains(), and iterator(). Finally, it discusses some common collection and map classes like HashSet, LinkedList, HashMap, and TreeMap. It notes that to properly use HashMap, both the equals() and hashCode() methods must be overridden together.

Uploaded by

dhanushsaikode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

#LifeKoKaroLift

HashSet

1
Introduction to HashSet

● The Collections Framework


○ Note the difference
■ “collection” (English) as in “collection of objects”
■ “Collections Framework” - classes, interfaces, etc. to handle
collection of objects
■ “Collection Interface” - An interface that represents
functionality of a collection of objects
■ “Collections” class - for useful methods that operate on
collection
Introduction to HashSet

● The Collections Framework


○ Features of The Collections Framework
■ High performance and efficient (standard implementations,
interfaces)
■ Interoperability among different types of collections
■ Extending and adapting a collection is easy
● Partial implementations (AbstractList, AbstractSet,
AbstractQueue, etc.)
■ Special Purpose Implementations (Stack, ArrayDeque, etc.)
■ Algorithms (static methods like Collections.sort())
■ Iterators and Spliterators(a way to access an element)
■ Legacy classes (Vector, Properties, Dictionary, Hashtable) still work
Introduction to HashSet

● The Collection Interfaces (Collection and its important super/sub-


interfaces)
Note: Map is NOT Iterable
or a Collection in true sense.
Iterable Each entity in map is (Key, Value)
pair and not an object
Map
However, it DOESN’T mean that
Map is not part of Collections
Collection framework.

List Queue
Set
Introduction to HashSet

● The Collection Interfaces (Collection and its important super/sub-


interfaces) (Important methods) Iterator<E> iterator() - returns an
Iterator for a given collection.

Iterator has hasNext() and next()


methods to check if there is a next
Map Iterable<E> element. If yes, then return the next
element in the collection.
Also has remove() method to remove
last returned element
Collection<E> Anything Iterable can be used in
enhanced-for (for-each) loop

List<E> Queue<E>
Set<E>
Introduction to HashSet

● The Collection Interfaces (Collection and its important super/sub-


interfaces) (Important methods) Methods applicable to collection of
objects are declared here including
(but not limited to):
Iterable<E> - [ADD] add(E e)
Map
- [DELETE ALL] clear()
- [DELETE] remove(E e)
- [SEARCH] containsAll()and
Collection<E> contains()
- [Access] iterator()
And others. . . .

List<E> Set<E> Queue<E>


Introduction to HashSet

● The Collection Interfaces (Collection and its important super/sub-


interfaces) (Important methods)
Iterable<E>
Map
Collection<E>

List<E> Set<E> Queue<E>


Collection methods and Collection methods and other Collection methods and
methods specific to List. methods. . . Queue-methods. . .
- [ADD AT INDEX] add(int - [ADD] add(E e) - [ADD] add(E e),
index, E e) - [REMOVE] remove(Object e) offer(E e)
- [REMOVE FROM INDEX] - [REMOVE]
add(int index, E e) remove(), poll()
- [ACCESS] peek()
Introduction to HashSet

● The Collection Interfaces (Collection and its important super/sub-


interfaces) (Important methods)
Map<K,V>

A MAP IS NOT A TRUE COLLECTION so it has following methods


- [Add/Set Key, value] put(K key, V val)

- [Get value for key] get(K key)

- [Search] containsKey(K key), containsValue(V val)

- [Remove] remove(K key), remove(K key, V val)

A Map is not Iterable (doesn’t inherit Iterable interface), thus cannot be used directly in a for-each loop
BUT YOU CAN GET COLLECTION VIEW of a Map with methods like entrySet(), keySet(), etc
Introduction to HashSet

● The Collection Classes

Iterable<E>

Collection<E>

List<E> Set<E> Queue<E>

- LinkedList - HashSet Inherited by. . .


- ArrayList - LinkedHashset - Deque (interface)
- TreeSet (implements Implemented by. . .
SortedSet sub-interface of - LinkedList
Set interface) - ArrayDeque
- PriorityQueue
Introduction to HashSet

Map<K,V>

Map Classes

- HashMap

- LinkedHashMap

- TreeMap
hashCode(), equals(), HashMap
If we only override equals(), the code may still NOT work as intended
Because each object will have a separate hashCode() even if they have same
roll numbers
Input Hash Fn hashCode

(Mehul,1) => (Mehul,1) => 200


100 (Mehul,1) => 300

167 141 661 299


hashCode(), equals(), HashMap
If we override both equals() and hashCode(), so that hash value is nothing
but roll number of the student . . (Not a very good hash Function)

Input Hash Fn hashCode

(Mehul,1) =>
300

1 2 3 4
Thanks for
Listening!

You might also like