Map集合由键和值组成,整个集合的特点都由键决定。
Map集合的键是无序不重复的,值不做要求可以重复。
package wryyyyy;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapDemo
{
public static void main(String[] args)
{
//1.创建一个Map集合对象
Map maps1 = new HashMap<>();//map是接口
maps1.put("Java",12);
maps1.put("Html",32);
maps1.put("C++",76);
maps1.put("Java",100);//键值重复是会覆盖前面的
System.out.println(maps1);
//2.清空集合
//maps1.clear();
//3.判断集合是否为空
System.out.println(maps1.isEmpty());
//4.获取键对应的值
Integer key = maps1.get("Java");
System.out.println(key);
System.out.println(maps1.get("html"));//没有这个键则取出值为null
//5.根据键删除整个元素
System.out.println(maps1.remove("Java"));//返回被删除元素的值
System.out.println(maps1);
//6.判断是否包含某个键
System.out.println(maps1.containsKey("Html"));
//7.判断是否包含某个值
System.out.println(maps1.containsValue(100));
//8.获取全部键的集合
Set sets = (maps1.keySet());//keySet:把键变成set集合
System.out.println(sets);//[C++, Html]
//9.获取全部值的集合
Collection values = maps1.values();//不把值放到set集合而是collection集合:set集合不重复,会去掉重复的值
System.out.println(values);
//10.集合的大小
System.out.println(maps1.size());
//11.合并其他map集合
Map map1 = new HashMap<>();
map1.put("Java1",1);
map1.put("Java2",22);
Map map2 = new HashMap<>();
map2.put("Java2",45);//会覆盖前面的
map2.put("Java3",90);
map1.putAll(map2);//把集合2中元素拷贝到map1中去
System.out.println(map1);//{Java2=45, Java3=90, Java1=1}
}
}
package wryyyyy;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
public class MapDemo
{
public static void main(String[] args)
{
//1.创建一个Map集合对象
Map maps1 = new HashMap<>();
maps1.put("Java",12);
maps1.put("Html",32);
maps1.put("C++",76);
maps1.put("Python",100);
System.out.println(maps1);
//1.方法1:键找值
Set keys = maps1.keySet();
for (String key : keys)
{
int value = maps1.get(key);
System.out.println(key+"===>"+value);
}
//方法2:键值对
//先把map集合转换成set集合,set集合中每个元素都是键值对实体类型了
Set> entries = maps1.entrySet();
//遍历set集合提取键以及提取值
for (Map.Entry entry : entries)
{
String key = entry.getKey();
int value = entry.getValue();
System.out.println(key+"====>"+value);
}
//方法3:Lambda表达式
maps1.forEach(new BiConsumer()
{
@Override
public void accept(String key, Integer value)
{
System.out.println(key+"====>"+value);
}
});
}
}
package wryyyyy;
import java.util.*;
import java.util.function.BiConsumer;
public class MapDemo
{
public static void main(String[] args)
{
//1.把80个学生选择的数据拿进来
String[] selects = {"A","B","C","D"};
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int i = 0; i < 80; i++)
{
sb.append(selects[r.nextInt(selects.length)]);
}
System.out.println(sb);
//2.定义一个map集合记录最终统计结果
Map infos = new HashMap<>();
//3.遍历学生选择的数据
for (int i = 0; i < sb.length(); i++)
{
//4.提取当前内容
char ch = sb.charAt(i);
//5.判断map集合中是否存在这个键
if (infos.containsKey(ch))
{
infos.put(ch,infos.get(ch)+1);//+1
}
else
{
//说明此景点是第一次统计
infos.put(ch,1);
}
}
//输出map集合
System.out.println(infos);
}
}
如果键要存储的是自定义对象,则需要重写hashCode和equals方法。
TreeSet和TreeMap底层原理是一样的。
TreeMap集合自定义排序规则:
(1)类实现Comparable接口,重写比较规则
(2)集合自定义Comparator比较器对象,重写比较规则
//TreeMap集合自带排序
Map maps = new TreeMap<>(new Comparator() {
@Override
public int compare(Apple o1, Apple o2) {
return Double.compare(o2.getPrice(),o1.getPrice());
//只要return值不为0就不认为重复
}
});
package wryyyyy;
import java.util.*;
public class MapDemo
{
public static void main(String[] args)
{
//1.记录每个学生选择的情况
Map> data = new HashMap<>();
//2.把学生选择的数据存进去
List selects1 = new ArrayList<>();
Collections.addAll(selects1,"A","C");
data.put("小王",selects1);
List selects2 = new ArrayList<>();
Collections.addAll(selects2,"B","C","D");
data.put("小李",selects2);
List selects3 = new ArrayList<>();
Collections.addAll(selects3,"A","B","D");
data.put("小孙",selects3);
List selects4 = new ArrayList<>();
Collections.addAll(selects4,"C","D");
data.put("小于",selects4);
System.out.println(data);
//统计每个景点选择的人数
Map infos = new HashMap<>();
//提取所有人选择景点的信息
Collection> values = data.values();//ctrl+alt+V
//遍历每个人选的信息
for (List value : values)
{
for (String s : value)
{
if (infos.containsKey(s))
{
infos.put(s,infos.get(s)+1);
}
else
{
infos.put(s,1);
}
}
}
System.out.println(infos);
}
}