Для структур данных взгляните на java.util.TreeMap
для реализации Map на основе дерева и java.util.TreeSet
для реализации Set на основе дерева.Это стандартные реализации, найденные в API коллекций Java.
package com.mindprod.example;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import static java.lang.System.out;
/**
* Example use of java.util.TreeMap.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2010-02-25 initial version
* @see TestHashMap
* @since 2010-02-25
*/
public final class TestTreeMap
{
// --------------------------- main() method ---------------------------
/**
* Sample code to TEST TreeMap.
*
* @param args not used
*/
public static void main( String[] args )
{
// create a new HashMap
TreeMap<String, String> t = new TreeMap<String, String>( /* no size estimates needed */ );
// add some key/value pairs to the TreeMap
t.put( "WA", "Washington" );
t.put( "NY", "New York" );
t.put( "RI", "Rhode Island" );
t.put( "BC", "British Columbia" );
t.put( "NC", "North Carolina" );
t.put( "NE", "Nebraska" );
// look up a key in the TreeMap
String stateName = t.get( "NY" );
// prints "New York"
out.println( stateName );
out.println( "enumerate all the keys in the TreeMap, by key" );
// keySet gives you a Set, which is not a List.
// If you need something you can sort, use toArray.
// If you need a List, then use Arrays.asList.
for ( String key : t.keySet() )
{
String value = t.get( key );
// prints lines of the form NY New York
// in key order, unlike a HashMap
out.println( key + " " + value );
}
out.println( "enumerate all the values in the TreeMap, by key, note values out of order" );
// values gives you a Collection which is not a List.
// If you need something you can sort, use to Array.
// If you need a List, then use Arrays.asList.
for ( String value : t.values() )
{
// prints lines of the form New York
// in key order, unlike a HashMap
out.println( value );
}
out.println( "enumerate all the key/value Entries in the TreeMap, by key" );
// This gives you a Map of Entry items. This is not suitable for sorting.
for ( Map.Entry<String, String> entry : t.entrySet() )
{
// prints lines of the form NY=New York
// in key order, unlike a HashMap
out.println( "as Entry: " + entry );
// this does not require an expensive get lookup to find the value.
String key = entry.getKey();
String value = entry.getValue();
out.println( "separately: " + key + " " + value );
}
out.println( "extract the keys into an array" );
// actual type is a private nested static class TreeMap.KeySet
// This Set is not Serializable.
Set<String> justKeys = t.keySet();
// Use toArray that takes an skeleton String[] array,
// otherwise we end up with a useless Object[] instead of a String[].
final String[] keys = justKeys.toArray( new String[ justKeys.size() ] );
out.println( "extract values into an array, may contain duplicates unlike a Set." );
// the actual type is a private nested static class TreeMap.Values
// This Collection is not Serializable.
final Collection<String> justValues = t.values();
final String[] values = justValues.toArray( new String[ justValues.size() ] );
out.println( "extract key/value pair entries into an array." );
final Set<Map.Entry<String, String>> justEntries = t.entrySet();
@SuppressWarnings( "unchecked" ) final Map.Entry<String, String>[] keyValuePairs =
justEntries.toArray( new Map.Entry[ justEntries.size() ] );
// Infuriatingly, this generates an unchecked conversion warning message.
// Type erasure won't let us say:
// Map.Entry<String, String>[] keyValuePairs =
// justEntries.toArray ( new Map.Entry<String,String>[justEntries.size()] );
// There might be some clever way of using Class.asSubclass to mollify the compiler.
// There so many times when generics create more problems than they solve.
}
}
Вас также может заинтересовать эта ссылка