/* * [TestTreeMap.java] * * Summary: Example use of java.util.TreeMap. * * Copyright: (c) 2010-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2010-02-25 initial version */ 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.*; /** * 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 { /** * Sample code to TEST TreeMap. * * @param args not used */ public static void main( String[] args ) { // create a new TreeMap TreeMap t = new TreeMap<>( /* 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. // prints lines of the form New York // in key order, unlike a HashMap // lamba to println each String in t.values t.values().forEach( out::println ); 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 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 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 justValues = t.values(); final String[] values = justValues.toArray( new String[ justValues.size() ] ); out.println( "extract key/value pair entries into an array." ); final Set> justEntries = t.entrySet(); @SuppressWarnings( "unchecked" ) final Map.Entry[] 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[] keyValuePairs = // justEntries.toArray ( new Map.Entry[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. } }