| Start |
|---|
| Initialization A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.
For example: HashSet: Here TreeSet: It stores data in a sorted manner sacrificing some speed for basic operations which take O(lg(n)). It does not maintain the insertion order of items. LinkedHashSet: It is a linked list implementation of |
| Basics of Set What is a Set? A set is a data structure which contains a set of elements with an important property that no two elements in the set are equal. Types of Set:
Creating a set Adding elements to a Set Elements can be added to a set using the Our set after executing this method: Delete all the elements of a Set After this set will be: Check whether an element is part of the Set Existence of an element in the set can be checked using the Output: Check whether a Set is empty
Output: True Remove an element from the Set Check the Size of the Set Output: 0 |
| Types and Usage of Sets Generally, sets are a type of collection which stores unique values. Uniqueness is determined by the Sorting is determined by the type of set. |
| Declaring a HashSet with values You can create a new class that inherits from HashSet: One line solution: Using guava: Using Streams: |
| Create a list from an existing Set Using a new List Using List.addAll() method Using Java 8 Steam API |
| Eliminating duplicates using Set Suppose you have a collection Example: Output: |
Read Article
https://develop.horje.com/learn/1434/reference