0% found this document useful (0 votes)
3 views13 pages

Java Set Functions

The document provides a comprehensive overview of the Java Set interface, detailing its main methods and their implementations through examples. It covers three primary implementations: HashSet (no order), LinkedHashSet (insertion order), and TreeSet (sorted order), explaining their functionalities and differences. Each method is illustrated with Java code snippets to enhance understanding and practical application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Java Set Functions

The document provides a comprehensive overview of the Java Set interface, detailing its main methods and their implementations through examples. It covers three primary implementations: HashSet (no order), LinkedHashSet (insertion order), and TreeSet (sorted order), explaining their functionalities and differences. Each method is illustrated with Java code snippets to enhance understanding and practical application.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

This is a copy of a conversation between ChatGPT & Anonymous.

Report conversation

in collection set under sutopics give all function with


example

Sure! Let’s go step by step. We’ll cover the Java Set interface under Collections with all
main methods, their explanation, and examples. I’ll also separate each method/topic for
clarity.

Java Collection: Set Interface


Set is a collection that does not allow duplicate elements. It does not preserve order

(except LinkedHashSet ) and cannot access elements by index (no get(i) ).

Common Implementations:

HashSet → No order

LinkedHashSet → Insertion order

TreeSet → Sorted order

1. add(E e)
Adds an element to the set. Returns true if added, false if element already exists.

java

import [Link];
import [Link];

public class SetAddExample {


public static void main(String[] args) {
Set<String> set = new HashSet<>();
[Link]([Link]("Apple")); // true
[Link]([Link]("Banana")); // true
[Link]([Link]("Apple")); // false (duplicate)
[Link](set); // [Apple, Banana]
}
Copy code
}

2. addAll(Collection<? extends E> c)


Adds all elements from another collection. Returns true if the set changed.

java

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


[Link]("A");
[Link]("B");

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


[Link]("B");
[Link]("C");

[Link](set2);
[Link](set1); // [A, B, C]
Copy code

3. remove(Object o)
Removes an element if present. Returns true if removed.

java

Set<Integer> set = new HashSet<>();


[Link](1);
[Link](2);
[Link](3);

[Link](2);
[Link](set); // [1, 3]
Copy code

4. removeAll(Collection<?> c)
Removes all elements in the specified collection.

java
Set<String> set = new HashSet<>();
[Link]("A");
[Link]("B");
[Link]("C");

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


[Link]("B");
[Link]("C");

[Link](removeSet);
[Link](set); // [A] Copy code

5. retainAll(Collection<?> c)
Keeps only elements that are in the specified collection (intersection).

java

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


[Link]("A");
[Link]("B");
[Link]("C");

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


[Link]("B");
[Link]("C");
[Link]("D");

[Link](set2);
[Link](set1); // [B, C]
Copy code

6. contains(Object o)
Checks if an element exists. Returns true or false .

java

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


[Link]("Apple");
[Link]("Banana");
[Link]([Link]("Apple")); // true
[Link]([Link]("Orange")); // false Copy code

7. containsAll(Collection<?> c)
Checks if set contains all elements of another collection.

java

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


[Link]("A");
[Link]("B");
[Link]("C");

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


[Link]("B");
[Link]("C");

[Link]([Link](set2)); // true
Copy code

8. clear()
Removes all elements.

java

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


[Link]("A");
[Link]("B");

[Link]();
[Link](set); // []
Copy code

9. isEmpty()
Checks if set is empty.

java
Set<Integer> set = new HashSet<>();
[Link]([Link]()); // true

[Link](10);
Copy code
[Link]([Link]()); // false

10. size()
Returns the number of elements.

java

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


[Link]("A");
[Link]("B");
[Link]([Link]()); // 2
Copy code

11. iterator()
Returns an iterator to traverse the set.

java

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


[Link]("A");
[Link]("B");
[Link]("C");

for (String s : set) {


[Link](s);
}

// Or using Iterator
Iterator<String> it = [Link]();
while([Link]()) {
[Link]([Link]());
}
Copy code
12. toArray()
Converts set to an array.

java

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


[Link]("X");
[Link]("Y");

Object[] arr = [Link]();


for(Object o : arr) {
[Link](o);
}
Copy code

13. equals(Object o)
Checks if two sets are equal (contain same elements, order doesn’t matter).

java

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


[Link]("A");
[Link]("B");

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


[Link]("B");
[Link]("A");

[Link]([Link](set2)); // true
Copy code

14. hashCode()
Returns hash code of the set.

java

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


[Link]("A");
[Link]("B");

Copy code
Copy code

[Link]([Link]());

✅ Summary:
Set mainly avoids duplicates. Methods are mainly for add, remove, check, traverse, and

convert.

If you want, I can make a PDF for all Set methods with examples like I did for List , so
you can save it for revision and interview prep.

Do you want me to do that next?

HashSet → No order

LinkedHashSet → Insertion order

TreeSet → Sorted order separate each topic all functions

Perfect! Let’s break this Set topic into three main implementations and cover all their
functions with examples separately. I’ll make it very structured.

1️⃣ HashSet (No order)


HashSet stores unique elements and does not maintain any order.

It is fast for add, remove, contains operations.

Common Functions in HashSet:

a. add(E e)

java
import [Link];
import [Link];

public class HashSetExample {


public static void main(String[] args) {
Set<String> set = new HashSet<>();
[Link]("Apple"); // true
[Link]("Banana"); // true
[Link]("Apple"); // false (duplicate)
[Link](set); // [Banana, Apple] (order not guaranteed)
Copy code
}
}

b. remove(Object o)

java

[Link]("Banana");
[Link](set); // [Apple]
Copy code

c. contains(Object o)

java

[Link]([Link]("Apple")); // true
[Link]([Link]("Orange")); // false
Copy code

d. size()

java

[Link]([Link]()); // 1
Copy code

e. isEmpty()

java
[Link]([Link]()); // false
Copy code

f. addAll(Collection c), removeAll(Collection c), retainAll(Collection c)


Standard collection operations work the same:

java

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


[Link]("Apple");
[Link]("Orange");

[Link](set2);
[Link](set); // [Apple, Orange]

[Link](set2); // intersection
[Link](set); // [Apple, Orange]

[Link](set2); // remove all


[Link](set); // []
Copy code

g. clear()

java

[Link]();
[Link]([Link]()); // true
Copy code

h. iterator()

java

[Link]("A");
[Link]("B");
[Link]("C");

for(String s : set) {
[Link](s); // order not guaranteed
Copy code
}

i. toArray()

java

Object[] arr = [Link]();


for(Object o : arr) [Link](o);
Copy code

2️⃣ LinkedHashSet (Insertion order)


LinkedHashSet maintains the order in which elements are inserted.

All functions are same as HashSet, but iteration preserves insertion order.

Example of LinkedHashSet

java

import [Link];
import [Link];

public class LinkedHashSetExample {


public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>();
[Link]("Banana");
[Link]("Apple");
[Link]("Orange");
[Link]("Banana"); // duplicate ignored

[Link](set); // [Banana, Apple, Orange] → insertion order pre


}
}
Copy code

 

Other methods:
add , remove , contains , size , isEmpty , addAll , retainAll , removeAll , clear ,

iterator , toArray → all work same as HashSet, but iteration preserves insertion
order.

3️⃣ TreeSet (Sorted order)


TreeSet stores unique elements in sorted order (ascending by default).

It implements SortedSet and NavigableSet .

a. add(E e)

java

import [Link];
import [Link];

public class TreeSetExample {


public static void main(String[] args) {
Set<Integer> set = new TreeSet<>();
[Link](50);
[Link](10);
[Link](30);
[Link](10); // duplicate ignored

[Link](set); // [10, 30, 50] → sorted order


}
}
Copy code

b. remove(Object o), contains(Object o), size(), isEmpty()

java

[Link](30);
[Link]([Link](10)); // true
[Link]([Link]()); // 2
[Link]([Link]()); // false
Copy code

c. addAll(Collection c), removeAll(Collection c), retainAll(Collection c)


Works like HashSet/LinkedHashSet, but result is sorted:

java

Set<Integer> set2 = new TreeSet<>();


[Link](5);
[Link](50);

[Link](set2);
[Link](set); // [5, 10, 50]
Copy code

d. first() and last()

java

TreeSet<Integer> treeSet = new TreeSet<>();


[Link](40);
ChatGPT
[Link](20);
Free offer

[Link](60);

[Link]([Link]()); // 20
[Link]([Link]()); // 60
Copy code

e. lower(), higher(), floor(), ceiling()


lower(x) → greatest element < x

higher(x) → least element > x

floor(x) → greatest ≤ x
ceiling(x) → least ≥ x

java

[Link]([Link](40)); // 20
Copy code
[Link]([Link](40)); // 60
[Link]([Link](35)); // 20
[Link]([Link](35)); // 40

f. iterator() and descendingIterator()


java Copy code

for(Integer i : treeSet) [Link](i); // ascending


Iterator<Integer> desc = [Link]();
while([Link]()) [Link]([Link]()); // descending

✅ Summary of Differences
Feature HashSet LinkedHashSet TreeSet

Order No order Insertion order Sorted order

Duplicates Not allowed Not allowed Not allowed

Iteration Random Insertion order Sorted

Performance Fast Slightly slower Slower (balanced tree)

Extra Methods - - first(), last(), lower(), higher(), floor(),


ceiling(), descendingIterator()

If you want, I can make a single PDF with all 3 sets with all their functions + examples, so
it’s ready for interview prep.

Do you want me to make that PDF next?

You might also like