您的位置:首页 > 文旅 > 美景 > Java数据结构- Map和Set

Java数据结构- Map和Set

2024/10/6 10:33:38 来源:https://blog.csdn.net/QUIXOTIC_/article/details/138324889  浏览:    关键词:Java数据结构- Map和Set

目录

  • 1. Map和Set
  • 2. Map的使用
  • 3. Set的使用

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()返回
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("-------------");//获取所有的valuesCollection<String> collections = map.values();System.out.println("获取所有的value" + collections);System.out.println("-------------");//获取所有的key和valuesSet<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();//迭代器,用于遍历setwhile (iterator.hasNext()) {System.out.print(iterator.next() + " ");}
}

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

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com