当前位置:网站首页>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点云滤波
Chain Reading|The latest and most complete digital collection sales calendar-08.02
泛型笔记()()()
Ten years of sharpening a sword!The digital collection market software, Link Reading APP is officially open for internal testing!
Chained Picks: Starbucks looks at digital collectibles and better engages customers
.las转.txt 再转.pcd,编译运行中出现的错误
matlab中的常用的类型转换
力扣——省份数量
The complex "metaverse" will be interpreted for you, and the Link Reading APP will be launched soon!
棋类游戏-五子棋小游戏
随机推荐
每天一个小知识点
连接 Nacos 报超时错误
Linux database Oracle client installation, used for shell scripts to connect to the database with sqlplus
训练集Loss收敛,但是测试集Loss震荡的厉害?
impdp import data
el-dropdown drop-down menu style modification, remove the small triangle
第二次实验
各个架构指令集对应的机型
Batch add watermark to pictures batch scale pictures to specified size
你不知道的常规流
用Pytorch从0到1实现逻辑回归
数据库 笔记 创建数据库、表 备份
三维点云分割
The submenu of the el-cascader cascade selector is double-clicked to display the selected content
【笔记】集合框架体系 Collection
sqlplus displays the previous command and the available backspace key
redis常见的面试题
Copy large files with crontab
常用类 String概述
棋类游戏-五子棋小游戏