好潦草的一篇文章,不想学习想摆烂了又 ,凑合看
文章目录
- 一、Map的实现类的结构
- 二、Map结构的理解
- 三、HashMap的底层实现原理? 以jdk7的说明:
- 四、Map中定义的方法
- 五、总结:常用方法
- 六、代码
提示:以下是本篇文章正文内容,下面案例可供参考
一、Map的实现类的结构
二、Map结构的理解
三、HashMap的底层实现原理? 以jdk7的说明:
四、Map中定义的方法
五、总结:常用方法
六、代码
package com.tyust.edu;
import org.junit.Test;
import java.util.*;
/**
* @author YML TYUST-XDU 2019-2026
* @create 2023-10-12 8:45
*/
public class MapTest {
@Test
public void test1(){
Map map = new HashMap();
// map = new Hashtable();
map.put(null,null);
}
@Test
public void test3() {
Map map = new HashMap();
//添加
map.put("AA",123);
map.put(45,123);
map.put("nn",23);
//修改
map.put("AA",37);
System.out.println(map);
Map map1 = new HashMap();
map1.put("CC",123);
map1.put("DD",123);
map.putAll(map1);
System.out.println(map);
Object value = map.remove("CC");
System.out.println(value);
System.out.println(map);
map.clear();
System.out.println(map.size());
}
@Test
public void test4() {
Map map = new HashMap();
//添加
map.put("AA", 123);
map.put(45, 123);
map.put("nn", 23);
//修改
map.put("AA", 37);
System.out.println(map.get(45));
boolean isExit = map.containsKey("nn");
System.out.println(isExit);
isExit = map.containsValue(123);
System.out.println(isExit);
map.clear();
System.out.println(map.isEmpty());
}
@Test
public void test5() {
Map map = new HashMap();
//添加
map.put("AA", 123);
map.put(45, 123);
map.put("nn", 23);
//修改
map.put("AA", 37);
//遍历所有的key集keySet()
Set set = map.keySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//遍历所有的value集:values()
Collection values = map.values();
for(Object obj:values){
System.out.println(obj);
}
System.out.println();
//遍历所有的key-value
//entrySet()
Set entrySet = map.entrySet();
Iterator iterator1 = entrySet.iterator();
while(iterator1.hasNext()){
Object obj = iterator1.next();
//entrySet集合中的元素都是entry
Map.Entry entry = (Map.Entry)obj;
System.out.println(entry.getKey()+"--->"+entry.getValue());
}
System.out.println();
Set keySet = map.keySet();
Iterator iterator2 = keySet.iterator();
while(iterator2.hasNext()){
//System.out.println(iterator2.next());
Object key = iterator2.next();
Object value = map.get(key);
System.out.println(key+"--->"+value);
}
}
}