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

Java Set Interface Methods Cheat Sheet

The Set interface in Java, part of the java.util package, represents a collection that disallows duplicate elements, with common implementations including HashSet, LinkedHashSet, and TreeSet. Key methods include add, remove, clear, contains, isEmpty, size, iterator, toArray, retainAll, removeAll, and containsAll, each serving specific functionalities for managing set elements. This cheat sheet provides examples for each method to illustrate their usage.

Uploaded by

Jaya Ram
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)
6 views3 pages

Java Set Interface Methods Cheat Sheet

The Set interface in Java, part of the java.util package, represents a collection that disallows duplicate elements, with common implementations including HashSet, LinkedHashSet, and TreeSet. Key methods include add, remove, clear, contains, isEmpty, size, iterator, toArray, retainAll, removeAll, and containsAll, each serving specific functionalities for managing set elements. This cheat sheet provides examples for each method to illustrate their usage.

Uploaded by

Jaya Ram
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

Java Collection Interface Methods - Cheat Sheet

What is Set Interface?

The Set interface in Java is a part of the [Link] package and extends the Collection interface. It represents

a collection that does not allow duplicate elements. Common implementations include HashSet,

LinkedHashSet, and TreeSet.

- HashSet: Unordered and allows null.

- LinkedHashSet: Maintains insertion order.

- TreeSet: Sorted set, no null allowed (uses natural ordering or custom Comparator).

add(E e)

Adds the specified element to the set if it is not already present.

Example:

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

[Link]("Java");

addAll(Collection<? extends E> c)

Adds all elements from the specified collection.

Example:

[Link]([Link]("Python", "C++"));

remove(Object o)

Removes the specified element if present.

Example:

[Link]("C++");

clear()

Removes all elements.

Example:
Java Collection Interface Methods - Cheat Sheet

[Link]();

contains(Object o)

Returns true if the set contains the specified element.

Example:

[Link]("Java");

isEmpty()

Checks if the set is empty.

Example:

[Link]();

size()

Returns the number of elements.

Example:

[Link]();

iterator()

Returns an iterator to traverse elements.

Example:

Iterator<String> it = [Link]();

toArray()

Converts the set to an array.

Example:

Object[] arr = [Link]();

retainAll(Collection<?> c)
Java Collection Interface Methods - Cheat Sheet

Keeps only the elements that are also in the specified collection.

Example:

[Link](anotherSet);

removeAll(Collection<?> c)

Removes all matching elements from the set.

Example:

[Link](anotherSet);

containsAll(Collection<?> c)

Checks if the set contains all elements of the specified collection.

Example:

[Link](anotherSet);

You might also like