Java by API/java.util/SortedSet

Материал из Java эксперт
Версия от 14:10, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

SortedSet: add(String e)

  
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class Main {
  public static void main(String[] argv) throws Exception {
    SortedSet<String> set = new TreeSet<String>();
    set.add("b");
    set.add("c");
    set.add("a");
    Iterator it = set.iterator();
    while (it.hasNext()) {
      Object element = it.next();
    }
    String[] array = (String[]) set.toArray(new String[set.size()]);
  }
}





SortedSet: headSet(E toElement)

 
/*
 Output:
[I, P]
[E, G]
[E, G, I]
[P]
[I, P]
[I]
[]
 * */
import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;
public class MainClass {
  public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("I"));
    System.out.println(set.headSet("I"));
    System.out.println(set.headSet("I\0"));
    System.out.println(set.tailSet("I\0"));
    System.out.println(set.subSet("I", "P\0"));
    System.out.println(set.subSet("I", "I\0"));
    System.out.println(set.subSet("I", "I"));
  }
}





SortedSet: iterator()

  
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class Main {
  public static void main(String[] argv) throws Exception {
    SortedSet<String> set = new TreeSet<String>();
    set.add("b");
    set.add("c");
    set.add("a");
    Iterator it = set.iterator();
    while (it.hasNext()) {
      Object element = it.next();
    }
    String[] array = (String[]) set.toArray(new String[set.size()]);
  }
}





SortedSet: subSet(E fromElement, E toElement)

 
/*
 Output:
[I, P]
[E, G]
[E, G, I]
[P]
[I, P]
[I]
[]
 * */
import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;
public class MainClass {
  public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("I"));
    System.out.println(set.headSet("I"));
    System.out.println(set.headSet("I\0"));
    System.out.println(set.tailSet("I\0"));
    System.out.println(set.subSet("I", "P\0"));
    System.out.println(set.subSet("I", "I\0"));
    System.out.println(set.subSet("I", "I"));
  }
}





SortedSet: tailSet(E fromElement)

 
/*
 Output:
[I, P]
[E, G]
[E, G, I]
[P]
[I, P]
[I]
[]
 * */
import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;
public class MainClass {
  public static void main(String args[]) throws Exception {
    String elements[] = { "I", "P", "E", "G", "P" };
    SortedSet set = new TreeSet(Arrays.asList(elements));
    System.out.println(set.tailSet("I"));
    System.out.println(set.headSet("I"));
    System.out.println(set.headSet("I\0"));
    System.out.println(set.tailSet("I\0"));
    System.out.println(set.subSet("I", "P\0"));
    System.out.println(set.subSet("I", "I\0"));
    System.out.println(set.subSet("I", "I"));
  }
}