java map集合简介说明
下文笔者将简明扼要的讲述map集合的简介说明,如下所示
Map是一个接口,它具有以下特点:
1.Map中的键是无序的,并且键是不重复的,没有索引,map中的值可以重复
2.Map集合中的键和值都可以为null
3.Map集合中后放入的键值会替换前面相同键的值
Map常见的实现类有以下三种
HashMap:
元素根据键是无序的
不重复的
无索引的
而值是可以重复的(与Map体系一致)
LinkedHashMap:
元素根据键是有序的,键不重复的,无索引的,值是可以重复的
TreeMap:
元素根据键是排序的,键不重复的,无索引的,值是可以重复的
Map集合实现类的示例分享
package com.java265.other;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class TestMap {
/**
* java265.com Map实现类的功能简介说明
*/
public static void main(String[] args) {
Map<String, String> map1 = new HashMap<>();
Map<String, String> map2 = new LinkedHashMap<>();
Map<String, String> map3 = new TreeMap<>();
map1.put("a", "java265.com-1");
map1.put("c", "java265.com-2");
map1.put("b", "java265.com-3");
map2.put("a", "java265.com-1");
map2.put("c", "java265.com-2");
map2.put("b", "java265.com-3");
map3.put("a", "java265.com-1");
map3.put("c", "java265.com-2");
map3.put("b", "java265.com-3");
map3.put("b", "java265.com-4");
System.out.println("map1:" + map1);
System.out.println("map2:" + map2);
System.out.println("map3:" + map3);
}
}
-----运行以上代码,将输出以下信息------
map1:{a=java265.com-1, b=java265.com-3, c=java265.com-2}
map2:{a=java265.com-1, c=java265.com-2, b=java265.com-3}
map3:{a=java265.com-1, b=java265.com-4, c=java265.com-2}
从以上代码,我们可以得出 map中LinkedHashMap是有顺序的 并且map都不可插入重复的键值 但是我们发现TreeMap的排序有些诡异,那么TreeMap的排序是一个什么样的规则呢? 下文笔者将一一道来,如下所示
TreeMap
TreeMap集合的排序是默认对键升序排列 当然我们也可以自定义排序规则,如下所示 1.类实现Comparable接口,重写比较规则 2.集合自定义Comparator比较器对象,重写比较规则
自定义排序规则
User类:
public class User implements Comparable<User>{
private String name;
private int age;
@Override
public int compareTo(User o) {
return o.getAge()-this.getAge();
}
/*此处省略 get set方法的编写*/
Map<User,String > User = new TreeMap<>();
User.put(new User("毛四",9000),"Java程序员");
User.put(new User("刘畅",32000),"python程序员");
User.put(new User("柳荫",6000),"清洁工");
System.out.println(User);
集合自定义Comparator比较器对象:
Map<User,String > User = new TreeMap<>(new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
return o2.getSalary()-o1.getSalary();
}
});
User.put(new User("毛四",9000),"Java程序员");
User.put(new User("刘畅",32000),"python程序员");
User.put(new User("柳荫",6000),"清洁工");
System.out.println(User);
Map集合常使用的API
Map<String ,Integer> maps = new HashMap<>();
//1、添加元素
maps.put("小敏",18);
maps.put("阿香",19);
maps.put("晓东",20);
System.out.println(maps);
System.out.println("-----------------------------");
//2、根据键key删除键值对key和value
maps.remove("阿香");
System.out.println(maps);
System.out.println("-----------------------------");
//3、清除Map集合中的所有元素
maps.clear();
System.out.println(maps);
System.out.println("-----------------------------");
//4、判断Map集合中是否包含此key
System.out.println(maps.containsKey("小敏"));
System.out.println(maps.containsKey("晓东"));
System.out.println("-----------------------------");
//5、判断Map集合中是否包含此value
System.out.println(maps.containsValue(30));
System.out.println(maps.containsValue(19));
System.out.println("-----------------------------");
//6、判断Map集合是否为空
System.out.println(maps.isEmpty());
System.out.println("-----------------------------");
//7、获取Map集合中元素个数
System.out.println(maps.size());
System.out.println("-----------------------------");
//8、获取集合中所有的键
maps.put("秋红",18);
maps.put("黄菲",19);
Set<String> key = maps.keySet();//将所有键储存在set集合中
System.out.println(key);
System.out.println("-----------------------------");
//9、获取集合中的所有值
Collection<Integer> values = maps.values();
System.out.println(values);
System.out.println("-----------------------------");
遍历Map集合
使用键查找值
实现思路:
1.获取keyset集合
2.foreach判断,并且使用get方法获取value值
Map<String ,Integer> maps = new HashMap<>();
maps.put("小敏",18);
maps.put("阿香",19);
maps.put("晓东",20);
System.out.println(maps);
Set<String> key = maps.keySet();
for (String k:key
) {
System.out.println(maps.get(k));
}
获取map的键值对
实现思路:
使用getkey()和getvalue()即可获取键值对集合
例:
Map<String ,Integer> maps = new HashMap<>();
maps.put("小敏",18);
maps.put("阿香",19);
maps.put("晓东",20);
System.out.println(maps);
Set<Map.Entry<String, Integer>> set = maps.entrySet();
System.out.println(set);
for (Map.Entry<String, Integer> s :
set) {
System.out.println(s.getKey()+"->"+s.getValue());
}
使用Lambda表达式获取map集合数据
Map<String ,Integer> maps = new HashMap<>();
maps.put("小敏",18);
maps.put("阿香",19);
maps.put("晓东",20);
System.out.println(maps);
maps.forEach(new BiConsumer<String, Integer>() {
@Override
public void accept(String k, Integer v) {
System.out.println(k+"->"+v);
}
});
//Lambda表达式简化map集合数据获取
maps.forEach((k,v)-> System.out.println(k+"->"+v) );
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


