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

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

Текущая версия на 14:11, 31 мая 2010

NavigableSet: ceiling(T e)

 
import java.util.NavigableSet;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    String[] cities = { "A", "B", "C", "D", "E", "F" };
    NavigableSet<String> citiesSet = new TreeSet<String>();
    for (String city : cities)
      citiesSet.add(city);
    System.out.println(citiesSet.ceiling("A"));
  }
}





NavigableSet: descendingSet()

 

import java.util.NavigableSet;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    String[] cities = { "A", "B", "C", "D", "E", "F" };
    NavigableSet<String> citiesSet = new TreeSet<String>();
    for (String city : cities)
      citiesSet.add(city);
    for (String city : citiesSet.descendingSet())
      System.out.println("  " + city);
  }
}





NavigableSet: floor(T e)

 
import java.util.NavigableSet;
import java.util.TreeSet;
public class Main {
  
  public static void main(String[] args) {
    String[] cities = { "A", "B", "C", "D", "E", "F" };
    NavigableSet<String> citiesSet = new TreeSet<String>();
    for (String city : cities)
      citiesSet.add(city);

    System.out.println(citiesSet.floor("A"));
  }

}





NavigableSet: higher(T e)

 

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(3, 2, 4, 1, 5);
    NavigableSet<Integer> ns = new TreeSet<Integer>(list);
    System.out.println("Ascending order (default): " + ns);
    Iterator<Integer> descendingIterator = ns.descendingIterator();
    StringBuilder sb = new StringBuilder("Descending order: ");
    while (descendingIterator.hasNext()) {
      int m = descendingIterator.next();
      sb.append(m + " ");
    }
    System.out.println(sb);
    int greatest = ns.lower(3);
    System.out.println("Lower of 3 = " + greatest);
    int smallest = ns.higher(3);
    System.out.println("Higher of 3 = " + smallest);
  }
}





NavigableSet: lower(T e)

 

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
public class Main {
  public static void main(String[] args) {
    List<Integer> list = Arrays.asList(3, 2, 4, 1, 5);
    NavigableSet<Integer> ns = new TreeSet<Integer>(list);
    System.out.println("Ascending order (default): " + ns);
    Iterator<Integer> descendingIterator = ns.descendingIterator();
    StringBuilder sb = new StringBuilder("Descending order: ");
    while (descendingIterator.hasNext()) {
      int m = descendingIterator.next();
      sb.append(m + " ");
    }
    System.out.println(sb);
    int greatest = ns.lower(3);
    System.out.println("Lower of 3 = " + greatest);
    int smallest = ns.higher(3);
    System.out.println("Higher of 3 = " + smallest);
  }
}