曲阜市古建设计院网站,响应式网站开发步骤,网站设计建设服务,网站优化 济南本篇文章建议在了解了哈希表和二叉搜索树后食用更佳。 链接: 二叉搜索树 和 哈希表 (JAVA)
Map和Set都是一种专门用来进行搜索的容器或者数据结构#xff0c;其搜索的效率与其具体的实例化子类有关。
Map接口
Map是一个接口#xff0c;不能直接实例化对象#xff0c;如果…本篇文章建议在了解了哈希表和二叉搜索树后食用更佳。 链接: 二叉搜索树 和 哈希表 (JAVA)
Map和Set都是一种专门用来进行搜索的容器或者数据结构其搜索的效率与其具体的实例化子类有关。
Map接口
Map是一个接口不能直接实例化对象如果要实例化对象只能实例化其实现类TreeMap或者HashMap;
MapInteger,Integer map1 new HashMap();
MapInteger,Integer map2 new TreeMap();Map接口并没有继承Collection该类中存储的是K,V结构的键值对并且K是唯一的且不能重复Value可以重复。 Map中的数据是以Key-Value 模型来存储的。 一般搜索的数据被称为Key而它的值被成为Value。 例如用Key-Value 模型来统计单词出现的次数就可以存储为单词单词出现的次数。
Map中的一些常用方法
put(key, value)
如果不存在 Key 就插入当前 key-value 键值对如果存在当前 Key 则更新 key 对应的 value值。
MapString,Integer map new TreeMap();
map.put(zhangsan, 5);
System.out.println(map);MapString,Integer map new TreeMap();
map.put(zhangsan, 5);
map.put(zhangsan, 10);
System.out.println(map);get(Key)
返回 key 对应的 value如果没有对应的 Key 则返回 null。
MapString,Integer map new TreeMap();
map.put(zhangsan, 5);
System.out.println(map.get(zhangsan));MapString,Integer map new TreeMap();
map.put(zhangsan, 5);
System.out.println(map.get(zll));getOrDefault(key, defaultValue)
返回 key 对应的 valuekey 不存在返回 defaultValue 。
MapString,Integer map new TreeMap();
map.put(zhangsan, 5);
System.out.println(map.getOrDefault(zhangsan, 0));
System.out.println(map.getOrDefault(llll, 0));remove(key)
删除 key 对应的映射关系(删除 Key 和它对应的 Value) 并返回 Value如果当前 key 不存在就返回 null 。
MapString,Integer map new TreeMap();
map.put(zhangsan, 5);
map.put(lisi, 5);
map.put(wangwu, 5);
System.out.println(map);
//删除“zhangsan”返回5
System.out.println(map.remove(zhangsan));
//因为不存在“zhangsan”返回null
System.out.println(map.remove(zhangsan));
System.out.println(map);void clear()
删除集合中的所有键值对 MapString,String map new TreeMap();map.put(sd,sd);System.out.println(map);map.clear();System.out.println(map);size()
返回map中的键值对的数量。
MapString,String map new TreeMap();
map.put(sd,sd);
System.out.println(map.size());Set K keySet()
返回包含所有 key 的一个集合对象。 此方法的返回值是 Set 类型的集合该集合中包含当前类中的所有 key 关键字Set 中不能存储重复的值。
MapString,Integer map new TreeMap();
map.put(zhangsan, 5);
map.put(lisi, 5);
map.put(wangwu, 5);
System.out.println(map);
SetString tmp map.keySet();
System.out.println(tmp);boolean isEmpty()
判断当前map集合中是否为空。 空就返回true否则返回false。
MapString,String map new TreeMap();
System.out.println(map.isEmpty());
map.put(sd,sd);
System.out.println(map.isEmpty());Collection V values()
返回所有 value 的可重复集合。 和上面的 keySet() 方法差不多这个方法是返回 Values 组成的集合。
MapString,Integer map new TreeMap();
map.put(zhangsan, 5);
map.put(lisi, 5);
map.put(wangwu, 5);
System.out.println(map);
CollectionInteger tmp map.values();
System.out.println(tmp);containsKey(key)
判断是否包含该 key返回类型是boolean类型。
containsValue(value)
判断是否包含该 value返回类型是boolean类型。
SetMap.EntryK, V entrySet()
返回所有的 key-value 映射关系。 在了解这个方法前先了解一下 Map.EntryK, V 。 Map.EntryK, V
Map 其实你可以想象成一个链表它里面的每一个 key-value 键值对都是以节点的形式来存储的而Map.EntryK, V 就是Map内部实现的用来存放key, value键值对映射关系的内部类。 在 Map.EntryK, V 中主要提供了key, value的获取value的设置以及Key的比较方式
getKey() 返回 entry 中的 keygetValue() 返回 entry 中的 valuesetValue(V value) 将键值对中的value替换为指定value
此时再来看 entrySet() 方法 entrySet() 方法其实就是返回 map 集合中的所有节点然后将其作为一个整体存放在 Set 类型的集合中。
MapString,Integer map new TreeMap();
map.put(zhangsan, 5);
map.put(lisi, 5);
map.put(wangwu, 5);
SetMap.EntryString,Integer tmp map.entrySet();
//第一种遍历方式
System.out.println(tmp);
System.out.println();
//第二种遍历方式
for (Map.EntryString,Integer a:map.entrySet()) {
System.out.println(Key:a.getKey() Valuea.getValue());
}Map 的一些注意事项
Map是一个接口不能直接实例化对象如果要实例化对象只能实例化其实现类TreeMap或者HashMap;Map中存放键值对的Key是唯一的value是可以重复的;在TreeMap中插入键值对时key不能为空否则就会抛NullPointerException异常value可以为空。但是HashMap的key和value都可以为空;Map中键值对的Key不能直接修改value可以修改如果要修改key只能先将该key删除掉然后再来进行重新插入。
TreeMap和HashMap的区别
TreeMapHashMap底层结构红黑树哈希桶插入/删除/查找时间O(log2N)O(1)是否有序关于Key有序无序线程安全不安全不安全插入/删除/查找区别需要进行元素比较通过哈希函数计算哈希地址比较与覆写key必须能够比较否则会抛出ClassCastException异常自定义类型需要覆写equals和hashCode方法应用场景需要Key有序场景下Key是否有序不关心需要更高的时间性能
Set 接口
Set相比于Map要简单很多。
Set是一个接口不能直接实例化对象如果要实例化对象只能实例化其实现类TreeSet或者HashSet。
SetInteger set1 new HashSet();
SetInteger set2 new TreeSet();Set与Map主要的不同有两点Set是继承自Collection的接口类Set中只存储了 Key
Set中的一些常用方法
boolean add(Object o)
添加元素 添加成功返回true失败返回false。
SetString set new TreeSet();
System.out.println(set.add(asd));
//因为该元素已经存在了所以不会添加成功返回false
System.out.println(set.add(asd));boolean addAll(Collection? extendsEc)
将集合c中的元素添加到set中可以达到去重的效果
void clear()
清空集合 SetString set new TreeSet();set.add(a);set.add(s);set.add(d);set.add(g);System.out.println(set);//删除所有元素set.clear();System.out.println(set);boolean contains(Object o)
判断 o 是否在集合中
SetString set new TreeSet();
set.add(a);
set.add(s);
set.add(d);
set.add(g);
System.out.println(set.contains(s));
System.out.println(set.contains(hhh));Iterator iterator()
返回一个迭代器
SetString set new TreeSet();
set.add(a);
set.add(s);
set.add(d);
set.add(g);
//利用迭代器进行集合的遍历
IteratorString tmp set.iterator();
while (tmp.hasNext()) {System.out.print(tmp.next() );
}boolean remove(Object o)
删除集合中的 o 成功删除返回true失败返回false。
SetString set new TreeSet();
set.add(a);
set.add(s);
set.add(d);
set.add(g);
System.out.println(set.remove(a));
//因为集合中不存在“asd”所以删除失败返回false
System.out.println(set.remove(asd));int size()
返回set集合中元素的个数
SetString set new TreeSet();
set.add(a);
set.add(s);
set.add(d);
set.add(g);
System.out.println(set.size());boolean isEmpty()
检测set是否为空空返回true否则返回false
SetString set new TreeSet();
System.out.println(set.isEmpty());
set.add(a);
set.add(s);
System.out.println(set.isEmpty());Object[] toArray()
将set中的元素转换为数组返回
SetString set new TreeSet();
set.add(a);
set.add(s);
Object[] tmp set.toArray();
for (int i 0; i 2; i) {System.out.println(tmp[i]);
}SetString set new TreeSet();
set.add(a);
set.add(s);
//此处不能进行强制类型转换
String[] tmp (String[]) set.toArray();
System.out.println(tmp);boolean containsAll(Collection? c)
集合c中的元素是否在set中全部存在是返回true否则返回false
Set 的一些注意事项
Set是继承自Collection的一个接口类Set中只存储了key并且要求key一定要唯一TreeSet的底层是使用Map来实现的其使用key与Object的一个默认对象作为键值对插入到Map中的Set最大的功能就是对集合中的元素进行去重实现Set接口的常用类有TreeSet和HashSet还有一个LinkedHashSetLinkedHashSet是在HashSet的基础上维护了一个双向链表来记录元素的插入次序Set中的Key不能修改如果要修改先将原来的删除掉然后再重新插入TreeSet中不能插入null的keyHashSet可以。
TreeSet和HashSet的区别
TreeSetHashSet底层结构红黑树哈希桶插入/删除/查找时间O(log2N)O(1)是否有序关于Key有序不一定有序线程安全不安全不安全插入/删除/查找区别按照红黑树的特性来进行插入和删除先计算key哈希地址 然后进行插入和删除比较与覆写key必须能够比较否则会抛出ClassCastException异常自定义类型需要覆写equals和hashCode方法应用场景需要Key有序场景下Key是否有序不关心需要更高的时间性能