Java TreeMap putAll() 方法



描述

Java TreeMap putAll(Map<? extends K,? extends V> map) 方法用於將指定對映中的所有映射覆制到此對映。這些對映將替換此對映中當前在指定對映中的任何鍵的任何對映。

宣告

以下是 java.util.TreeMap.putAll() 方法的宣告。

public void putAll(Map<? extends K,? extends V> map)

引數

map − 這是要儲存在此對映中的對映。

返回值

異常

  • ClassCastException − 如果指定對映中鍵或值的類阻止將其儲存在此對映中,則丟擲此異常。

  • NullPointerException − 如果指定的對映為空,或者指定的對映包含空鍵並且此對映不允許空鍵,則丟擲此異常。

向 Integer,Integer 對的 TreeMap 新增多個鍵值對示例

以下示例演示了 Java TreeMap putAll() 方法的使用,以在單個語句中向此對映中新增/更新多個鍵值對映。我們建立了兩個 Integer,Integer 對的 TreeMap 物件。然後使用 put() 方法向兩個對映新增一些條目,列印第一個對映,然後使用 putAll() 方法將第二個對映的所有條目新增到第一個對映,並列印對映以驗證更改。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating tree maps 
      TreeMap<Integer, Integer> treemap = new TreeMap<>();
      TreeMap<Integer, Integer> treemap_putall = new TreeMap<>();

      // populating tree map
      treemap.put(2, 2);
      treemap.put(1, 1);
      treemap.put(3, 3);
      treemap.put(6, 6);
      treemap.put(5, 5);

      treemap_putall.put(1, 111); 
      treemap_putall.put(2, 222);
      treemap_putall.put(7, 777);      

      System.out.println("Value before modification: "+ treemap);

      // Putting 2nd map in 1st map
      treemap.putAll(treemap_putall);

      System.out.println("Value after modification: "+ treemap);
   }     
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Value before modification: {1=1, 2=2, 3=3, 5=5, 6=6}
Value after modification: {1=111, 2=222, 3=3, 5=5, 6=6, 7=777}

向 Integer,String 對的 TreeMap 新增多個鍵值對示例

以下示例演示了 Java TreeMap putAll() 方法的使用,以在單個語句中向此對映中新增/更新多個鍵值對映。我們建立了兩個 Integer,String 對的 TreeMap 物件。然後使用 put() 方法向兩個對映新增一些條目,列印第一個對映,然後使用 putAll() 方法將第二個對映的所有條目新增到第一個對映,並列印對映以驗證更改。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating tree maps 
      TreeMap<Integer, String> treemap = new TreeMap<>();
      TreeMap<Integer, String> treemap_putall = new TreeMap<>();

      // populating tree map
      treemap.put(2, "two");
      treemap.put(1, "one");
      treemap.put(3, "three");
      treemap.put(6, "six");
      treemap.put(5, "five");

      treemap_putall.put(1, "111"); 
      treemap_putall.put(2, "222");
      treemap_putall.put(7, "777");      

      System.out.println("Value before modification: "+ treemap);

      // Putting 2nd map in 1st map
      treemap.putAll(treemap_putall);

      System.out.println("Value after modification: "+ treemap);
   }     
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Value before modification: {1=one, 2=two, 3=three, 5=five, 6=six}
Value after modification: {1=111, 2=222, 3=three, 5=five, 6=six, 7=777}

向 Integer,Object 對的 TreeMap 新增多個鍵值對示例

以下示例演示了 Java TreeMap putAll() 方法的使用,以在單個語句中向此對映中新增/更新多個鍵值對映。我們建立了兩個 Integer,Student 對的 TreeMap 物件。然後使用 put() 方法向兩個對映新增一些條目,列印第一個對映,然後使用 putAll() 方法將第二個對映的所有條目新增到第一個對映,並列印對映以驗證更改。

package com.tutorialspoint;

import java.util.TreeMap;

public class TreeMapDemo {
   public static void main(String[] args) {

      // creating tree maps 
      TreeMap<Integer, Student> treemap = new TreeMap<>();
      TreeMap<Integer, Student> treemap_putall = new TreeMap<>();

      // populating tree map
      treemap.put(2, new Student(2, "Robert"));
      treemap.put(1, new Student(1, "Julie"));  
      treemap.put(3, new Student(3, "Adam"));
      treemap.put(6, new Student(6, "Julia"));
      treemap.put(5, new Student(5, "Tom"));

      treemap_putall.put(1, new Student(111, "Julie")); 
      treemap_putall.put(2, new Student(222, "Robert"));
      treemap_putall.put(7, new Student(777, "Jose"));      

      System.out.println("Value before modification: "+ treemap);

      // Putting 2nd map in 1st map
      treemap.putAll(treemap_putall);

      System.out.println("Value after modification: "+ treemap);
   }     
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   @Override
   public boolean equals(Object obj) {
      if(obj == null) return false;
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Value before modification: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ], 5=[ 5, Tom ], 6=[ 6, Julia ]}
Value after modification: {1=[ 111, Julie ], 2=[ 222, Robert ], 3=[ 3, Adam ], 5=[ 5, Tom ], 6=[ 6, Julia ], 7=[ 777, Jose ]}
java_util_treemap.htm
廣告

© . All rights reserved.