当前位置:网站首页>集合 Map
集合 Map
2022-08-10 05:32: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和value可以是任何引用类型数据
map.put("no3","马六");//key相同时 就等价与替换
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"));
//查找值是否存在
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("======第二组迭代器==========");
Iterator iterator1 = values.iterator();
while (iterator1.hasNext()) {
Object obj = iterator1.next();
System.out.println(obj);
}
System.out.println("=========第二组增强for======");
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)添加键值对。
扩容机制:当到达临界值后,将老数组向左移动一位+1(两倍+1)



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

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

TreeMap
TreeSet和TreeMap的区别,TreeSet中的e放的是key, present放的是个静态的对象,
TreeMap,e放的是key,prent位置放的是value。

排序方法
和TreeSet无参构造器情况下也是没有排序的
默认是按照字母升序 ?
比较方法和TreeSet一样,可以自己写比较方法。比如字符串长度.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);
}
}
边栏推荐
猜你喜欢
随机推荐
Linux数据库Oracle客户端安装,用于shell脚本用sqlplus连接数据库
文本元素
AWR1843型号毫米波雷达使用
利用 crontab 拷贝大文件
使用Google Protobuf 在 Matlab 中工作
The complex "metaverse" will be interpreted for you, and the Link Reading APP will be launched soon!
Content related to ZigBee network devices
Batch add watermark to pictures batch scale pictures to specified size
图片批量添加水印批量缩放图片到指定大小
IDEA的database使用教程(使用mysql数据库)
PCL,VS配置过程中出现:用 _sopen_s 代替 _open, 或用_CRT_SECURE_NO_WARNNINGS错误
One step ahead, don't miss it again, the chain reading APP will be launched soon!
几种绘制时间线图的方法
Pony语言学习(七)——表达式(Expressions)语法(单篇向)
GtkD开发之路
cesium 添加点,移动点
知识蒸馏论文学习
Pony语言学习(六):Struct, Type Alias, Type Expressions
Operation table Function usage
定时器(setInterval)的开启与关闭









