Basic Linked HashSet Operations
package com.java2novice.linkedhashset;
import java.util.LinkedHashSet;
public class MyLkdHashSetOperations
{
public static void main(String a[])
{
LinkedHashSet
lhs = new LinkedHashSet();
lhs.add("first");
lhs.add("second");
lhs.add("third");
System.out.println(lhs);
System.out.println("LinkedHashSet
size: "+lhs.size());
System.out.println("Is
LinkedHashSet emplty? : "+lhs.isEmpty());
}
}
How to delete all elements from Linked HashSet
package com.java2novice.linkedhashset;
import java.util.LinkedHashSet;
public class MyLinkedHashSetClear
{
public static void main(String a[])
{
LinkedHashSet
lhs = new LinkedHashSet();
lhs.add("first");
lhs.add("second");
lhs.add("third");
System.out.println("My
LinkedHashSet content:");
System.out.println(lhs);
System.out.println("Clearing
LinkedHashSet:");
lhs.clear();
System.out.println("Content
After clear:");
System.out.println(lhs);
}
}
How to add another collection to Linked HashSet
package com.java2novice.linkedhashset;
import java.util.HashSet;
import java.util.LinkedHashSet;
public class MyLhsAddAllEx
{
public static void main(String a[])
{
LinkedHashSet
lhs = new LinkedHashSet();
lhs.add("first");
lhs.add("second");
lhs.add("third");
System.out.println(lhs);
HashSet
subSet = new HashSet();
subSet.add("s1");
subSet.add("s2");
lhs.addAll(subSet);
System.out.println("LinkedHashSet
content after adding another collection:");
System.out.println(lhs);
}
}
How to iterate through Linked HashSet
package com.java2novice.linkedhashset;
import java.util.Iterator;
import java.util.LinkedHashSet;
public class MyLhsIterateEx
{
public static void main(String a[])
{
LinkedHashSet
lhs = new LinkedHashSet();
lhs.add("first");
lhs.add("second");
lhs.add("third");
Iterator
itr = lhs.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
How to compare two Linked HashSet and retain elements which are same on both Linked HashSet
package com.java2novice.linkedhashset;
import java.util.LinkedHashSet;
public class MyLhsRetainEx
{
public static void main(String a[])
{
LinkedHashSet
lhs = new LinkedHashSet();
lhs.add("first");
lhs.add("second");
lhs.add("third");
lhs.add("apple");
lhs.add("rat");
System.out.println(lhs);
LinkedHashSet
subSet = new LinkedHashSet();
subSet.add("rat");
subSet.add("second");
subSet.add("first");
lhs.retainAll(subSet);
System.out.println("LinkedHashSet
content:");
System.out.println(lhs);
}
}
How to copy content of Linked HashSet to an array
package com.java2novice.linkedhashset;
import java.util.LinkedHashSet;
public class MyLhsToArrayEx
{
public static void main(String a[])
{
LinkedHashSet
lhs = new LinkedHashSet();
lhs.add("first");
lhs.add("second");
lhs.add("third");
System.out.println("LinkedHashSet
content: ");
System.out.println(lhs);
String[]
strArr = new String[lhs.size()];
lhs.toArray(strArr);
System.out.println("Copied
array content:");
for(String
str:strArr)
{
System.out.println(str);
}
}
}
No comments:
Post a Comment