Core java - Advance Topics
  • Welcome
  • Schedule
  • 1) Exception Handling
    • 1) Introduction to Exception Handling
    • 2) Categories of Exceptions
    • 3) Creating a method that throws an exception
    • 4) Creating Custom Exception Classes
    • 5)What happens when an exception is thrown?
      • 5.1) Creating try-catch-finally blocks
      • 5.2) Using a method that throws a checked exception
      • 5.3) Using a method that throws a runtime exception
      • 5.4) Using a method that throws an error
      • 5.5) Will a finally block execute even if the catch block defines a return statement?
      • 5.6) What happens if both a catch and a finally block define return statement?
      • 5.7) What happens if a finally block modifies the value returned from a catch block?
      • 5.8) Can a try block be followed only by a finally block?
      • 5.9) Does the order of the exceptions caught in the catch blocks matter?
      • 5.10) Can I rethrow an exception or the error I catch?
      • 5.11) Can I declare my methods to throw a checked exception instead of handling it?
      • 5.12) I can create nested loops, so can I create nested try-catch blocks too?
      • 5.13) Should I handle errors?
    • 6) Best Practices
    • 7) Cheat Sheet
    • 8) Problems
  • 2) Wrapper Classes and Enums
    • 2.1) Creating objects of the wrapper classes
    • Enums
  • 3) Inner Classes
    • 3.1) Static nested class (also called static inner class)
    • 3.2) Inner class (also called member class)
    • 3.3) Anonymous inner class
    • 3.4) Method local inner classes
    • CheatSheet
  • 4) Generics
    • Multiple Type parameters in Generic classes
    • Inheritance using Generics
    • Generic interfaces
    • Generic Methods
    • Bounded type parameters
    • Applications
  • 5) Equals and Hashcode
    • Problems
  • CompareTo method overview
  • Basic DS
    • 1) Simple Array List
    • 2) Simple HashMap
  • 5) Collections Framework - Part 1
    • Introducing the collections framework
    • Working with the Collection interface
      • The core Collection interface
      • Methods of the Collection interface
    • Creating and using List, Set, and Deque implementations
      • List interface and its implementations
      • Iterators
      • Sorting List using custom sorting technique
      • Comparable Interface
      • Custom Sorting using comparator
      • ArrayList - Examples and practice problems
    • Stack
    • Linked List
    • LinkedList Operations
  • 6) Collections Framework - Part 2
    • Sets
      • Set Types
      • Array to Set (vice versa)
    • Maps
    • TreeMap
    • Autoboxing And Unboxing
  • Collections Framework - Part 3
    • Basics : DS , Number System
    • Internal Working
      • HashMap
      • HashSet
  • 7) Reflection API
  • 8) Annotations
  • 9) Reading Input From Various Sources
    • File Handling
    • Reading From Xml
    • Reading From JSON
  • 10) Multi-threading (Concurrency)
    • Protect shared data
    • Thread-safe access to shared data
  • 11) Design Patterns
    • Singleton
    • DI
  • 12) Internal Working of JVM
  • 13) Garbage Collection
  • 14) More on Strings (Buffer and Builder)
  • 15) Cloning and Immutable Class
    • 16) Serialization And Deserialization
    • Untitled
  • JAVA 8
    • Interface Changes
    • Lambda
    • Method Ref
    • Optional
    • Streams
    • Predicates
  • Practice Tests
    • Test - Collections
    • OOPS
    • S-OOPS
Powered by GitBook
On this page
  • Set in Java
  • Basic Set operation :
  • Problems:
  • CC:

Was this helpful?

  1. 6) Collections Framework - Part 2

Sets

Set in Java

  • Set is an interface which extends Collection. It is an unordered collection of objects in which duplicate values cannot be stored.

  • Basically, Set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation).

  • Set has various methods to add, remove clear, size, etc to enhance the usage of this interface

// Java code for adding elements in Set 
import java.util.HashSet;
import java.util.Set; 
public class SetExample 
{ 
	public static void main(String[] args) 
	{ 
		// Set demo using HashSet 
		Set<String> hash_Set = new HashSet<String>(); 
		
		hash_Set.add("GyanSetu"); 
		hash_Set.add("For"); 
		hash_Set.add("GyanSetu"); 
		hash_Set.add("Example"); 
		hash_Set.add("Set"); 
		System.out.print("Set output without the duplicates"); 

		System.out.println(hash_Set); 
 
	} 
} 
Set output without the duplicates[GyanSetu, Set, Example, For]

Basic Set operation :

Union In this, we could simply add one Set with other. Since the Set will itself not allow any duplicate entries, we need not take care of the common values.

Expected Output:

Union : [0, 1, 2, 3, 4, 5, 7, 8, 9]

Intersection We just need to retain the common values from both Sets.

Expected Output:

Intersection : [0, 1, 3, 4]

Difference We just need to remove all the values of one Set from the other. Expected Output:

Difference : [2, 8, 9]
// Java code for demonstrating union, intersection and difference 
// on Set 
import java.util.HashSet;
import java.util.Set; 
public class SetExample 
{ 
	public static void main(String args[]) 
	{ 
		Set<Integer> a = new HashSet<Integer>(); 
		a.add(1);
		a.add(3);
		a.add(2);
		a.add(4);
		a.add(8);
		a.add(9);
		a.add(0);
		Set<Integer> b = new HashSet<Integer>(); 
		b.add(1);
		b.add(3);
		b.add(7);
		b.add(5);
		b.add(4);
		b.add(0);
		b.add(7);
		a.add(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); 
	} 
} 
Union of the two Set[0, 1, 2, 3, 4, 5, 7, 8, 9]
Intersection of the two Set[0, 1, 3, 4, 5]
Difference of the two Set[2, 8, 9]

Problems:

CC:

Previous6) Collections Framework - Part 2NextSet Types

Last updated 6 years ago

Was this helpful?

1. Write a Java program to append the specified element to the end of a hash set.

2. Write a Java program to iterate through all elements in a hash list.

3. Write a Java program to get the number of elements in a hash set.

4. Write a Java program to empty an hash set.

5. Write a Java program to test a hash set is empty or not.

6. Write a Java program to clone a hash set to another hash set.

7. Write a Java program to convert a hash set to an array.

8. Write a Java program to convert a hash set to a tree set.

9. Write a Java program to convert a hash set to a List/ArrayList.

10. Write a Java program to compare two hash set.

11. Write a Java program to compare two sets and retain elements which are same on both sets.

12. Write a Java program to remove all of the elements from a hash set.

Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
LogoJava Hashset | HackerRankHackerRank