当前位置:网站首页>Collection Map
Collection Map
2022-08-10 05:51:00 【hagong9】
目录
结构图


package com.map_.Map_;
import java.util.HashMap;
public class Demo {
public static void main(String[] args) {
HashMap map = new HashMap();
map.put("no1","张三");//ok
map.put("no2","李四");//ok
map.put("no3","王五");//ok
map.put("no3","王五");//no
map.put(1,"呜啊");//key和valueCan be any reference type data
map.put("no3","马六");//key相同时 Equivalence and substitution
map.put(null,null);// Map 的key 和value可以为null
map.put("no4",null);//key只能有一个是null 但value可以有多个为null
System.out.println(map);
System.out.println(map.get("no2"));//可以通过key得到value
}
}
常用方法和遍历方式

package com.map_.Map_;
import java.util.*;
//Map接口方法 六大遍历方法
public class Demo02 {
public static void main(String[] args) {
//添加 put
HashMap map = new HashMap();
map.put("no1","张三");
map.put("no2","李四");
map.put("no3","王五");
map.put("no4",new People("马六"));
map.put(null,"刘七");
System.out.println(map);
//删除 remove
map.remove("no3");
System.out.println(map);
//获取 get
System.out.println(map.get("no2"));
//获取元素个数 size
System.out.println(map.size());
//清空 clear
//map.clear();
//查找键是否存在
System.out.println(map.containsKey("no1"));
//Find if the value exists
System.out.println(map.containsValue("李四"));
//遍历方法
//第一组:先取出所有的key,通过key取出对应的Value
Set keyset = map.keySet();
//1.增强for
System.out.println("========增强for========");
for (Object key :keyset) {
System.out.println(key +"-"+map.get(key));
}
//2.迭代器
System.out.println("=========迭代器============");
Iterator iterator = keyset.iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println(key + "-" + map.get(key));
}
//第二组 取出所有的值 用collocation的遍历方法
Collection values = map.values();
System.out.println("======The second set of iterators==========");
Iterator iterator1 = values.iterator();
while (iterator1.hasNext()) {
Object obj = iterator1.next();
System.out.println(obj);
}
System.out.println("=========The second group of enhancementsfor======");
for (Object o : values) {
System.out.println(o);
}
//第三组 通过EntrySet 来获取 k-v
Set entrySet = map.entrySet();
//增强for
System.out.println("=======使用entrySet 的增强for=======");
for (Object entry :entrySet) {
//将entry 转成 Map.Entry
Map.Entry m = (Map.Entry)entry;
System.out.println(m.getKey()+"-"+m.getValue());
}
//迭代器
System.out.println("========使用entrySet的迭代器");
Iterator iterator2 = entrySet.iterator();
while (iterator2.hasNext()) {
Object o = iterator2.next();
System.out.println(o);
}
}
}
class People{
private String name;
public People(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
'}';
}
}
小结

HashTable
底层有数组 Hashtable$Entry[]初始化大小为11
临界值 threshold 8 = 11*0.75
底层使用addEntry(hash k,value index)添加键值对.
扩容机制:when the critical value is reached,Shift the old array one bit to the left+1(两倍+1)



Properties
继承了hashTable
通过k-v存放数据,key和value不能为空.

由于继承HashTable类,使用方法与HashTable相差不大.
如何选择集合实现类

TreeMap
TreeSet和TreeMap的区别,TreeSet中的e放的是key, presentIt is a static object,
TreeMap,e放的是key,prent位置放的是value.

排序方法
和TreeSetThere is also no ordering in the case of no-argument constructors
The default is ascending alphabetical order ?
比较方法和TreeSet一样,You can write your own comparison method.比如字符串长度.length().
package com.map_.Map_;
import java.util.Comparator;
import java.util.TreeMap;
public class TreeMap_ {
public static void main(String[] args) {
//按照传入的Key(String) 的大小进行排序
TreeMap treeMap = new TreeMap(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return ((String)o1).compareTo((String)o2);
}
});
treeMap.put("jack","杰克");
treeMap.put("tom","汤姆");
treeMap.put("kristina","克瑞斯提诺");
treeMap.put("smith","史密斯");
System.out.println(treeMap);
}
}
边栏推荐
猜你喜欢

PCL,VS配置过程中出现:用 _sopen_s 代替 _open, 或用_CRT_SECURE_NO_WARNNINGS错误

The latest and most complete digital collection sales calendar-07.26

定时器(setInterval)的开启与关闭

链读推荐:从瓷砖到生成式 NFT

The latest and most complete digital collection sales calendar-07.27

非会员更改有道云笔记背景

深度学习中的学习率调整策略(1)

来亲自手搭一个ResNet18网络

Analysis of the investment value of domestic digital collections

用Pytorch从0到1实现逻辑回归
随机推荐
The complex "metaverse" will be interpreted for you, and the Link Reading APP will be launched soon!
复杂的“元宇宙”,为您解读,链读APP即将上线!
Bifrost micro synchronous database implementation services across the library data synchronization
第十天作业
文本元素
第五次实验
图片批量添加水印批量加背景缩放批量合并工具picUnionV4.0
Chain Reading Good Article: Jeff Garzik Launches Web3 Production Company
Operation table Function usage
网络安全6
redis---非关系型数据库(NoSql)
优先队列
反射【笔记】
Batch add watermark to pictures batch scale pictures to specified size
Four characteristics of ACID
2021-06-22
IDEA 项目中设置 Sources Resources 等文件夹
网络安全3
IDEA的database使用教程(使用mysql数据库)
Collection工具类