Examples for usage of all methods of 'java.util.HashSet' with console output of example code.
Please click on method from below list to go to code example for usage of that method. Click [↓ Imports] to get import statements used in examples. To read javadoc of methods, click on [⿺ Javadoc] for that method.
Set<String> set = new HashSet<>();
set.add("item-1");
// Returns true if element was not present before.
boolean isAdded = set.add("item-2");
System.out.println("Is item-2 added? = " + isAdded);
set.add("item-3");
set.add("item-4");
System.out.println("Set = " + set);
// Try to add same element again. It is already present & set can't have
// duplicate, so returns false.
boolean is2Added = set.add("item-2");
System.out.println("Is item-2 added? = " + is2Added);
// Try adding multiple null values to hashset. Only adds one null.
System.out.println("Adding null " + set.add(null));
System.out.println("Adding null " + set.add(null));
System.out.println("HashSet after adding null = " + set);
Output: Is item-2 added? = true Set = [item-4, item-3, item-2, item-1] Is item-2 added? = false Adding null true Adding null false HashSet after adding null = [null, item-4, item-3, item-2, item-1]
Tag: Example for add method of class java.util.HashSet., HashSet add function example with arguments E arg0, How to use add method of HashSet?, Usage of HashSet.add, HashSet.add() examples
Set<String> set = new HashSet<>();
set.add("item-1");
set.add("item-2");
set.add("item-3");
set.add("item-4");
// List has item-1 & item-2 same as hash set
List<String> list = new ArrayList<>();
list.add("item-1");
list.add("item-2");
list.add("item-5");
list.add("item-6");
// Add array list to hash set
set.addAll(list);
System.out.println("Set after addAll from list = " + set);
List<String> listOfNulls = new ArrayList<>();
listOfNulls.add(null);
listOfNulls.add(null);
// Add array list of nulls to hash set
set.addAll(listOfNulls);
System.out.println("Set after addAll from list of null = " + set);
Output: Set after addAll from list = [item-6, item-5, item-4, item-3, item-2, item-1] Set after addAll from list of null = [null, item-6, item-5, item-4, item-3, item-2, item-1]
Tag: Example for addAll method of class java.util.AbstractCollection., AbstractCollection addAll function example with arguments java.util.Collection<? extends E> arg0, How to use addAll method of AbstractCollection?, Usage of AbstractCollection.addAll, AbstractCollection.addAll() examples
Set<String> set = new HashSet<>();
set.add("item-1");
set.add("item-2");
set.add("item-3");
set.add("item-4");
System.out.println("Set = " + set);
set.clear();
System.out.println("Set after clear = " + set);
Output: Set = [item-4, item-3, item-2, item-1] Set after clear = []
Tag: Example for clear method of class java.util.HashSet., HashSet clear function example , How to use clear method of HashSet?, Usage of HashSet.clear, HashSet.clear() examples
HashSet<String> set = new HashSet<>();
set.add("item-0");
set.add("item-1");
set.add("item-2");
System.out.println("set values = " + set);
HashSet<String> cloneHashSet = (HashSet<String>) set.clone();
System.out.println("cloneHashSet values = " + cloneHashSet);
Output: set values = [item-2, item-1, item-0] cloneHashSet values = [item-1, item-0, item-2]
Tag: Example for clone method of class java.util.HashSet., HashSet clone function example , How to use clone method of HashSet?, Usage of HashSet.clone, HashSet.clone() examples
Import statements used in examples.
java.util.HashSet
java.util.List
java.util.Set
java.util.ArrayList
java.util.Collection
java.util.function.Consumer
java.util.function.Predicate
java.util.function.IntFunction
Tag: Simple working examples of methods / functions of class java.util.HashSet along with their console output, java.util.HashSet tutorial., Guide to java.util.HashSet & its methods., Understanding java.util.HashSet with examples.