0% found this document useful (0 votes)
4 views

OOP1_Unit 11 (Amiraj) VisionPapers.in

Chapter 11 covers the concepts of sets and maps in Java, detailing the characteristics and implementations of different set types such as HashSet, LinkedHashSet, and TreeSet. It also explains the differences between sets and lists, as well as the functionality of maps, specifically HashMap and TreeMap. The chapter highlights key features, performance comparisons, and the use of immutable collections.

Uploaded by

Arya Desai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OOP1_Unit 11 (Amiraj) VisionPapers.in

Chapter 11 covers the concepts of sets and maps in Java, detailing the characteristics and implementations of different set types such as HashSet, LinkedHashSet, and TreeSet. It also explains the differences between sets and lists, as well as the functionality of maps, specifically HashMap and TreeMap. The chapter highlights key features, performance comparisons, and the use of immutable collections.

Uploaded by

Arya Desai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CHAPTER 11

SETS AND MAPS

-PREPARED BY
SUBJECT:OOP-I PREPARED BY:
CODE:3140705 ASST.PROF.NENSI KANSAGARA PROF.NENSI KANSAGARA
(CSE DEPARTMENT,ACET)
SETS:

❖ The set interface extends the collection interface


❖ The set is a collection having no duplicates elements
❖ The concrete classes of set are
HashSet,LinkedHashSet and TreeSet
HASH SET:

This class implements the Set interface, backed by a hash table


(actually a HashMap instance). It makes no guarantees as to the
iteration order of the set; in particular, it does not guarantee that the
order will remain constant over time. This class permits the null
element. This class is not synchronized. However it can be
synchronized explicitly like this:

Set s = Collections.synchronizedSet(new HashSet(...));


Points to Note about HashSet:

1. HashSet doesn’t maintain any order, the elements would be returned in


any random order.
2. HashSet doesn’t allow duplicates. If you try to add a duplicate element in
HashSet, the old value would be overwritten.
3. HashSet allows null values however if you insert more than one nulls it
would still return only one null value.
4. HashSet is non-synchronized.
5. The iterator returned by this class is fail-fast which means iterator would
throw ConcurrentModificationException if HashSet has been modified
after creation of iterator, by any means except iterator’s own remove
method.
LINKEDLIST HASHSET:

LinkedHashSet is also an implementation of Set interface, it is similar to


the HashSet and TreeSet except the below mentioned differences:

1. HashSet doesn’t maintain any kind of order of its elements.


2. TreeSet sorts the elements in ascending order.
3. LinkedHashSet maintains the insertion order. Elements gets
sorted in the same sequence in which they have been added to the
Set.
TREE SET:

TreeSet is similar to HashSet except that it sorts the elements in the


ascending order while HashSet doesn’t maintain any order. TreeSet
allows null element but like HashSet it doesn’t allow. Like most of the
other collection classes this class is also not synchronized, however it
can be synchronized explicitly like this:

SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));


COMPARING THE PERFORMANCE OF SETS AND
LISTS

1) List is an ordered collection it maintains the insertion order, which means


upon displaying the list content it will display the elements in the same order in
which they got inserted into the list.

Set is an unordered collection, it doesn’t maintain any order. There are few
implementations of Set which maintains the order such as LinkedHashSet (It
maintains the elements in insertion order).

2) List allows duplicates while Set doesn’t allow duplicate elements. All the
elements of a Set should be unique if you try to insert the duplicate element in
Set it would replace the existing value.
3) List implementations: ArrayList, LinkedList etc.

Set implementations: HashSet, LinkedHashSet, TreeSet etc.

4) List allows any number of null values. Set can have only a single null
value at most.

5) ListIterator can be used to traverse a List in both the directions(forward


and backward) However it can not be used to traverse a Set. We can use
Iterator (It works with List too) to traverse a Set.

6) List interface has one legacy class called Vector whereas Set interface
does not have any legacy class.
SINGLETON AND UNMODIFIED COLLECTION:

❖ Collections class defines three constants


-EMPTY_SET,EMPTY_LIST AND EMPTY_MAP for empty
set,an empty list and empty map.These Collections are
immutable.
❖ The collection class also provides a method name singleton(object
ob) for creating an immutable set containing only one item
❖ There are some unmodifiable methods defined by Collection
class.
MAPS

❖ A map contains values on the basis


of key, i.e. key and value pair. Each
key and value pair is known as an
entry. A Map contains unique keys.
❖ A Map is useful if you have to
search, update or delete elements on
the basis of a key.
HASH TABLE

This class implements a hash table, which maps keys to values. Any
non-null object can be used as a key or as a value. Hashtable is similar
to HashMap except it is synchronized.

Hashtable class declaration


Let's see the declaration for java.util.Hashtable class.

1. public class Hashtable<K,V> extends Dictionary<K,V> implements


Map<K,V>
Methods for HashTable
HASH MAP

Java HashMap class implements the map interface by using a hash table. It inherits
AbstractMap class and implements Map interface.

Points to remember
❖ Java HashMap class contains values based on the key.
❖ Java HashMap class contains only unique keys.
❖ Java HashMap class may have one null key and multiple null values.
❖ Java HashMap class is non synchronized.
❖ Java HashMap class maintains no order.
❖ The initial default capacity of Java HashMap class is 16 with a load factor of 0.75.
Methods for HashMap
DIFFERENCE BETWEEN HASHTABLE AND
HASHMAP
TREEMAP

Java TreeMap class is a red-black tree based implementation. It provides an efficient


means of storing key-value pairs in sorted order.

The important points about Java TreeMap class are:

❖ Java TreeMap contains values based on the key. It implements the NavigableMap
interface and extends AbstractMap class.
❖ Java TreeMap contains only unique elements.
❖ Java TreeMap cannot have a null key but can have multiple null values.
❖ Java TreeMap is non synchronized.
❖ Java TreeMap maintains ascending order.
public class TreeMap<K,V> extends AbstractMap<K,V> implements
NavigableMap<K,V>

TreeMap class Parameters


Let's see the Parameters for java.util.TreeMap class.

● K: It is the type of keys maintained by this map.


● V: It is the type of mapped values.
Methods For TreeMap

You might also like