1. Map和Set

Java中,Map和Set是两个接口,TreeSet、HashSet这两个类实现了Set接口,TreeMap、HashMap这两个类实现了Map接口。
在这里插入图片描述

带Tree的这两个类(TreeSet、TreeMap)底层的数据结构是一棵红黑树(一棵特殊的二叉搜索树),带Map的这两个类(HashSet、HashMap)底层的数据结构是哈系桶

2. Map的使用

Map中常用的方法

方法作用
V get(Object Key)返回key的value
V getOrDefault(Object key,V defaultValue)返回key的value,key不存在则返回默认值
V put(K key,V value)设置key对应的value/插入一个新的键值对
V remove(Object key)删除key对应的映射关系
Set< K > keySet()返回所有的key
Collection< V > values()返回所有的value
Set<Map.Entry<K,V>> entrySet()返回所有的key-value映射
boolean containKey(Object key)判断是否包含key
boolean containValue(Object value)判断是否包含value

Map.Entry<K,V>中的方法

方法说明
K getKey()返回key
V getValue()返回Entry的value
V setValue(V value)将原来的value替换为指定的value

举个例子~~

public static void main(String[] args) {
    Map<Integer, String> map = new TreeMap<>();//new HashMap也是一样的
    map.put(1, "ZhangSan");
    map.put(2, "LiSi");
    map.put(3, "WangWu");
    //获取所有的key,返回值是Set<K>
    Set<Integer> set = map.keySet();
    System.out.println("获取所有的key:" + set);
    System.out.println("-------------");
    //获取所有的values
    Collection<String> collections = map.values();
    System.out.println("获取所有的value" + collections);
    System.out.println("-------------");
    //获取所有的key和values
    Set<Map.Entry<Integer, String>> entries = map.entrySet();
    System.out.println("获取所有的key和value" + entries);
    System.out.println("-------------");
    //key不能重复,value可以重复

    System.out.println("使用Map.Entry<Integer, String>中的setValue替换前");//
    for (Map.Entry<Integer, String> entry : entries) {
        System.out.println(entry.getKey() + " " + entry.getValue());
    }
    for (Map.Entry<Integer, String> entry : entries) {
        entry.setValue("111111");
    }
    System.out.println("替换后");
    for (Map.Entry<Integer, String> entry : entries) {
        System.out.println(entry.getKey() + " " + entry.getValue());
    }
}

输出结果:
在这里插入图片描述

注意事项:

  • 1、Map存储的是Key-Value结构的键值对,key是唯一的不能重复,value可以重复
  • 2、插入新的键值对时,如果key重复了,会更新key对应的value的值
  • 3、TreeMap插入的键值对,key不能为空,value可以为空;HashMap插入的键值对key和value都可以为空
  • 4、Map中的key想要修改,只能先删除,再重新插入

3. Set的使用

与Map不同的是,Set只存储key,不存储value

常用的方法

方法说明
boolean add()添加元素,重复的元素不会添加成功
void clear()清空整个集合
boolean contains(Object o)判断o是否在集合中
Iterator< E > iterator()迭代器
boolean remove(Object o)删除集合中的o
int size()返回set中的元素个数
boolean isEmpty()判断是否为空
Object[] toArray()将set中的元素转换为数组

例

public static void main(String[] args) {
    Set<Integer> set = new TreeSet<>();
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);
    set.add(5);
    System.out.println(set);
    System.out.println(set.size());
    System.out.println(set.contains(6));
    System.out.println(set.contains(5));
    Object[] arr = set.toArray();
    System.out.println("-------------");
    for (Object o : arr) {
        System.out.print(o + " ");
    }
    System.out.println();
    System.out.println("---------------");
    Iterator<Integer> iterator = set.iterator();
    //迭代器,用于遍历set
    while (iterator.hasNext()) {
        System.out.print(iterator.next() + " ");
    }
}

输出结果:
在这里插入图片描述
注意事项:

  • 1、set只存储了key值,并且key是唯一的,不能重复
  • 2、TreeSet的底层是使用Map来实现的,插入key时,value会默认插入一个Object对象
  • 3、TreeSet不能插入null,HashSet可以插入null

更多推荐