Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create java-hash-set examples #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions java-hash-set examples
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import java.util.HashSet;
import java.util.Set;

public class CreateHashSetExample {
public static void main(String[] args) {
// Creating a HashSet
Set<String> daysOfWeek = new HashSet<>();

// Adding new elements to the HashSet
daysOfWeek.add("Monday");
daysOfWeek.add("Tuesday");
daysOfWeek.add("Wednesday");
daysOfWeek.add("Thursday");
daysOfWeek.add("Friday");
daysOfWeek.add("Saturday");
daysOfWeek.add("Sunday");

// Adding duplicate elements will be ignored
daysOfWeek.add("Monday");

System.out.println(daysOfWeek);
}
}

***************************************************************************************************************************************************************************
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CreateHashSetFromCollectionExample {
public static void main(String[] args) {
List<Integer> numbersDivisibleBy5 = new ArrayList<>();
numbersDivisibleBy5.add(5);
numbersDivisibleBy5.add(10);
numbersDivisibleBy5.add(15);
numbersDivisibleBy5.add(20);
numbersDivisibleBy5.add(25);

List<Integer> numbersDivisibleBy3 = new ArrayList<>();
numbersDivisibleBy3.add(3);
numbersDivisibleBy3.add(6);
numbersDivisibleBy3.add(9);
numbersDivisibleBy3.add(12);
numbersDivisibleBy3.add(15);

// Creating a HashSet from another collection (ArrayList)
Set<Integer> numbersDivisibleBy5Or3 = new HashSet<>(numbersDivisibleBy5);

// Adding all the elements from an existing collection to a HashSet
numbersDivisibleBy5Or3.addAll(numbersDivisibleBy3);

System.out.println(numbersDivisibleBy5Or3);
}
}
***************************************************************************************************************************************************************************

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class HashSetRemoveExample {
public static void main(String[] args) {
Set<Integer> numbers = new HashSet<>();
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
numbers.add(7);
numbers.add(8);
numbers.add(9);
numbers.add(10);

System.out.println("numbers : " + numbers);

// Remove an element from a HashSet (The remove() method returns false if the element does not exist in the HashSet)
boolean isRemoved = numbers.remove(10);
System.out.println("After remove(10) => " + numbers);

// Remove all elements belonging to a given collection from a HashSet
List<Integer> perfectSquares = new ArrayList<>();
perfectSquares.add(4);
perfectSquares.add(9);

numbers.removeAll(perfectSquares);
System.out.println("After removeAll(perfectSquares) => " + numbers);

// Remove all elements matching a given predicate
numbers.removeIf(num -> num % 2 == 0);
System.out.println("After removeIf() => " + numbers);

// Remove all elements from HashSet (clear it completely)
numbers.clear();
System.out.println("After clear() => " + numbers);
}
}

***************************************************************************************************************************************************************************

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

public class IterateOverHashSetExample {
public static void main(String[] args) {
Set<String> programmingLanguages = new HashSet<>();
programmingLanguages.add("C");
programmingLanguages.add("C++");
programmingLanguages.add("Java");
programmingLanguages.add("Python");
programmingLanguages.add("PHP");
programmingLanguages.add("Ruby");

System.out.println("=== Iterate over a HashSet using Java 8 forEach and lambda ===");
programmingLanguages.forEach(programmingLanguage -> {
System.out.println(programmingLanguage);
});

System.out.println("=== Iterate over a HashSet using iterator() ===");
Iterator<String> programmingLanguageIterator = programmingLanguages.iterator();
while (programmingLanguageIterator.hasNext()) {
String programmingLanguage = programmingLanguageIterator.next();
System.out.println(programmingLanguage);
}

System.out.println("=== Iterate over a HashSet using iterator() and Java 8 forEachRemaining() method ===");
programmingLanguageIterator = programmingLanguages.iterator();
programmingLanguageIterator.forEachRemaining(programmingLanguage -> {
System.out.println(programmingLanguage);
});

System.out.println("=== Iterate over a HashSet using simple for-each loop ===");
for(String programmingLanguage: programmingLanguages) {
System.out.println(programmingLanguage);
}
}
}


***************************************************************************************************************************************************************************