Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
118 views

Sets and Maps in Java

The Set interface in Java represents an unordered collection of unique elements that does not allow duplicate values. It extends the Collection interface and implements the mathematical set abstraction. Common implementations of Set include HashSet, LinkedHashSet, and TreeSet. Sets allow adding, removing, and checking for presence of elements and provide various operations like union, intersection, and difference on multiple sets.

Uploaded by

sathwik vagu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Sets and Maps in Java

The Set interface in Java represents an unordered collection of unique elements that does not allow duplicate values. It extends the Collection interface and implements the mathematical set abstraction. Common implementations of Set include HashSet, LinkedHashSet, and TreeSet. Sets allow adding, removing, and checking for presence of elements and provide various operations like union, intersection, and difference on multiple sets.

Uploaded by

sathwik vagu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Set in Java

 The set interface is present in java.util package


 extends the Collection interface
 an unordered collection of objects 
 duplicate values cannot be stored
 an interface that implements the mathematical set.
 interface contains the methods inherited from the Collection interface
 adds a feature that restricts the insertion of the duplicate elements.

Creating Set Objects


Since Set is an interface, We always need a class that extends this list in order to create an
object.
Set<Obj> set = new HashSet<Obj> ();
Methods:
add(element)
addAll(collection)
clear()
contains(element)
containsAll(collection)
isEmpty()
iterator()
remove(element)
removeAll(collection)
size()

Example:

Set<String> hash_Set = new HashSet<String>();


  
        // Adding elements to the Set
        hash_Set.add("Geeks");
        hash_Set.add("For");
        hash_Set.add("Geeks");
        hash_Set.add("Example");
        hash_Set.add("Set");
  
        // Printing elements of HashSet object
        System.out.println(hash_Set);

Output
[Set, Example, Geeks, For]
Operations on the Set Interface
Let set1 = [1, 3, 2, 4, 8, 9, 0] and set2 = [1, 3, 7, 5, 4, 0, 7, 5]. 

 Intersection = [0, 1, 3, 4] 


 Union = [0, 1, 2, 3, 4, 5, 7, 8, 9] 
 Difference = [2, 8, 9]
Example:
  Set<Integer> a = new HashSet<Integer>();

a.addAll(Arrays.asList(new Integer[] { 1, 3, 2, 4, 8, 9, 0 }));


Set<Integer> b = new HashSet<Integer>();
      b.addAll(Arrays.asList(new Integer[] { 1, 3, 7, 5, 4, 0, 7, 5 }));
 // To find union
        Set<Integer> union = new HashSet<Integer>(a);
        union.addAll(b);
        System.out.print("Union of the two Set");
        System.out.println(union);
  
        // To find intersection
        Set<Integer> intersection = new HashSet<Integer>(a);
        intersection.retainAll(b);
        System.out.print("Intersection of the two Set");
        System.out.println(intersection);
  
        // To find the symmetric difference
        Set<Integer> difference = new HashSet<Integer>(a);
        difference.removeAll(b);
        System.out.print("Difference of the two Set");
        System.out.println(difference);

Performing Various Operations on SortedSet


 Adding Elements

Set<String> hs = new HashSet<String>();


  
        // Adding elements to above object
        // using add() method
        hs.add("B");
        hs.add("B");
        hs.add("C");
        hs.add("A");
  
        // Printing the elements inside the Set object
        System.out.println(hs);
 Accessing the Elements

String check = "D";


  
        // Check if the above string exists in
        // the SortedSet or not
        // using contains() method
        System.out.println("Contains " + check + " "
                           + hs.contains(check));
 Removing the Values

  hs.remove("B");
  
        // Printing Set elements after removing an element
        // and printing updated Set elements
        System.out.println("After removing element " + hs);
  Iterating through the Set

 for (String value : hs)


  
            // Printing all the values inside the object 
            System.out.print(value + ", ");
Java HashSet
A HashSet is a collection of items where every item is unique, and it is found in
the java.util package:
import java.util.HashSet; // Import the HashSet class
HashSet<String> cars = new HashSet<String>();
Add Items
cars.add("Ford");
cars.add("BMW");
cars.add("Mazda");
System.out.println(cars);
Check If an Item Exists
cars.contains("Mazda");
Remove an Item
cars.remove("Volvo");

To remove all items, cars.clear();

HashSet Size
cars.size();
Loop Through a HashSet
for (String i : cars) {
System.out.println(i);
}

Difference between List, Set, and Map in Java


List Set Map

The list interface allows Set does not allow The map does not allow duplicate
duplicate elements duplicate elements. elements

The list maintains Set do not maintain any The map also does not maintain any
insertion order. insertion order.  insertion order. 

We can add any number of But in set almost only The map allows a single null key at
null values. one null value. most and any number of null values.

Set implementation Map implementation classes


List implementation classes are HashMap, HashTable, TreeMap, C
classes are Array are HashSet, LinkedHash oncurrentHashMap,
List, LinkedList. Set, and TreeSet.  and LinkedHashMap.

Set does not provide get


The list provides get() method to get the The map does not  provide get
method to get the element elements at a specified method to get the elements at a
at a specified index. index specified index

If you need to access the If you want to create a


elements frequently by collection of unique If you want to store the data in the
using the index then we elements then we can form of key/value pair then we can
can use the list use set use the map.

To traverse the list


elements by using Iterator can be used
Listlterator. traverse the set elements Through keyset, value, and entry set.

You might also like