Java TreeMap put() 方法



描述

Java TreeMap put(K key,V value) 方法用於在此對映中將指定的值與指定的鍵關聯。如果對映先前包含該鍵的對映,則舊值將被替換。

宣告

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

public V put(K key,V value)

引數

  • key − 這是要與其關聯指定值的鍵。

  • value − 這是要與指定鍵關聯的值。

返回值

方法呼叫返回與鍵關聯的上一個值,如果鍵沒有對映,則返回 null。

異常

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

以下示例演示了 Java TreeMap put() 方法的使用,用於在此對映中新增/更新鍵值對映。我們建立了一個 Integer,Integer 對的 TreeMap 物件。然後使用 put() 方法添加了一些條目,列印對映,然後使用 put() 更新鍵的值,並再次列印對映以檢視更新條目的效果。

package com.tutorialspoint;

import java.util.TreeMap;

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

      // creating tree map 
      TreeMap<Integer, Integer> treemap = 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);   

      // Putting value at key 3
      System.out.println("Value before modification: "+ treemap);            
      System.out.println("Value returned: "+ treemap.put(3,7));      
      System.out.println("Value after modification: "+ treemap);
   }     
}

輸出

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

Value before modification: {1=1, 2=2, 3=3, 5=5, 6=6}
Value returned: 3
Value after modification: {1=1, 2=2, 3=7, 5=5, 6=6}

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

以下示例演示了 Java TreeMap put() 方法的使用,用於在此對映中新增/更新鍵值對映。我們建立了一個 Integer,String 對的 TreeMap 物件。然後使用 put() 方法添加了一些條目,列印對映,然後使用 put() 更新鍵的值,並再次列印對映以檢視更新條目的效果。

package com.tutorialspoint;

import java.util.TreeMap;

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

      // creating tree map 
      TreeMap<Integer, String> treemap = 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");   

      // Putting value at key 3
      System.out.println("Value before modification: "+ treemap);            
      System.out.println("Value returned: "+ treemap.put(3,"TP"));      
      System.out.println("Value after modification: "+ treemap);
   }     
}

輸出

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

Value before modification: {1=one, 2=two, 3=three, 5=five, 6=six}
Value returned: three
Value after modification: {1=one, 2=two, 3=TP, 5=five, 6=six}

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

以下示例演示了 Java TreeMap put() 方法的使用,用於在此對映中新增/更新鍵值對映。我們建立了一個 Integer,Student 對的 TreeMap 物件。然後使用 put() 方法添加了一些條目,列印對映,然後使用 put() 更新鍵的值,並再次列印對映以檢視更新條目的效果。

package com.tutorialspoint;

import java.util.TreeMap;

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

      // creating tree map 
      TreeMap<Integer, Student> treemap = 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")); 

      // Putting value at key 3
      System.out.println("Value before modification: "+ treemap);            
      System.out.println("Value returned: "+ treemap.put(3, new Student(7, "Alfred")));     
      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 returned: [ 3, Adam ]
Value after modification: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 7, Alfred ], 5=[ 5, Tom ], 6=[ 6, Julia ]}
java_util_treemap.htm
廣告