Java/Collections Data Structure/TreeMap

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

Check if a particular key exists in Java TreeMap

 
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String,String>();
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");
    System.out.println(treeMap.containsKey("1"));
  }
}





Check if a particular value exists in Java TreeMap

 

import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");
    System.out.println(treeMap.containsValue("Three"));
  }
}





Get Head Map from Java TreeMap

 
import java.util.SortedMap;
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String,String> treeMap = new TreeMap<String,String>();
    treeMap.put("1", "One");
    treeMap.put("3", "Three");
    treeMap.put("2", "Two");
    treeMap.put("5", "Five");
    treeMap.put("4", "Four");
    SortedMap sortedMap = treeMap.headMap("3");
    System.out.println("Head Map Contains : " + sortedMap);
  }
}





Get lowest and highest key stored in Java TreeMap

 
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");    
    treeMap.put("4", "Four");
    treeMap.put("5", "Five");
    System.out.println("Lowest key: " + treeMap.firstKey());
    System.out.println("Highest key: " + treeMap.lastKey());
  }
}





Get Set view of Keys from Java TreeMap

 
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Set;
public class Main {
  public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    treeMap.put("1", "One");
    treeMap.put("3", "Three");
    treeMap.put("2", "Two");
    Set st = treeMap.keySet();
    Iterator itr = st.iterator();
    while (itr.hasNext()){
      System.out.println(itr.next());
    }
    st.remove("1");
    System.out.println(treeMap.containsKey("1"));
  }
}





Get Size of Java TreeMap

 
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    System.out.println("Size of TreeMap : " + treeMap.size());
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");
    System.out.println("Size of TreeMap after addition : " + treeMap.size());
    treeMap.remove("2");
    System.out.println("Size of TreeMap after removal : " + treeMap.size());
  }
}





Get Sub Map from Java TreeMap

 
import java.util.SortedMap;
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");
    treeMap.put("4", "Four");
    treeMap.put("5", "Five");    
    SortedMap sortedMap = treeMap.subMap("2", "5");
    System.out.println("SortedMap Contains : " + sortedMap);
  }
}





Get Synchronized Map from Java TreeMap

 
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap treeMap = new TreeMap();
    Map map = Collections.synchronizedMap(treeMap);
  }
}





Get Tail Map from Java TreeMap

 
import java.util.SortedMap;
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String,String>();
    treeMap.put("1", "One");
    treeMap.put("3", "Three");
    treeMap.put("2", "Two");
    treeMap.put("5", "Five");
    treeMap.put("4", "Four");
    SortedMap sortedMap = treeMap.tailMap("2");
    System.out.println("Tail Map Contains : " + sortedMap);
  }
}





Iterate through the values of Java TreeMap

 
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String,String>();
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");
    Collection c = treeMap.values();
    Iterator itr = c.iterator();
    while (itr.hasNext()){
      System.out.println(itr.next());
    }
  }
}





Read file content and save to a TreeMap

 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class LogTest {
  public static void main(String[] args) throws IOException {
    String inputfile = args[0];
    String outputfile = args[1];
    Map<String, Integer> map = new TreeMap<String, Integer>();
    Scanner scanner = new Scanner(new File(inputfile));
    while (scanner.hasNext()) {
      String word = scanner.next();
      Integer count = map.get(word);
      count = (count == null ? 1 : count + 1);
      map.put(word, count);
    }
    scanner.close();
    List<String> keys = new ArrayList<String>(map.keySet());
    Collections.sort( keys );
    PrintWriter out = new PrintWriter(new FileWriter(outputfile));
    for (String key : keys)
      out.println(key + " : " + map.get(key));
    out.close();
  }
}





Remove all values from Java TreeMap

 
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String,String> treeMap = new TreeMap<String,String>();
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");
    treeMap.clear();
    System.out.println(treeMap.size());
  }
}





Remove value from Java TreeMap

 
import java.util.TreeMap;
public class Main {
  public static void main(String[] args) {
    TreeMap<String,String> treeMap = new TreeMap<String,String>();
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");
    Object obj = treeMap.remove("2");
    System.out.println(obj + " Removed from TreeMap");
  }
}





TreeMap<String, Double>

  
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
class TreeMapDemo {
  public static void main(String args[]) {
    TreeMap<String, Double> tm = new TreeMap<String, Double>();
    tm.put("A", new Double(3.34));
    tm.put("B", new Double(1.22));
    tm.put("C", new Double(1.00));
    tm.put("D", new Double(9.22));
    tm.put("E", new Double(-1.08));
    Set<Map.Entry<String, Double>> set = tm.entrySet();
    for (Map.Entry<String, Double> me : set) {
      System.out.print(me.getKey() + ": ");
      System.out.println(me.getValue());
    }
    double balance = tm.get("A");
    tm.put("B", balance + 1000);
    System.out.println(tm.get("A"));
  }
}





Use Generic TreeMap to store Integer as key and String as value

 
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapExample {
  public static void main(String[] args) {
    Map<Integer, String> map = new TreeMap<Integer, String>();
    // Add Items to the TreeMap
    map.put(new Integer(1), "One");
    map.put(new Integer(2), "Two");
    map.put(new Integer(3), "Three");
    map.put(new Integer(4), "Four");
    map.put(new Integer(5), "Five");
    map.put(new Integer(6), "Six");
    map.put(new Integer(7), "Seven");
    map.put(new Integer(8), "Eight");
    map.put(new Integer(9), "Nine");
    map.put(new Integer(10), "Ten");
    // Use iterator to display the keys and associated values
    System.out.println("Map Values Before: ");
    Set keys = map.keySet();
    for (Iterator i = keys.iterator(); i.hasNext();) {
      Integer key = (Integer) i.next();
      String value = (String) map.get(key);
      System.out.println(key + " = " + value);
    }
    // Remove the entry with key 6
    System.out.println("\nRemove element with key 6");
    map.remove(new Integer(6));
    // Use iterator to display the keys and associated values
    System.out.println("\nMap Values After: ");
    keys = map.keySet();
    for (Iterator i = keys.iterator(); i.hasNext();) {
      Integer key = (Integer) i.next();
      String value = (String) map.get(key);
      System.out.println(key + " = " + value);
    }
  }
}