ConcurrentHashMap 类的 toString() 方法返回此映射的字符串表示形式。字符串表示由括在大括号 ("{}") 中的键值映射列表(无特定顺序)组成。
用法
public String toString()
参数
没有传递参数。
返回
此Map的字符串表示
抛出
不会抛出任何异常。
例子1
import java.util.concurrent.*;
import java.util.*;
public class ConcurrentHashMaptoStringExample1 {
public static void main(String[] args)
{
// crete a HashMap and add some values
HashMap<String, Integer> mapcon
= new HashMap<>();
mapcon.put("k1", 100);
mapcon.put("k2", 200);
mapcon.put("k3", 300);
mapcon.put("k4", 400);
System.out.println("HashMap values:\n " + mapcon.toString());
//replace the value of key k1 to 1000
mapcon.replace("k1" , 1000);
System.out.println("New HashMap after remove:\n "+ mapcon.toString());
}
}
输出:
HashMap values: {k1=100, k2=200, k3=300, k4=400} New HashMap after remove: {k1=100, k3=300, k4=400}
例子2
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMaptoStringExample2 {
public static void main(String args[]) {
ConcurrentHashMap<String , String> mymap = new ConcurrentHashMap<String ,String>();
ConcurrentHashMap<Integer , Integer> mymap2 = new ConcurrentHashMap<Integer , Integer>();
mymap2.put(1, 101);
mymap2.put(2, 102);
mymap2.put(3, 103);
System.out.println(" mymap2:" +mymap2.toString());
mymap.put("AA", "Java");
mymap.put("BB", "angular");
mymap.put("CC", "JavaScript");
System.out.println(" mymap:" +mymap.toString());
System.out.println("comparing mymap and mymap2:" +mymap.equals(mymap2));
}
}
输出:
mymap2:{1=101, 2=102, 3=103} mymap:{AA=Java, BB=angular, CC=JavaScript} comparing mymap and mymap2:false
相关用法
- Java ConcurrentHashMap contains()用法及代码示例
- Java ConcurrentHashMap newKeySet()用法及代码示例
- Java ConcurrentHashMap elements()用法及代码示例
- Java ConcurrentHashMap forEach()用法及代码示例
- Java ConcurrentHashMap putIfAbsent()用法及代码示例
- Java ConcurrentHashMap computeIfAbsent()用法及代码示例
- Java ConcurrentHashMap isEmpty()用法及代码示例
- Java ConcurrentHashMap equals()用法及代码示例
- Java ConcurrentHashMap putAll()用法及代码示例
- Java ConcurrentHashMap mappingCount()用法及代码示例
- Java ConcurrentHashMap merge()用法及代码示例
- Java ConcurrentHashMap hashcode()用法及代码示例
- Java ConcurrentHashMap containsKey()用法及代码示例
- Java ConcurrentHashMap put()用法及代码示例
- Java ConcurrentHashMap computeIfPresent()用法及代码示例
- Java ConcurrentHashMap replace()用法及代码示例
- Java ConcurrentHashMap clear()用法及代码示例
- Java ConcurrentHashMap size()用法及代码示例
- Java ConcurrentHashMap remove()用法及代码示例
- Java ConcurrentHashMap keys()用法及代码示例
注:本文由纯净天空筛选整理自 Java ConcurrentHashMap toString() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。