| |
集, HashSet和TreeSet |
|
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String args[]) {
Set<String> hs = new HashSet<String>();
hs.add("one");
hs.add("two");
hs.add("three");
System.out.println("Here is the HashSet: " + hs);
if (!hs.add("three"))
System.out.println("Attempt to add duplicate. " + "Set is unchanged: " + hs);
TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(8);
ts.add(19);
ts.add(-2);
ts.add(3);
System.out.println(ts);
System.out.println("First element in ts: " + ts.first());
System.out.println("Last element in ts: " + ts.last());
System.out.println("First element > 15: " + ts.higher(15));
System.out.println("First element < 15: " + ts.lower(15));
}
}
|
|
|
Related examples in the same category |
|