package org.JavaIncloud.java;
import java.util.HashSet;
import java.util.Set;
public class SetShowCase
{
public static void main
(String...
JavaInCloud) {
/*create an ArrayList(a class implements List) and assign
* to List as we can not create object for List interface*/
Set<String> animals = new HashSet<String>();
//Store some animal name to the collection of animal object(Here String)
animals.add("Tiger");animals.add("Monkey");
animals.add("Cow");animals.add("Cat");
animals.add("Dog");
/*print the Set, it'll print the animal name, as internally ArrayList overwrite toString()*/
System.
out.
println("Animal Name>>"+animals
); //output:Animal Name>>[Cat, Dog, Tiger, Monkey, Cow]
/*there is not get() method for Set hence it'll not work, instead it'll give you a compilation error*/
//System.out.println(Animal.get(3));
/*Retrieve the element from set.
* This is how for-each loop works if you want then you can use Iterator also
* Get the iterator from HashSet and iterate it. Iterator<String> iterator = animals.iterator();*/
{
}
//output: Cat,Tiger,Dog,Monkey,Cow,
/*If you are bothering about order of the set then you have go for LinedHashSet and TreeSet.
* HashSet makes no guarantees as to the iteration order of the set; in particular,
* it does not guarantee that the order will remain constant over time. This class permits the null element.*/
}
}
cGFja2FnZSBvcmcuSmF2YUluY2xvdWQuamF2YTsKCmltcG9ydCBqYXZhLnV0aWwuSGFzaFNldDsKaW1wb3J0IGphdmEudXRpbC5TZXQ7CgpwdWJsaWMgY2xhc3MgU2V0U2hvd0Nhc2UKewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nLi4uSmF2YUluQ2xvdWQpIAoJewoJCS8qY3JlYXRlIGFuIEFycmF5TGlzdChhIGNsYXNzIGltcGxlbWVudHMgTGlzdCkgYW5kIGFzc2lnbiAKCQkgKiB0byBMaXN0IGFzIHdlIGNhbiBub3QgY3JlYXRlIG9iamVjdCBmb3IgTGlzdCBpbnRlcmZhY2UqLwoJCVNldDxTdHJpbmc+CWFuaW1hbHMgPSBuZXcgSGFzaFNldDxTdHJpbmc+KCk7CgkJCgkJLy9TdG9yZSBzb21lIGFuaW1hbCBuYW1lIHRvIHRoZSBjb2xsZWN0aW9uIG9mIGFuaW1hbCBv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