fork download
  1. package org.JavaIncloud.java;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6.  
  7. public class CommonOpetationWithSet
  8. {
  9. public static void main(String...JavaInCloud)
  10. {
  11. Set<String> luxuryBike = new HashSet<String>();
  12.  
  13. //List will not give an exception if you'll try adding duplicate element but it'll simply ignore while storing
  14. luxuryBike.add("MTT-Turbine"); luxuryBike.add("Coventry-Eagle");
  15. luxuryBike.add("Fighter-Combat "); luxuryBike.add("Wazuma-V8");
  16. luxuryBike.add("Brough-Superior ");luxuryBike.add("Ferrari 900");
  17. luxuryBike.add("Harley-Davidson"); luxuryBike.add("Harley-Davidson");
  18.  
  19. System.out.println("luxuryBike>>"+luxuryBike);
  20. //luxuryBike>>[Coventry-Eagle, Fighter-Combat , Brough-Superior , Wazuma-V8, Harley-Davidson, Ferrari 900, MTT-Turbine]
  21.  
  22.  
  23. if(luxuryBike.contains("Harley-Davidson"))
  24. luxuryBike.remove("Harley-Davidson");
  25.  
  26. System.out.println("luxuryBike after removing>>"+luxuryBike);
  27. //luxuryBike after removing>>[Coventry-Eagle, Fighter-Combat , Brough-Superior , Wazuma-V8, Ferrari 900, MTT-Turbine]
  28.  
  29. if(luxuryBike.size() < 8 && !luxuryBike.contains("Harley-Davidson"))
  30. luxuryBike.add("Harley-Davidson");
  31.  
  32. System.out.println("luxuryBike>>"+luxuryBike);
  33. //luxuryBike>>[Coventry-Eagle, Fighter-Combat , Brough-Superior , Wazuma-V8, Harley-Davidson, Ferrari 900, MTT-Turbine]
  34. //How to use Iterator over Set.
  35. Iterator<String> iterator = luxuryBike.iterator();
  36. while (iterator.hasNext())
  37. {
  38. String bike = (String) iterator.next();
  39. System.out.print(bike);
  40. }
  41. }
  42. }
  43.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:7: error: class CommonOpetationWithSet is public, should be declared in a file named CommonOpetationWithSet.java
public class CommonOpetationWithSet 
       ^
1 error
stdout
Standard output is empty