Java/Collections Data Structure/LinkedHashSet
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Creating a Set That Retains Order-of-Insertion
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
public class Main {
public static void main(String[] argv) throws Exception {
Set<String> set = new LinkedHashSet<String>();
// Add some elements
set.add("1");
set.add("2");
set.add("3");
set.add("2");
// List the elements
for (Iterator it = set.iterator(); it.hasNext();) {
Object o = it.next();
}
}
}
Get Size of Java LinkedHashSet
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<Integer> lhashSet = new LinkedHashSet<Integer>();
System.out.println("Size of LinkedHashSet : " + lhashSet.size());
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println(lhashSet.size());
lhashSet.remove(new Integer("1"));
System.out.println(lhashSet.size());
}
}
Iterate through elements of Java LinkedHashSet
import java.util.Iterator;
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<Integer> lhashSet = new LinkedHashSet<Integer>();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
Iterator itr = lhashSet.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
}
}
}
Remove all elements from Java LinkedHashSet
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<Integer> lhashSet = new LinkedHashSet<Integer>();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println(lhashSet);
lhashSet.clear();
System.out.println(lhashSet);
System.out.println(lhashSet.isEmpty());
}
}
Remove specified element from Java LinkedHashSet
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<Integer> lhashSet = new LinkedHashSet<Integer>();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println(lhashSet);
boolean blnRemoved = lhashSet.remove(new Integer("2"));
System.out.println(blnRemoved);
System.out.println(lhashSet);
}
}
Simple Java LinkedHashSet
import java.util.LinkedHashSet;
public class Main {
public static void main(String[] args) {
LinkedHashSet<Integer> lhashSet = new LinkedHashSet<Integer>();
lhashSet.add(new Integer("1"));
lhashSet.add(new Integer("2"));
lhashSet.add(new Integer("3"));
System.out.println(lhashSet);
}
}