day13 Map的基本概念

Map

1)是双列集合的父接口
2)List中的元素,一个元素是一个数据,是单列集合
3)Map中的元素,一个元素是一对数据,这对数据叫键值对,键为key,值为value,一个key对应一个
value
4)实现类: HashMap, Hashtable,TreeMap
在这里插入图片描述

1 概述

1)key是键,不能重复,value是值,可以重复
2)通过key可以找到对应的value
3)key和value是一一对应的
4)map中有三个集合,键集、值集或键-值映射关系集
5)map是无序

方法

Map<String, Integer> map = new HashMap<>();
// 添加元素
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);

//清空
map.clear();
// 删除key对应的元素
map.remove("two");

// 当key值已经存在了,则put为修改,修改原来的key对用的value值
map.put("two", 22);

// 是否包括key
System.out.println(map.containsKey("three1"));
// 是否包含value
System.out.println(map.containsValue(11));
// 根据key获取对应的value 如果没有该key值,则返回null
System.out.println(map.get("three1"));
// 是否是空的集合
System.out.println(map.isEmpty());
// 获取value集 重复的无序的
Collection<Integer> values = map.values();
for (Integer integer : values) {
System.out.println(integer);
}
// 获取key集
Set<String> keys = map.keySet();
for (String key : keys) {
// 遍历map集合
System.out.println(key + "-->" + map.get(key));
}
// 映射关系集
3 HashMap
1)HashSet中存储数据,其实底层还是 HashMap中key值的存储,
2) HashMap设置自定义类型的唯一性,需要重写自定义类型中的hashCode()equlas()方法
// 将map中的元素,key和value的关系封装到一个内部类中,叫Entry类,该类中可以获取key
和value
Set<Map.Entry<String, Integer>> entries = map.entrySet();
for (Map.Entry<String, Integer> entry : entries) {
System.out.println(entry.getKey() + "-->" + entry.getValue());
}
Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> entry = iterator.next();
System.out.println(entry.getKey() + "-->" + entry.getValue());
}

3 HashMap

1)HashSet中存储数据,其实底层还是 HashMap中key值的存储,
2) HashMap设置自定义类型的唯一性,需要重写自定义类型中的hashCode()和equlas()方法

package com.map;
import java.util.HashMap;
public class HashMapTest {
public static void main(String[] args) {
HashMap<Stu, String> map = new HashMap<>();
map.put(new Stu("tom", 20), "tom");
map.put(new Stu("lily", 21), "lily");
map.put(new Stu("jeck", 22), "jeck");
map.put(new Stu("tom", 20), "tom");
System.out.println(map);
}
}
class Stu {
String name;
int age;
public Stu(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int hashCode() {
return 2;
}
@Override
public boolean equals(Object obj) {
Stu stu = (Stu) obj;
return stu.name.equals(this.name) && stu.age == this.age;
4 LinkedHashMap
存储的数据是key值不重复的,并且是按照存放顺序进行有序存储的
5 Collections
}
@Override
public String toString() {
return "Stu [name=" + name + ", age=" + age + "]";
}
}

4 LinkedHashMap

存储的数据是key值不重复的,并且是按照存放顺序进行有序存储的

LinkedHashMap<String, String> map2 = new LinkedHashMap<>();
map2.put("one", "1");
map2.put("two", "2");
map2.put("three", "3");
map2.put("four", "4");
map2.put("five", "5");
System.out.println(map2);

5 Collections

package com.map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsTest {
public static void main(String[] args) {
List<String> names = new ArrayList<String>();
// 想把一些数据存储到集合中
Collections.addAll(names, "tom", "lily", "张", "李");
System.out.println(names);
List<String> names1 = new ArrayList<>();
names1.add("1");
names1.add("1");
names1.add("1");
names1.add("1");
names1.add("1");
// 将参数二的集合元素覆盖到第一个参数集合中
Collections.copy(names1, names);
// 替换集合中的元素
Collections.fill(names1, "2");
// 获取集合中有几个该元素的数据
int num = Collections.frequency(names, "2");
System.out.println(names1);
System.out.println(num);
// 获取集合中的最大值
**HashMap和Hashtable的区别
System.out.println(Collections.max(names));
// 获取集合中最小值
System.out.println(Collections.min(names));
// 替换集合中的某个元素
Collections.replaceAll(names, "张", "张三");
System.out.println(names);
// 反转
Collections.reverse(names);
System.out.println(names);
// 随机置换集合中的元素顺序
// Collections.shuffle(names);
Collections.sort(names);
System.out.println(names);
// 查询元素的下标
System.out.println(Collections.binarySearch(names, "tom"));
// 互换某两个下标处的元素
Collections.swap(names, 1, 2);
System.out.println(names);
}
}

**HashMap和Hashtable的区别

1)HashMap允许有null值和null键,Hashtable不能有null值和null键
2)HashMap是不同步的,线程不安全的,执行效率高的,Hashtable是同步的,线程安全的,执行效率低的
3)HashMap和Hashtable的版本不同,HashMap是jdk1.2的,Hashtable是JDK1.0
4)两个的直接父类不同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值