當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ConcurrentSkipListMap put()用法及代碼示例


java.util.concurrent.ConcurrentSkipListMap的put()方法是Java中的內置函數,它將指定值與該映射中的指定鍵相關聯。如果該映射先前包含該鍵的映射,則替換舊值。

用法:

public V put(K key, V value)

參數:該函數接受兩個強製性參數:


  • key它指定與指定值關聯的鍵
  • value:它指定與指定鍵關聯的值。

返回值:該函數返回與指定鍵關聯的先前值。如果指定鍵沒有映射,則此方法返回null。

以下示例程序旨在說明上述方法:

示例1:

// Java Program Demonstrate put() 
// method of ConcurrentSkipListMap 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Initializing the map 
        ConcurrentSkipListMap<Integer, Integer> 
            mpp = new ConcurrentSkipListMap<Integer, 
                                            Integer>(); 
  
        // Adding elements to this map 
        for (int i = 1; i <= 5; i++) 
            mpp.put(i, i); 
  
        // put() operation on the map 
        System.out.println("After put(): "
                           + mpp); 
    } 
}
輸出:
After put(): {1=1, 2=2, 3=3, 4=4, 5=5}

示例2:

// Java Program Demonstrate put() 
// method of ConcurrentSkipListMap 
  
import java.util.concurrent.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Initializing the map 
        ConcurrentSkipListMap<Integer, Integer> 
            mpp = new ConcurrentSkipListMap<Integer, 
                                            Integer>(); 
  
        // Adding elements to this map 
        for (int i = 1; i <= 9; i++) 
            mpp.put(i, i); 
  
        // put() operation on the map 
        System.out.println("After put(): "
                           + mpp); 
    } 
}
輸出:
After put(): {1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}

參考: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/ConcurrentSkipListMap.html#put-K-V-



相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 ConcurrentSkipListMap put() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。