Java by API/java.util/TreeSet — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:10, 31 мая 2010
Содержание
- 1 new TreeSet(Collection<?> c)
- 2 new TreeSet < E > ()
- 3 new TreeSet < E > (Comparator < ? super)
- 4 TreeSet: add(E o)
- 5 TreeSet: comparator()
- 6 TreeSet: first()
- 7 TreeSet: headSet(T toElement)
- 8 TreeSet: last()
- 9 TreeSet: remove(Object o)
- 10 TreeSet: subSet(T fromElement, T toElement)
- 11 TreeSet: tailSet(T fromElement)
new TreeSet(Collection<?> c)
import java.util.Arrays;
import java.util.TreeSet;
public class Main {
public static void main(String args[]) throws Exception {
String elements[] = { "A", "C", "D", "G", "F" };
TreeSet<String> set = new TreeSet<String>(Arrays.asList(elements));
System.out.println(set.headSet("D"));
System.out.println(set.tailSet(""));
}
}
new TreeSet < E > ()
/**
*Output:
[A, B, C, D, E, F]
*/
import java.util.TreeSet;
public class MainClass {
public static void main(String args[]) {
TreeSet<String> ts = new TreeSet<String>();
ts.add("C");
ts.add("A");
ts.add("B");
ts.add("E");
ts.add("F");
ts.add("D");
System.out.println(ts);
}
}
new TreeSet < E > (Comparator < ? super)
/**
*Output:
F E D C B A
*/
import java.util.ruparator;
import java.util.TreeSet;
class MyComparator implements Comparator<String> {
public int compare(String a, String b) {
String aStr, bStr;
aStr = a;
bStr = b;
return bStr.rupareTo(aStr);
}
// No need to override equals.
}
public class MainClass {
public static void main(String args[]) {
TreeSet<String> ts = new TreeSet<String>(new MyComparator());
ts.add("C");
ts.add("A");
ts.add("B");
ts.add("E");
ts.add("F");
ts.add("D");
for (String element : ts)
System.out.print(element + " ");
System.out.println();
}
}
TreeSet: add(E o)
/**
*Output:
[A, B, C, D, E, F]
*/
import java.util.TreeSet;
public class MainClass {
public static void main(String args[]) {
TreeSet<String> ts = new TreeSet<String>();
ts.add("C");
ts.add("A");
ts.add("B");
ts.add("E");
ts.add("F");
ts.add("D");
System.out.println(ts);
}
}
TreeSet: comparator()
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String args[]) throws Exception {
String elements[] = { "A", "C", "D", "G", "F" };
Set<String> set = new TreeSet<String>(Collections.reverseOrder());
for (int i = 0, n = elements.length; i < n; i++) {
set.add(elements[i]);
}
System.out.println(set);
System.out.println(((TreeSet) set).ruparator());
}
}
TreeSet: first()
import java.util.Arrays;
import java.util.TreeSet;
public class Main {
public static void main(String args[]) throws Exception {
String elements[] = { "A", "C", "D", "G", "F" };
TreeSet<String> set = new TreeSet<String>(Arrays.asList(elements));
System.out.println(set);
System.out.println(set.first());
System.out.println(set.last());
}
}
TreeSet: headSet(T toElement)
import java.util.Arrays;
import java.util.TreeSet;
public class Main {
public static void main(String args[]) throws Exception {
String elements[] = { "A", "C", "D", "G", "F" };
TreeSet<String> set = new TreeSet<String>(Arrays.asList(elements));
System.out.println(set.headSet("D"));
System.out.println(set.tailSet(""));
}
}
TreeSet: last()
import java.util.Arrays;
import java.util.TreeSet;
public class Main {
public static void main(String args[]) throws Exception {
String elements[] = { "A", "C", "D", "G", "F" };
TreeSet<String> set = new TreeSet<String>(Arrays.asList(elements));
System.out.println(set);
System.out.println(set.first());
System.out.println(set.last());
}
}
TreeSet: remove(Object o)
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> tSet = new TreeSet<Integer>();
System.out.println("Size of TreeSet : " + tSet.size());
tSet.add(new Integer("1"));
tSet.add(new Integer("2"));
tSet.add(new Integer("3"));
System.out.println(tSet.size());
// remove one element from TreeSet using remove method
tSet.remove(new Integer("1"));
System.out.println("Size of TreeSet after removal : " + tSet.size());
}
}
TreeSet: subSet(T fromElement, T toElement)
import java.util.Arrays;
import java.util.TreeSet;
public class Main {
public static void main(String args[]) throws Exception {
String elements[] = { "A", "C", "D", "G", "F" };
TreeSet<String> set = new TreeSet<String>(Arrays.asList(elements));
System.out.println(set.subSet("C", "F"));
}
}
TreeSet: tailSet(T fromElement)
import java.util.Arrays;
import java.util.TreeSet;
public class Main {
public static void main(String args[]) throws Exception {
String elements[] = { "A", "C", "D", "G", "F" };
TreeSet<String> set = new TreeSet<String>(Arrays.asList(elements));
System.out.println(set.headSet("D"));
System.out.println(set.tailSet(""));
}
}