Bootstrap

JavaSE基本知识补充 -Map集合

目录

Map(key,value键值对呈现)

1.1 Map的映射的特点

1. 2.HashMap (键值对的业务偏多,而且hashmap在jdk1.7和1.8之间有所不同,性能做了提升,面试高频考点)

1.3 Map接口的方法

方法

HashMap遍历方式:

1.4 map的实现类TreeMap

1.5 Map的实现类HashTable

1.6 map的实现类LinkedHashMap

LinkedHashMap的特点

Map中实现线程安全的方式有哪些?


Map(key,value键值对呈现)

List集合:
Set:
Map是一个键值对的集合:
我们如果想要从map中获得值,可以根据键
Map
Map虽然是集合但是和Collection的集合体系不是一个体系:

1.1 Map的映射的特点

Map:
        1,Map集合(无序)的数据结构实现只针对键有效,与值无关。
        2,存储的是键值对形式的元素,键唯一,值可重复。   --和索引一样
一个映射不能包含重复的键;  -键不能重复
每个键最多只能映射到一个值。
键只允许有一个空值,  -键不能重复
值可以有多个空值。
Map也是无序的 -键无序
        Map :K和V表示泛型,map的key和value可以是任何类型(不能是基本数据类型,可以使用它的包装类),实际项目中K一般都是字符串类型。
基本类型
引用类型
boolean
Boolean
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character

1. 2.HashMap (键值对的业务偏多,而且hashmap在jdk1.7和1.8之间有所不同,性能做了提升,面试高频考点

Hashmap:
  1. hashmap键唯一,值可重复。(键的特点就相当于set集合特点)
  2. 底层数据结构是1.7(数组+链表)1.8(数组,红黑树,链表)。 数据结构不一样,导致性能不一样
  3. 线程不安全,效率高
一个映射不能包含重复的键;
如果存在重复的key就会把之前的覆盖掉
其实键就是Set,元素唯一,只能有一个null,元素是无序的。
如果大家能理解Set那么map的key也就能理解了。
package cn.qcby.demo;

import java.util.HashMap;
import java.util.Map;

public class MapTest {


    public static void main(String[] args) {
        //创建一个Map集合
        Map<String, String> map = new HashMap<>();
        //向集合总加元素
        map.put("白日鼠","白胜");
        map.put("豹子头","林冲");
        map.put("小诸葛","富安");

        //key只能有一个是null,值任意
        /*map.put(null, null);
        map.put("aaaa", null);*/
        //值是可以重复的
        //map.put("大诸葛","富安");

        //如果存在重复的key就会把之前的覆盖掉
        //map.put("小诸葛","高衙内");
        System.out.println(map);

    }
}

1.3 Map接口的方法

方法

1.put
//创建一个Map集合
Map<String, String> map = new HashMap<>();
//向集合总加元素
map.put("白日鼠","白胜");
map.put("豹子头","林冲");
map.put("小诸葛","富安");

System.out.println(map);
2.putAll
Map<String, String> map1 = new HashMap<>();
//向集合总加元素
map1.put("智多星","吴用");
map1.put("入云龙","公孙胜");

map.putAll(map1);


System.out.println(map);

 

1.clear
//清空集合
map.clear();
System.out.println(map);
2. remove
map.remove("小诸葛");
System.out.println(map);

 

1. get
//获得,根据key获得值
 String value = map.get("入云龙");
 System.out.println(value);
2. keySet
//获得一个map的集合的key的set集合
Set<String> strings = map.keySet();
System.out.println(strings);

 

1.containsKey
//判断map集合是否包含指定的key
boolean isCon = map.containsKey("豹子头");
System.out.println(isCon);
2. containsValue
//判断集合是否包含指定的值
boolean isCon1 = map.containsValue("白胜");
System.out.println(isCon1);
3. isEmpty
map.clear();
System.out.println(map);
//判断集合是否是空集合
boolean empty = map.isEmpty();
System.out.println(empty);
entrySet
Set<Map.Entry<String, String>> entries = map.entrySet();

for (Map.Entry<String, String> entry : entries){
    //获得map的元素的key
    String key = entry.getKey();
    //获得map的元素的value
    String value = entry.getValue();
    System.out.println(key +"----------->"+value);
}

HashMap遍历方式:

        keySet()  
        entrySet()
         lambada遍历
        Streams Api单线程遍历
        Streams Api多线程遍历
lambada遍历:
map.forEach((key,value) -> {
                System.out.print(key);
                System.out.print(value);
});
Streams Api单线程遍历
map.entrySet().stream().forEach((StringStringEntry -> {
    System.out.println(StringStringEntry.getKey());
    System.out.println(StringStringEntry.getValue());
}));


1.4 map的实现类TreeMap

        该映射根据其键的 自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,具体取决于使用的构造方法。
TreeMap特点:
  1. 只针对键来说元素唯一,不能为null,可自定义排序。(set集合特点)
  2. 底层数据结构红黑树(是一种平衡的二叉树)。
  3. 线程不安全。
        可以按着key来做排序
        Key不能null,key不能重复,值可以有多个null --比较的时候,会报空指针异常
        不是线程安全的
添加:
//创建一个Map集合
Map<String, String> map = new TreeMap<>();
//向集合总加元素
map.put("b","白胜");
map.put("a","林冲");
map.put("e","富安");
map.put("c","富安");

System.out.println(map);
有null值:
//创建一个Map集合
Map<String, String> map = new TreeMap<>();
//向集合总加元素
map.put("b","白胜");
map.put("a","林冲");
map.put("e","富安");
map.put("c",null);

System.out.println(map);
       
         Person实现了comparable接口,我们的treemap可以根据key来做排序
   按年龄排

public class Person implements Comparable<Person>{

    private String name;

    private int age;

    public Person(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;
    }

    @Override
    public int compareTo(Person o) {
        return this.age - o.age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

//创建一个Map集合
Map<Person, String> map = new TreeMap<>();
//向集合总加元素
map.put(new Person("白日鼠", 30),"白胜");
map.put(new Person("豹子头", 28),"林冲");
map.put(new Person("及时雨", 35),"宋江");

System.out.println(map);

1.5 Map的实现类HashTable

HashTable 的特点
        1. HashTable是 map 的实现类
        2.不允许任何 null 值和 null --无法调用hashCode(),会抛出空指针异常 get(key)获取的null无法确定是键不存在还是值为null
        3. HashTable中的元素没有顺序(跟添加的顺序无关)
        4. HashTable是线程安全的。
        5.底层实现是哈希表。
//创建一个Map集合
Map<String, String> map = new Hashtable<>();
//向集合总加元素
map.put("b","白胜");
map.put("a","林冲");
map.put("e","富安");
map.put("c","asdsa");
map.put("d","afdsdsa");
map.put("e","asddffsa");
map.put("f","adfdfdfdsa");
map.put("g","assdsdsa");
System.out.println(map);

1.6 map的实现类LinkedHashMap

LinkedHashMap的特点

        1. LinkedHashMap是 map 的实现类(底层实现是链表)
        2.允许多个 null 值和一个 null 键 键是唯一的 注意,使用 get () 方法获取值时,如果键对应的值为 null,它会返回 null
        3. LinkedHashMap有顺序(添加的顺序)
        4. LinkedHashMap不是线程安全的。
//创建一个Map集合
Map<String, String> map = new LinkedHashMap<>();
//向集合总加元素
map.put("b","白胜");
map.put("a","林冲");
map.put("e","富安");
map.put("c",null);


System.out.println(map);

 

Map中实现线程安全的方式有哪些?

        1.多线程环境下,可以使用concurrent包下有一个ConcurrentHashMap(方法和hashmap一样只是做了升级加了同步) 或者是使用Collections.synchronized Map (new HashMap());  
实现同步:
三个:
hashTable直接使用  
ConcurrentHashMap直接使用  
Collections.synchronizedMap(new HashMap()) -使用collections接口
        2.ConcurrentHashMap保证线程安全,效率比HashTable高
JDK1.7版本:ReentrantLock+Segment+HashEntry
JDK1.8版本:synchronized+CAS+HashEntry+红黑树
;