From 35ec87a0f21202cfa042908bd1049e8ce102e8bb Mon Sep 17 00:00:00 2001 From: parte-Ajinkya <112530584+parte-Ajinkya@users.noreply.github.com> Date: Wed, 19 Oct 2022 22:09:18 +0530 Subject: [PATCH] Create java-hash-set examples --- java-hash-set examples | 142 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 java-hash-set examples diff --git a/java-hash-set examples b/java-hash-set examples new file mode 100644 index 0000000..dbc7a1b --- /dev/null +++ b/java-hash-set examples @@ -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 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 numbersDivisibleBy5 = new ArrayList<>(); + numbersDivisibleBy5.add(5); + numbersDivisibleBy5.add(10); + numbersDivisibleBy5.add(15); + numbersDivisibleBy5.add(20); + numbersDivisibleBy5.add(25); + + List 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 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 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 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 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 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); + } + } +} + + +***************************************************************************************************************************************************************************