集合容器概述
集合的特点
集合的特点主要有如下两点:
-
对象封装数据,对象多了也需要存储。集合用于存储对象。
-
对象的个数确定可以使用数组,对象的个数不确定的可以用集合。因为集合是可变长度的。
集合框架
Java集合框架,又称为容器,是定义在java.util 包下的一组接口(interfaces)和其实现类(classes)。
其主要表现为将多个元素置于一个单元中,用于对这些元素进行快速、便捷的存储、检索、管理,即俗称的增删查改CRUD。
集合框架是为表示和操作集合而规定的一种统一的标准的体系结构。
任何集合框架都包含三大块内容:对外的接口、接口的实现和对集合运算的算法。
集合框架通过提供有用的数据结构和算法使你能集中注意力于你的程序的重要部分上,而不是为了让程序能正常运转而将注意力于低层设计上。
使用集合框架的好处
- 容量自增长;
- 提供了高性能的数据结构和算法,使编码更轻松,提高了程序速度和质量;
- 允许不同 API 之间的互操作,API之间可以来回传递集合;
- 可以方便地扩展或改写集合,提高代码复用性和可操作性。
- 通过使用JDK自带的集合类,可以降低代码维护和学习新API成本。
常用的集合类有哪些?
Map接口和Collection接口是所有集合框架的父接口:
- Collection接口的子接口包括:Set接口和List接口
- Map接口的实现类主要有:HashMap、TreeMap、Hashtable、ConcurrentHashMap以及Properties等
- Set接口的实现类主要有:HashSet、TreeSet、LinkedHashSet等
- List接口的实现类主要有:ArrayList、LinkedList、Stack以及Vector等
类和接口的关系
迭代器:Iterator(I)
工具类:Arrays(数组方法集),Collections(集合方法集)
对象比较:Comparable,Comparator
Collection接口
Collection <E> 接口扩展了Iterable <E> 接口
所有Collection的实现类(通过其子接口之一间接实现Collection)都提供两个标准的构造函数:一个无参的构造函数,用于创建一个空集合;以及一个类型为单个参数的构造函数,创建一个新集合。
Collection常用的方法
方法签名 | 说明 |
boolean add(E e) | 将元素 e 放入集合中 |
void clear() | 删除集合中的所有元素 |
boolean contains(Object o) | 如何集合中包含指定元素o,返回true |
boolean equals(Object o) | 如果指定对象等于此集合,则为true |
int hashCode() | 返回集合的哈希值 |
boolean isEmpty() | 判断集合是否为空 |
boolean remove(Object o) | 如果元素e出现在集合中,删除其中一个 |
int size() | 返回集合中的元素数量 |
Object[ ] toArray() | 返回一个装有所有集合中元素的数组 |
Collection常用方法使用
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class CollectionTest {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
//使用isEmpty和size
System.out.println(collection.isEmpty());
System.out.println(collection.size());
//add操作,进行尾插
collection.add("我");
collection.add("和");
collection.add("你");
System.out.println(collection.size());
System.out.println(collection.isEmpty());
//toArray转换为数组
Object[] arr = collection.toArray();
System.out.println(Arrays.toString(arr));
//for Each遍历
for (String s: collection){
System.out.println(s);
}
//使用contains
boolean res = collection.contains("我");
System.out.println(res);
//使用remove删除
collection.remove("和");
for (String s : collection){
System.out.println(s);
}
}
}
Map接口
将键映射到值的对象。
不能包含重复的键;每个键最多可以映射到一个值。
Map常用方法
方法签名 | 说明 |
V get (Object k) | 根据指定的k查找对应的v,,如果不包含返回null |
V getOrDefault(Object k, V defaultValue) | 根据指定的k查找对应的v,没有找到用默认值代替 |
V put(K key,V value) | 将指定的k-v 放入Map |
boolean containsKey(Object key) | 判断是否包含key |
booolean conainsValue(Object value) | 判断是否包含value |
Set<Map.Entry<K,V>> entrySet() | 返回所有键值对 |
boolean isEmpty() | 判断是否为空 |
int size() | 返回键值对的数量 |
Map常用方法使用
import java.util.HashMap;
import java.util.Map;
public class MapTest {
public static void main(String[] args) {
Map<String ,String> map = new HashMap<>();
//使用size()和isEmpty()
System.out.println(map.size());
System.out.println(map.isEmpty());
//使用指定的key查找对应的value
System.out.println(map.get("作者"));
//查找对应的value,如果没有找到就是用默认值
System.out.println(map.getOrDefault("作者","佚名"));
System.out.println(map.containsKey("作者"));
System.out.println(map.containsValue("佚名"));
//使用put插入键值对
map.put("作者","鲁迅");
map.put("标题","狂人日记");
//相同的key,新的value值会覆盖旧的value值
map.put("发表时间","1918年");
map.put("发表时间","1919年");
System.out.println(map.size());
System.out.println(map.isEmpty());
System.out.println(map.get("作者"));
System.out.println(map.getOrDefault("作者","佚名"));
//containsKey,containsValue判断是否存在
System.out.println(map.containsKey("作者"));
System.out.println(map.containsValue("佚名"));
//遍历,每一个entry就是map中的一个键值对
for (Map.Entry<String ,String> entry : map.entrySet()){
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
Java集合框架中其余的接口和其实现类的使用,后续继续总结~~~~~