×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Added: May 5, 2021 4:12 PM
Views: 3988
Tags: collections
  1.  public static void main(String[] args) {
  2.  
  3.         //Initialisieren wie ArrayLists
  4.         var colors = new HashSet<String>();
  5.  
  6.         // use add() method to add values in the hash set
  7.         colors.add("Red");
  8.         colors.add("Green");
  9.         colors.add("Black");
  10.         colors.add("White");
  11.         colors.add("Pink");
  12.         colors.add("Yellow");
  13.  
  14.  
  15.         var newcolors = new HashSet<String>();
  16.         newcolors.add("Red");
  17.         newcolors.add("Pink");
  18.         newcolors.add("Black");
  19.         newcolors.add("Orange");
  20.  
  21.         System.out.println("Erstes HashSet Inhalt: " + colors);
  22.         System.out.println("Zweites HashSet Inhalt: " + newcolors);
  23.  
  24.         //clear() Methode löscht alle Elemente aus einem HashSet
  25.         //HashSet wird danach leer sein!
  26.         colors.clear();
  27.  
  28.         //Danach
  29.         System.out.println("Nachher: " + colors);
  30.     }
  31. }
  32.