Java by API/java.util/TreeMap

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

new TreeMap < K, V > ()

   <source lang="java">
 

/**

*Output: 
D: 99.22
A: 3434.34
C: 1378.0
B: 123.22
E: -19.08
B"s new balance: 1123.22
*/

import java.util.Map; import java.util.Set; import java.util.TreeMap; public class MainClass {

 public static void main(String args[]) {
   TreeMap<String, Double> tm = new TreeMap<String, Double>();
   tm.put("A", new Double(3434.34));
   tm.put("B", new Double(123.22));
   tm.put("C", new Double(1378.00));
   tm.put("D", new Double(99.22));
   tm.put("E", new Double(-19.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());
   }
   System.out.println();
   double balance = tm.get("B");
   tm.put("B", balance + 1000);
   System.out.println("B"s new balance: " + tm.get("B"));
 }

}


 </source>
   
  
 
  



TreeMap: entrySet()

   <source lang="java">
 

/**

*Output: 
D: 99.22
A: 3434.34
C: 1378.0
B: 123.22
E: -19.08
B"s new balance: 1123.22
*/

import java.util.Map; import java.util.Set; import java.util.TreeMap; public class MainClass {

 public static void main(String args[]) {
   TreeMap<String, Double> tm = new TreeMap<String, Double>();
   tm.put("A", new Double(3434.34));
   tm.put("B", new Double(123.22));
   tm.put("C", new Double(1378.00));
   tm.put("D", new Double(99.22));
   tm.put("E", new Double(-19.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());
   }
   System.out.println();
   double balance = tm.get("B");
   tm.put("B", balance + 1000);
   System.out.println("B"s new balance: " + tm.get("B"));
 }

}


 </source>
   
  
 
  



TreeMap: firstKey()

   <source lang="java">
 

/*

Output:

Virginia, New York, Massachusetts

* */

import java.util.TreeMap; public class MainClass {

 public static void main(String args[]) {
   TreeMap map = new TreeMap();
   map.put("Virginia", "Richmond");
   map.put("Massachusetts", "Boston");
   map.put("New York", "Albany");
   map.put("Maryland", "Annapolis");
   if (!map.isEmpty()) {
     Object last = map.lastKey();
     boolean first = true;
     do {
       if (!first) {
         System.out.print(", ");
       }
       System.out.print(last);
       last = map.headMap(last).lastKey();
       first = false;
     } while (last != map.firstKey());
     System.out.println();
   }
 }

}


 </source>
   
  
 
  



TreeMap: get(K k)

   <source lang="java">
 

/**

*Output: 
D: 99.22
A: 3434.34
C: 1378.0
B: 123.22
E: -19.08
B"s new balance: 1123.22
*/

import java.util.Map; import java.util.Set; import java.util.TreeMap; public class MainClass {

 public static void main(String args[]) {
   TreeMap<String, Double> tm = new TreeMap<String, Double>();
   tm.put("A", new Double(3434.34));
   tm.put("B", new Double(123.22));
   tm.put("C", new Double(1378.00));
   tm.put("D", new Double(99.22));
   tm.put("E", new Double(-19.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());
   }
   System.out.println();
   double balance = tm.get("B");
   tm.put("B", balance + 1000);
   System.out.println("B"s new balance: " + tm.get("B"));
 }

}


 </source>
   
  
 
  



TreeMap: headMap(K toKey)

   <source lang="java">
 

/*

Output:

Virginia, New York, Massachusetts

* */

import java.util.TreeMap; public class MainClass {

 public static void main(String args[]) {
   TreeMap map = new TreeMap();
   map.put("Virginia", "Richmond");
   map.put("Massachusetts", "Boston");
   map.put("New York", "Albany");
   map.put("Maryland", "Annapolis");
   if (!map.isEmpty()) {
     Object last = map.lastKey();
     boolean first = true;
     do {
       if (!first) {
         System.out.print(", ");
       }
       System.out.print(last);
       last = map.headMap(last).lastKey();
       first = false;
     } while (last != map.firstKey());
     System.out.println();
   }
 }

}


 </source>
   
  
 
  



TreeMap: headMap(K toKey, boolean inclusive)

   <source lang="java">
 

import java.util.TreeMap; public class Main {

 public static void main(String[] a) {
   TreeMap<String,String> map = new TreeMap<String,String>();
   map.put("key1", "value1");
   map.put("key2", "value2");
   map.put("key3", "value3");
   if (!map.isEmpty()) {
     String last = map.lastKey();
     boolean first = true;
     do {
       if (!first) {
         System.out.print(", ");
       }
       System.out.print(last);
       last = map.headMap(last, true).lastKey();
       first = false;
     } while (last != map.firstKey());
     System.out.println();
   }
 }

}


 </source>
   
  
 
  



TreeMap: higherKey(K key)

   <source lang="java">
 

import java.util.TreeMap; public class Main {

 public static void main(String[] args) {
   TreeMap<Integer, Product> db = new TreeMap<Integer, Product>();
   db.put(1000, new Product("D", 350));
   db.put(1011, new Product("p", 15.75));
   db.put(1102, new Product("M", 8.50));
   db.put(2023, new Product("A", 150));
   db.put(2034, new Product("T", 9.99));
   System.out.println(db.subMap(1000, 1999) + "\n");
   System.out.println(db.tailMap(1011) + "\n");
   System.out.println(db.headMap(2023));
   System.out.println("First key higher than 2034: " + db.higherKey(2034));
   System.out.println("First key lower than 2034: " + db.lowerKey(2034));
 }

} class Product {

 String desc;
 double price;
 Product(String desc, double price) {
   this.desc = desc;
   this.price = price;
 }
 public String toString() {
   return "Description=" + desc + ", Price=" + price;
 }

}


 </source>
   
  
 
  



TreeMap: lastKey()

   <source lang="java">
 

/*

Output:

Virginia, New York, Massachusetts

* */

import java.util.TreeMap; public class MainClass {

 public static void main(String args[]) {
   TreeMap map = new TreeMap();
   map.put("Virginia", "Richmond");
   map.put("Massachusetts", "Boston");
   map.put("New York", "Albany");
   map.put("Maryland", "Annapolis");
   if (!map.isEmpty()) {
     Object last = map.lastKey();
     boolean first = true;
     do {
       if (!first) {
         System.out.print(", ");
       }
       System.out.print(last);
       last = map.headMap(last).lastKey();
       first = false;
     } while (last != map.firstKey());
     System.out.println();
   }
 }

}


 </source>
   
  
 
  



TreeMap: lowerKey(K key)

   <source lang="java">
 

import java.util.TreeMap; public class Main {

 public static void main(String[] args) {
   TreeMap<Integer, Product> db = new TreeMap<Integer, Product>();
   db.put(1000, new Product("D", 350));
   db.put(1011, new Product("p", 15.75));
   db.put(1102, new Product("M", 8.50));
   db.put(2023, new Product("A", 150));
   db.put(2034, new Product("T", 9.99));
   System.out.println(db.subMap(1000, 1999) + "\n");
   System.out.println(db.tailMap(1011) + "\n");
   System.out.println(db.headMap(2023));
   System.out.println("First key higher than 2034: " + db.higherKey(2034));
   System.out.println("First key lower than 2034: " + db.lowerKey(2034));
 }

} class Product {

 String desc;
 double price;
 Product(String desc, double price) {
   this.desc = desc;
   this.price = price;
 }
 public String toString() {
   return "Description=" + desc + ", Price=" + price;
 }

}


 </source>
   
  
 
  



TreeMap: put(K k, V v)

   <source lang="java">
 

/**

*Output: 
D: 99.22
A: 3434.34
C: 1378.0
B: 123.22
E: -19.08
B"s new balance: 1123.22
*/

import java.util.Map; import java.util.Set; import java.util.TreeMap; public class MainClass {

 public static void main(String args[]) {
   TreeMap<String, Double> tm = new TreeMap<String, Double>();
   tm.put("A", new Double(3434.34));
   tm.put("B", new Double(123.22));
   tm.put("C", new Double(1378.00));
   tm.put("D", new Double(99.22));
   tm.put("E", new Double(-19.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());
   }
   System.out.println();
   double balance = tm.get("B");
   tm.put("B", balance + 1000);
   System.out.println("B"s new balance: " + tm.get("B"));
 }

}


 </source>
   
  
 
  



TreeMap: size()

   <source lang="java">

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());
 }

}

 </source>
   
  
 
  



TreeMap: subMap(K fromKey, K toKey)

   <source lang="java">
 

import java.util.TreeMap; public class Main {

 public static void main(String[] args) {
   TreeMap<Integer, Product> db = new TreeMap<Integer, Product>();
   db.put(1000, new Product("D", 350));
   db.put(1011, new Product("p", 15.75));
   db.put(1102, new Product("M", 8.50));
   db.put(2023, new Product("A", 150));
   db.put(2034, new Product("T", 9.99));
   System.out.println(db.subMap(1000, 1999) + "\n");
   System.out.println(db.tailMap(1011) + "\n");
   System.out.println(db.headMap(2023));
   System.out.println("First key higher than 2034: " + db.higherKey(2034));
   System.out.println("First key lower than 2034: " + db.lowerKey(2034));
 }

} class Product {

 String desc;
 double price;
 Product(String desc, double price) {
   this.desc = desc;
   this.price = price;
 }
 public String toString() {
   return "Description=" + desc + ", Price=" + price;
 }

}


 </source>
   
  
 
  



TreeMap: tailMap(T fromKey)

   <source lang="java">
 

import java.util.TreeMap; public class Main {

 public static void main(String[] args) {
   TreeMap<Integer, Product> db = new TreeMap<Integer, Product>();
   db.put(1000, new Product("D", 350));
   db.put(1011, new Product("p", 15.75));
   db.put(1102, new Product("M", 8.50));
   db.put(2023, new Product("A", 150));
   db.put(2034, new Product("T", 9.99));
   System.out.println(db.subMap(1000, 1999) + "\n");
   System.out.println(db.tailMap(1011) + "\n");
   System.out.println(db.headMap(2023));
   System.out.println("First key higher than 2034: " + db.higherKey(2034));
   System.out.println("First key lower than 2034: " + db.lowerKey(2034));
 }

} class Product {

 String desc;
 double price;
 Product(String desc, double price) {
   this.desc = desc;
   this.price = price;
 }
 public String toString() {
   return "Description=" + desc + ", Price=" + price;
 }

}


 </source>
   
  
 
  



TreeMap: values()

   <source lang="java">

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());
   }
 }

}

 </source>