Map集合与Collection不是从属关系,是平级的html
Map集合的映射特色java
Map<KEY,VALUE>:KEY和VALUE表示泛型,KEY和VALUE能够是任何数据类型【除基本数据类型以外】,实际项目中KEY通常是String类型node
一个映射不能包含重复的键:web
若是存在重复的KEY,则后面添加的会把前面的覆盖windows
其实键就是Set,元素惟一,只能有一个null,安全
元素是无序的(与添加顺序无关)app
HashMap不是同步的,即线程不安全less
如何将HashMap变成线程安全?ide
Collections.synchronizedMap(<HashMap集合>);
public static void main(String[] args) {
Map<String, String> maps = new HashMap<>();
/*1.添加*/
//加入单条键值对
maps.put("智多星","吴用");
maps.put("豹子头","林冲");
maps.put("玉麒麟","卢俊义");
Map<String, String> map1 = new HashMap<>();
map1.put("小诸葛","富安");
map1.put("及时雨","宋江");
//加入一个集合:将一个集合复制到另外一个集合中
maps.putAll(map1);
System.out.println("map集合:"+maps);
System.out.println("map1集合:"+map1);
/*2.清空*/
map1.clear();
System.out.println("map1是否为空:"+map1.isEmpty()); //isEmpty()检查集合是否为空 返回布尔值
/*3.获取*/
//获取VALUE,经过KEY获取VALUE
System.out.println("获取智多星的值:"+maps.get("智多星"));
//获取所有的KEY,返回一个Set集合
Set<String> KEYSET = maps.keySet();
System.out.println("获取KEY的Set集合:"+KEYSET); //输出Map集合中的KEY
/*4.遍历集合*/
//遍历集合:经过遍历Set集合的KEYSET,来获取集合的值VALUE
System.out.println("===============\n经过for()加强遍历HashMap集合");
for (String str : KEYSET) {
String value = maps.get(str);
System.out.println(str+"——>"+value);
}
//遍历集合:经过entrySet(),返回 Set<Map.Entry<K,V>>,一个Set集合
System.out.println("===============\n经过entrySet()遍历HashMap集合");
Set<Map.Entry<String, String>> entries = maps.entrySet();
for (Map.Entry<String, String> entry : entries) {
//获取KEY
String key = entry.getKey();
//获取VALUE
String value = entry.getValue();
System.out.println(key+"——>"+value);
}
/*5.判断功能*/
//是否包含指定的键KEY,返回布尔值
System.out.println("maps集合中是否包含”玉麒麟“这个KEY:"+(maps.containsKey("玉麒麟")?"是":"否"));
//是否包含指定的值VALUE,返回布尔值
System.out.println("maps集合中是否包含”宋江“这个VALUE:"+(maps.containsValue("宋江")?"是":"否"));
//检查集合是否为空null
System.out.println("maps集合是否为空:"+(maps.isEmpty()?"是":"否"));
//HashMap不能同步,即线程不安全
/*6.如何线程安全?*/
Collections.synchronizedMap(maps);
Collections.synchronizedMap(map1);
}
特色svg
代码
xxxxxxxxxx
public static void main(String[] args) {
/*LinkedHashMap*/
Map<String, String> maps = new LinkedHashMap<>();
maps.put("智多星","吴用");
maps.put("豹子头","林冲");
maps.put("玉麒麟","卢俊义");
//LinkedHashMap根据元素添加顺序进行排序
System.out.println(maps);
/*HashMap*/
Map<String, String> hashmap = new HashMap<String, String>();
hashmap.put("智多星","吴用");
hashmap.put("豹子头","林冲");
hashmap.put("玉麒麟","卢俊义");
//排序与添加顺序无关
System.out.println(hashmap);
//综上比较LinkedHashMap和HashMap之间的区别即元素是否有序
}
/*
LinkedHashMap-->{智多星=吴用, 豹子头=林冲, 玉麒麟=卢俊义}
HashMap-->{玉麒麟=卢俊义, 智多星=吴用, 豹子头=林冲}
*/
不多用到,须要自定义比较器
TreeMap能够参考TreeSet,TreeMap能够支持Map的排序
TreeMap根据键KEY的天然顺序进行排序,或者根据建立映射【键和值相对应】时提供的Comparator进行排序,具体取决于使用的构造方法。
特色
能够按照KEY来排序
xxxxxxxxxx
/*
Student实现Comparable接口,则TreeMap能够根据KEY来排序
*/
public class Student implements Comparable<Student> {
private String name ;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
*
* @param o
* @return
* 自定义比较器:根据年龄来比较
*/
public int compareTo(Student o) {
return this.age - o.age;
}
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
xxxxxxxxxx
public class HashMapDemo02 {
public static void main(String[] args) {
Map<Student, String> maps = new TreeMap<>();
/*1.添加*/
//加入单条键值对
maps.put(new Student("智多星",30),"吴用");
maps.put(new Student("豹子头",30),"林冲");
maps.put(new Student("玉麒麟",30),"卢俊义");
maps.put(new Student("小诸葛",25),"富安");
System.out.println(maps);
}
}
/*答案
//由于以年龄age为排序依据,因此系统误认为第一条到第二条是同一条数据
//在Student中是设置根据年龄进行排序,TreeMap集合是根据KEY进行排序
{Student{name='小诸葛', age=25}=富安,
Student{name='智多星', age=30}=林冲,
Student{name='玉麒麟', age=33}=卢俊义}
*/
不经常使用
特色