日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 默认站点!

默认站点

當(dāng)前位置: 首頁 >

java treemap用法_Java TreeMap put()用法及代码示例

發(fā)布時間:2023/11/27 30 豆豆
默认站点 收集整理的這篇文章主要介紹了 java treemap用法_Java TreeMap put()用法及代码示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

TreeMap的java.util.TreeMap.put()方法用于將映射插入Map。這意味著我們可以將特定的鍵及其映射到的值插入到特定的映射中。如果傳遞了現(xiàn)有鍵,則以前的值將被新值替換。如果傳遞了新的配對,則該配對將整體插入。

用法:

Tree_Map.put(key, value)

參數(shù):該方法有兩個參數(shù),它們都是TreeMap的Object類型。

key:這是指需要插入Map中進(jìn)行映射的關(guān)鍵元素。

value:這是指以上鍵將映射到的值。

返回值:如果傳遞了現(xiàn)有鍵,則返回先前的值。如果傳遞了新對,則返回NULL。

以下程序用于說明java.util.TreeMap.put()方法的用法:

示例1:傳遞現(xiàn)有 key 時。

// Java code to illustrate the put() method

import java.util.*;

public class Tree_Map_Demo {

public static void main(String[] args)

{

// Creating an empty TreeMap

TreeMap tree_map = new TreeMap();

// Mapping string values to int keys

tree_map.put(10, "Geeks");

tree_map.put(15, "4");

tree_map.put(20, "Geeks");

tree_map.put(25, "Welcomes");

tree_map.put(30, "You");

// Displaying the TreeMap

System.out.println("Initial Mappings are: " + tree_map);

// Inserting existing key along with new value

String returned_value = (String)tree_map.put(20, "All");

// Verifying the returned value

System.out.println("Returned value is: " + returned_value);

// Displayin the new map

System.out.println("New map is: " + tree_map);

}

}

輸出:

Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}

Returned value is: Geeks

New map is: {10=Geeks, 15=4, 20=All, 25=Welcomes, 30=You}

示例2:傳遞新 key 時。

// Java code to illustrate the put() method

import java.util.*;

public class Tree_Map_Demo {

public static void main(String[] args)

{

// Creating an empty TreeMap

TreeMap tree_map = new TreeMap();

// Mapping string values to int keys

tree_map.put(10, "Geeks");

tree_map.put(15, "4");

tree_map.put(20, "Geeks");

tree_map.put(25, "Welcomes");

tree_map.put(30, "You");

// Displaying the TreeMap

System.out.println("Initial Mappings are: " + tree_map);

// Inserting existing key along with new value

String returned_value = (String)tree_map.put(50, "All");

// Verifying the returned value

System.out.println("Returned value is: " + returned_value);

// Displayin the new map

System.out.println("New map is: " + tree_map);

}

}

輸出:

Initial Mappings are: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You}

Returned value is: null

New map is: {10=Geeks, 15=4, 20=Geeks, 25=Welcomes, 30=You, 50=All}

注意:可以對具有不同數(shù)據(jù)類型的變化和組合的任何類型的映射執(zhí)行相同的操作。

總結(jié)

以上是默认站点為你收集整理的java treemap用法_Java TreeMap put()用法及代码示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得默认站点網(wǎng)站內(nèi)容還不錯,歡迎將默认站点推薦給好友。