✍️
Notes.md
  • Table of contents
  • React.Js
    • React Hooks
    • Old :- React : Using Classes
  • Blockchain
    • Solidity
    • Custom ERC20 token
    • Contract
  • Tools and Tech
    • Docker
    • Git version Control
  • Java
    • Data & Data Types
    • IO in Java
    • Data Structures
      • Array in Java
      • Collections in Java
      • Map in Java
      • Enums in Java
      • Linked List in Java
      • List in Java
      • Queues & Stacks
      • Set in Java
      • TreeSet and TreeMap
    • Object Oriented Programming
      • Object Class Methods and Constructor
      • Immutable Class & Objects
      • Constructors
      • Visibility
      • Generics
    • Threads in Java
    • Useful Stuff Java
      • Lambda & Stream
    • Keywords in Java
      • Annotations
      • Comparators
      • Packages in Java
    • Miscellaneous
    • Articles to refer to
  • Golang
    • Competitive Programming in Go
    • Testing simple web server
    • Learning Go : Part 1
    • Maps vs slices
    • Golang Garbage Collector 101
    • Things Golang do differently
    • Go Things
  • Linux
    • Shell programming
    • Linux Commands Part 1 - 4
    • Linux Commands Part 5 - 8
    • Linux Commands Part 9 - 10
  • Software Design
    • Solid Design
    • OOPS
    • Design Patterns
      • Creational Design Pattern
        • Builder DP
        • Factory DP
        • Singleton DP
      • Adapter DP
      • Bridge DP
      • Iterator DP
      • State DP
      • Strategy DP
      • Behavioral Design Pattern
        • Observer DP
      • Structural Design Pattern
        • Facade DP
  • Cloud
    • Google Cloud Platform
      • GCP Core Infrastructure
      • Cloud Networking
  • Spring Boot
    • Spring Basics
      • Spring Beans
      • Important Annotations
      • Important Spring Things
      • Maven Things
      • Spring A.O.P
    • Spring Boot Controller
      • Response Entity Exception Handling
    • Spring Things
    • Spring MVC
    • Spring Data
      • Redis
      • Spring Data JPA
      • JDBC
    • Apache Camel
  • Miscellaneous
    • Troubleshooting and Debugging
Powered by GitBook
On this page
  • Set
  • Creating a set
  • Adding elements to a Set
  • Delete all the elements of a Set
  • Check size

Was this helpful?

  1. Java
  2. Data Structures

Set in Java

Get set Go

PreviousQueues & StacksNextTreeSet and TreeMap

Last updated 3 years ago

Was this helpful?

import

import java.util.Set;
import java.util.HashSet;

Set

Set have its implementation in various classes like HashSet , TreeSet , LinkedHashSet .

// Hashset Random Sorting
Set<T> set = new HashSet<T>();

// TreeSet - By compareTo() or Comparator
TreeSet<T> sortedSet = new TreeSet<T>();

// LinkedHashSet - Insertion Order
LinkedHashSet<T> linkedhashset = new LinkedHashSet<T>();

Creating a set

Set<Integer> set = new HashSet<Integer>();
// Creates an empty Set of Integers

Set<Integer> linkedHashSet = new LinkedHashSet<Integer>(); 
//Creates a empty Set of Integers, with predictable iteration order

Adding elements to a Set

set.add(12); 
set.add(13);

Delete all the elements of a Set

set.clear();
//Removes all objects from the collection.

set.remove(0); 
// Removes first occurrence of a specified object from the collection

Check size

set.size(); 
//Returns the number of elements in the collection

set.isEmpty();
//Returns true if the set has no elements
Class Diagram