Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views12 pages

Unit 3 Session 7

The document provides an overview of the Set and Map interfaces in Java, detailing their characteristics and implementations such as HashSet, LinkedHashSet, and TreeSet. It explains the unique properties of each collection type, including how they handle duplicates and maintain order. Additionally, it covers the HashMap class, emphasizing its role in key-value data management and efficient data retrieval.

Uploaded by

gnanavel.m
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)
6 views12 pages

Unit 3 Session 7

The document provides an overview of the Set and Map interfaces in Java, detailing their characteristics and implementations such as HashSet, LinkedHashSet, and TreeSet. It explains the unique properties of each collection type, including how they handle duplicates and maintain order. Additionally, it covers the HashMap class, emphasizing its role in key-value data management and efficient data retrieval.

Uploaded by

gnanavel.m
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/ 12

KGiSL Institute of Technology

(Approved by AICTE, New Delhi; Affiliated to Anna University, Chennai)


Recognized by UGC, Accredited by NBA (IT)
365, KGiSL Campus, Thudiyalur Road, Saravanampatti, Coimbatore – 641035.

Department of Computer Science and Engineering

Name of the Faculty : Gnanavel M


Subject Name & Code : 24UCS312 - OBJECT ORIENTED PROGRAMMING
Branch & Department : BE.CSE
Year & Semester : II Year / III Sem
Academic Year :2025-2026
Set Interface

• Set Interface in Java is present in java.util package. It extends the


Collection interface.
• It represents the unordered set of elements which does not allow us
to store the duplicate items.
• We can store at most one null value in Set. HashSet, LinkedHashSet,
and TreeSet implement set. The set can be instantiated as:
• Set<data-type> s1 = new HashSet<data-type>();
• Set<data-type> s2 = new LinkedHashSet<data-type>();
• Set<data-type> s3 = new TreeSet<data-type>();
HashSet

• The HashSet class implements Set Interface. It represents the


collection that uses a hash table for storage. Hashing is used to store
the elements in the HashSet. It contains unique items.
Examples.
• import java.util.*;
• public class Main {
• public static void main(String args[]) {
• //Creating HashSet and adding elements
• HashSet<String> set=new HashSet<String>();
• set.add("Andrew");
• set.add("Mark");
• set.add("Peter");
• set.add("Johnson");
• //Traversing elements
• Iterator<String> itr=set.iterator();
• while(itr.hasNext()) {
• System.out.println(itr.next());
• }
• }
• }
LinkedHashSet

• The LinkedHashSet class represents the LinkedList implementation of


the Set Interface.
• It extends the HashSet class and implements the Set interface. Like
HashSet, it also contains unique elements.
• It maintains the insertion order and permits null elements
Example

• import java.util.*;
• public class Main{
• public static void main(String args[]){
• LinkedHashSet<String> set=new LinkedHashSet<String>();
• set.add("Peter");
• set.add("Jack");
• set.add("Peter");
• set.add("Johnson");
• Iterator<String> itr=set.iterator();
• while(itr.hasNext()){
• System.out.println(itr.next());
• }
• }
• }
SortedSet Interface

• SortedSet is the alternative to the Set interface that provides a total


ordering of its elements.
• The elements of the SortedSet are arranged in the increasing
(ascending) order.
• The SortedSet provides additional methods that inhibit the natural
ordering of the components.
• The SortedSet can be instantiated as:
• SortedSet<data-type> set = new TreeSet();
TreeSet

• Java TreeSet class implements the Set interface that uses a tree for
storage.
• Like HashSet, TreeSet also contains unique elements. However, the
access and retrieval time of TreeSet is quite fast.
• The aspects in TreeSet are stored in ascending order
Examples..
• import java.util.*;
• public class Main{
• public static void main(String args[]){
• //Creating and adding elements
• TreeSet<String> set=new TreeSet<String>();
• set.add("Thomas");
• set.add("Davis");
• set.add("Thomas");
• set.add("Donald");
• //traversing elements
• Iterator<String> itr=set.iterator();
• while(itr.hasNext()){
• System.out.println(itr.next());
• }
• }
• }
Map Interface

• The Map interface in Java is part of the Java Collections Framework.


• It represents a mapping between a set of keys and their corresponding values.
• A Map cannot contain duplicate keys; each key can map to at most one value.
• The Map interface is used to store key-value pairs, where each key is unique,
and it provides an efficient way to retrieve, update, and manipulate data
based on keys.
• Syntax:
• Map<T, T> hm = new HashMap<>();
• Map<T, T> tm = new TreeMap<>();
HashMap

• HashMap in Java is a key-value data structure offering efficient data


access via keys using hashing.
• Hashing converts large strings or other objects into smaller, consistent
values for quick indexing and searching.
• HashMap implements the Map interface and is used for managing
large datasets efficiently. Additionally,
• HashSet uses HashMap internally to store elements uniquely,
demonstrating the utility of hashing in Java's collections framework
for fast data retrieval and management.
Example..

• import java.util.HashMap;
• import java.util.Map;
• public class Main {
• public static void main(String[] args) {
• // Creating a HashMap
• Map<String, Integer> map = new HashMap<>();
• // Adding key-value pairs to the HashMap
• map.put("Alice", 10);
• map.put("Bob", 20);
• map.put("Charlie", 30);
• // Retrieving a value
• System.out.println("Value for 'Alice': " + map.get("Alice"));
• // Iterating over key-value pairs
• for (Map.Entry<String, Integer> entry : map.entrySet()) {
• String key = entry.getKey();
• Integer value = entry.getValue();
• System.out.println(key + ": " + value);
• }
• // Removing a key-value pair
• map.remove("Charlie");
• // Checking the presence of a key
• if (map.containsKey("Bob")) {
• System.out.println("Map contains key 'Bob'.");
• }
• }
• }

You might also like