使用put函數的變體處理TreeMap中的插入。 Java.util.TreeMap中有put()的兩個變體,本文都將進行討論。 1. put():它將指定的值與映射中的指定鍵相關聯。如果已經存在 key ,則對其進行更新將導致該 key 的更新。
參數: key:The key with which the value is to be associated. value: The value to be associated with the given key. 返回值: It returns the previously associated value with this key, or null if there was no mapping for key. Exception: Not Available.
// Java code demonstrating the working
// of put()
import java.io.*;
import java.util.*;
public class Put {
public static void main(String[] args)
{
// Declaring the tree map of Integer and String
TreeMap<String, Integer> tmp = new TreeMap<String, Integer>();
// assigning the values in the tree map
// using put()
tmp.put("one", 1);
tmp.put("two", 3);
// Printing initial TreeMap
System.out.println("The initial TreeMap is:" + tmp);
// Putting value at key two
// replaces the previous value if present
int z = tmp.put("two", 2);
// checking the previous value associated with "two"
System.out.println("The previous value with two is:" + z);
// prints the complete map
System.out.println("Updated TreeMap is:" + tmp);
}
}
輸出:
The initial TreeMap is:{one=1, two=3} The previous value with two is:3 Updated TreeMap is:{one=1, two=2}
2. putAll():它將所有映射從指定映射複製到給定映射,並在重複鍵的情況下覆蓋。
參數: map: The mappings to be stored. 返回值: Not Available. Exception: NullPointerException: Thrown if specified map is null, specified map contains a null key.
// Java code to demonstrate the working
// of putAll()
import java.io.*;
import java.util.*;
public class putAll {
public static void main(String[] args)
{
// Declaring the tree maps of Integer and String
TreeMap<String, Integer> tmp1 = new TreeMap<String, Integer>();
TreeMap<String, Integer> tmp2 = new TreeMap<String, Integer>();
// assigning the values in the tree map
// using put()
tmp1.put("two", 3);
tmp1.put("one", 1);
// assigning in 2nd TreeMap
tmp2.put("three", 3);
tmp2.put("two", 2);
System.out.println("First treemap values are:" + tmp1);
// use of putAll()
// Putting 2nd map in 1st map
tmp1.putAll(tmp2);
System.out.println("Values after modifying 1st treemap:" + tmp1);
}
}
輸出:
First treemap values are:{one=1, two=3} Values after modifying 1st treemap:{one=1, three=3, two=2}
異常
NullPointerException :如果Map包含空鍵或在putAll()中為空,則發生。
// Java code to demonstrate the Exception
// of putAll()
import java.io.*;
import java.util.*;
public class putAll {
public static void main(String[] args)
{
// Declaring the tree maps of Integer and String
TreeMap<String, Integer> tmp1 = new TreeMap<String, Integer>();
TreeMap<String, Integer> tmp2 = new TreeMap<String, Integer>();
// assigning the values in the tree map
// using put()
tmp1.put("two", 3);
tmp1.put("one", 1);
// assigning in 2nd TreeMap
tmp2.put("three", 3);
tmp2.put(null, 2);
System.out.println("First treemap values are:" + tmp1);
// use of putAll()
// Putting 2nd map in 1st map
tmp1.putAll(tmp2);
System.out.println("Values after modifying 1st treemap:" + tmp1);
}
}
運行時錯誤:
Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.put(TreeMap.java:563) at putAll.main(putAll.java:21)
實際應用:該函數具有存儲值並在需要時更新它們的函數。這種函數主要在目錄和詞典或記錄中有用。下麵的一小段代碼說明了合並數據庫以獲取遊戲中的最新分數。
// Java code to demonstrate the Application
// of putAll()
import java.io.*;
import java.util.*;
public class putAllAppli {
public static void main(String[] args)
{
// Declaring the tree maps of Integer and String
TreeMap<String, Integer> Score1 = new TreeMap<String, Integer>();
TreeMap<String, Integer> Score2 = new TreeMap<String, Integer>();
// Assigning Scores at Drinks break
Score1.put("Sachin", 40);
Score1.put("Rahul", 32);
Score1.put("Dhoni", 68);
System.out.println("The scores till Drinks break are:" + Score1);
// Assigning Scores at Lunch
Score2.put("Rahul", 67);
Score2.put("Dhoni", 102);
Score2.put("Raina", 10);
System.out.println("The scores at Lunch:" + Score2);
// use of putAll()
// Getting net score board
Score1.putAll(Score2);
System.out.println("Net scorecard is:" + Score1);
}
}
輸出:
The scores till Drinks break are:{Dhoni=68, Rahul=32, Sachin=40} The scores at Lunch:{Dhoni=102, Rahul=67, Raina=10} Net scorecard is:{Dhoni=102, Rahul=67, Raina=10, Sachin=40}
相關用法
- Java IdentityHashMap putAll()用法及代碼示例
- Java HashMap putAll()用法及代碼示例
- Java TreeMap putAll()用法及代碼示例
- Java WeakHashMap putall()用法及代碼示例
- Java Map putAll()用法及代碼示例
- Java EnumMap putAll(map)用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java AbstractMap putAll()用法及代碼示例
- Java SimpleBindings putAll()用法及代碼示例
- Java SortedMap putAll()用法及代碼示例
- Java Java.util.function.IntPredicate用法及代碼示例
- Java Java.util.concurrent.RecursiveTask用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
注:本文由純淨天空篩選整理自Shambhavi Singh 1大神的英文原創作品 Java.util.TreeMap.put() and putAll() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。