当前位置:网站首页>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);
}
}
}
边栏推荐
- Talk about floating
- 20220319
- [day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
- 在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
- 多个全局异常处理类,怎么规定执行顺序
- Centos切换安装mysql5.7和mysql8.0
- RestHighLevelClient获取某个索引的mapping
- Raspberry pie update tool chain
- Advanced APL (realize group chat room)
- Flask Foundation
猜你喜欢
docker建立mysql:5.7版本指定路径挂载不上。
[HCAI] learning summary OSI model
Discussion on some problems of array
Common APIs
带你全流程,全方位的了解属于测试的软件事故
Final, override, polymorphism, abstraction, interface
Summary of abnormal mechanism of interview
Setting up the development environment of dataworks custom function
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
Recursion, Fibonacci sequence
随机推荐
Strategy mode
TypeScript let與var的區別
4279. 笛卡尔树
Common analysis with criteria method
Inno setup production and installation package
Summary of abnormal mechanism of interview
Laravel Web Framework
带你全流程,全方位的了解属于测试的软件事故
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
Pat grade a real problem 1166
Distributed lock
【已解决】win10找不到本地组策略编辑器解决方法
docker建立mysql:5.7版本指定路径挂载不上。
JS monitors empty objects and empty references
JMeter test result output
树莓派更新工具链
RestHighLevelClient获取某个索引的mapping
SecureCRT取消Session记录的密码
Pits encountered in the use of El checkbox group
Store WordPress media content on 4everland to complete decentralized storage