当前位置:网站首页>Map interface and method
Map interface and method
2022-07-03 07:21:00 【four thousand three hundred and forty-three】



value Time repeatable , But disorderly , use Collection For short .
Entry: Key value pair object .
stay Map Class design is , Provides a nested interface (static Modified interface ):Entry.Entry Encapsulate the correspondence of key value pairs into objects , Key value pair object , So we're walking through Map When the collection , From each key value pair (Entry) Object to get the corresponding key and the corresponding value .
HashMap Implementation principle of JDK7 in

stay JDK8 in :
Map Interface method :

package MapInterface;
import org.junit.Test;
import java.util.*;
/*
map Common methods in interfaces
*/
public class MapMethod {
@Test
public void test1(){
Map map = new HashMap();
map.put("aa",123);
map.put("bb",456);
map.put("cc",789);
map.put("dd",101112);
System.out.println(map);
Map map1 = new HashMap();
// modify
map1.put("cc",123);
map1.put("dd",123);
map.putAll(map1);
System.out.println(map);
// remove(Object key)
Object value = map.remove("cc");
System.out.println(value);
System.out.println(map);
// clear() Empty map Data in , And map=null Different
map.clear();
System.out.println(map);
}
// Element query
@Test
public void test2(){
Map map = new HashMap();
map.put("aa",123);
map.put(123,"aa");
map.put("bb",456);
// get(key)
System.out.println(map.get(123));
// containisKey(key)
boolean isExist = map.containsKey("bb");
System.out.println(isExist);
// containisValue(value)
isExist = map.containsValue(123);
System.out.println(isExist);
Map map1 = new HashMap();
map1.put(123,"aa");
map1.put("aa",123);
map1.put("bb",456);
System.out.println(map.equals(map1));
}
@Test
public void test3(){
Map map = new HashMap();
map.put(123,1);
map.put(012,2);
map.put(456,4);
System.out.println(map);
}
@Test
// keySet: Traverse key、value、key-value aggregate .
public void test4(){
Map map = new HashMap();
map.put(123,1);
map.put(012,2);
map.put(456,4);
// Traverse key
Set set = map.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
// Traverse value
Collection values = map.values();
for (Object obj : values){
System.out.println(obj);
}
// The way 1:
Set keyValue = map.entrySet();
Iterator iterator1 = keyValue.iterator();
while (iterator1.hasNext()) {
// Object key = iterator1.next();
// Map.Entry entry = (Map.Entry)key;
// System.out.println(entry.getKey()+"===="+entry.getValue());
System.out.println(iterator1.next());
}
// The way 2:
Set keySet = map.keySet();
Iterator iterator2 = keySet.iterator();;
while(iterator2.hasNext()){
Object key = iterator2.next();
Object value = map.get(key);
System.out.println(key+"===="+value);
}
}
}
边栏推荐
猜你喜欢

Interview questions about producers and consumers (important)

Dora (discover offer request recognition) process of obtaining IP address

在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储

JMeter JSON extractor extracts two parameters at the same time

《指環王:力量之戒》新劇照 力量之戒鑄造者亮相

Basic components and intermediate components

Le Seigneur des anneaux: l'anneau du pouvoir

Inno setup production and installation package

带你全流程,全方位的了解属于测试的软件事故

4279. 笛卡尔树
随机推荐
高并发内存池
Strategy mode
Discussion on some problems of array
dataworks自定義函數開發環境搭建
docker建立mysql:5.7版本指定路径挂载不上。
[HCAI] learning summary OSI model
Advanced API (byte stream & buffer stream)
Laravel Web Framework
The underlying mechanism of advertising on websites
Advanced API (character stream & net for beginners)
[solved] unknown error 1146
【最详细】最新最全Redis面试大全(50道)
CentOS php7.3 installing redis extensions
Inno setup production and installation package
Pat grade a real problem 1166
TypeScript let與var的區別
【已解决】SQLException: Invalid value for getInt() - ‘田鹏‘
MySQL transaction rollback, error points record
JS date comparison
Raspberry pie update tool chain