Java by API/java.util/SortedSet — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

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"));
  }
}