Java Tutorial/Collections/NavigableSet

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

Create TreeSet from String array

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





For each loop with generic NavigableSet, created from TreeSet

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

    for (String city : citiesSet)
      System.out.println("  " + city);
  }
}





NavigableSet by TreeSet

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

    for (String city : citiesSet)
      System.out.println("  " + city);
  }
}





NavigableSet.ceiling

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

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

}





NavigableSet.floor

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

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

}





NavigableSet.higher

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

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

}





NavigableSet.lower

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

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

}





The NavigableSet Interface

The NavigableSet interface is a subinterface of SortedSet that provides methods for searching for elements.

java.util.TreeSet is an implementation of NavigableSet.



import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
public class NavigableSetDemo {
  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);
  }
}



Ascending order (default): [1, 2, 3, 4, 5]
Descending order: 5 4 3 2 1 
Lower of 3 = 2
Higher of 3 = 4