目录
介绍
Java 集合框架
Java Collection Framework
,又被称为容器 container
,是定义在 java.util 包下的一组接口 interfaces 和其实现类 classes 。
其主要表现为将多个元素element
置于一个单元中,用于对这些元素进行快速、便捷的存储 store 、检索retrieve 、管理 manipulate ,即平时我们俗称的增删查改 CRUD 。
例如,一副扑克牌(一组牌的集合)、一个邮箱(一组邮件的集合)、一个通讯录(一组姓名和电话的映射关系)等等。
类和接口总览
Java 集合框架的优点及作用
使用成熟的集合框架,有助于我们便捷、快速的写出高效、稳定的代码…
学习背后的数据结构知识,有助于我们理解各个集合的优缺点及使用场景…
接口 interfaces
基本关系说明
Collection: 用来存储管理一组对象 objects ,这些对象一般被成为元素 elements
Set : 元素不能重复,背后隐含着查找/搜索的语义
SortedSet : 一组有序的不能重复的元素
List : 线性结构
Queue : 队列
Deque : 双端队列
Map : 键值对 Key-Value-Pair ,背后隐含着查找/搜索的语义
SortedMap : 一组有序的键值对
Collection 常用方法
将元素 放入集合中
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("hello2");
//collection.add(1);
//尖括号当中 放的类型 一定要是 类类型 不能是简单的基本类型
Collection<Integer> collection2 = new ArrayList<>();
collection2.add(1);
collection2.add(2);
collection2.add(13);
System.out.println(collection);
System.out.println(collection2);
}
删除集合中的所有元素
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("hello2");
System.out.println(collection);
collection.clear();
System.out.println(collection);
//尖括号当中 放的类型 一定要是 类类型 不能是简单的基本类型
Collection<Integer> collection2 = new ArrayList<>();
collection2.add(1);
collection2.add(2);
collection2.add(13);
System.out.println(collection2);
collection2.clear();
System.out.println(collection2);
}
返回一个装有所有集合中元素的数组
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("hello2");
System.out.println(collection);
Object[] objects = collection.toArray();
System.out.println(Arrays.toString(objects));
}
如果元素 e 出现在集合中,删除其中一个
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("hello2");
collection.remove("hello");
System.out.println(collection);
}
返回集合中的元素个数
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("hello2");
collection.add("hello3");
int ret = collection.size();
System.out.println(ret);
}
Map 常用方法
将指定的 k-v 放入 Map (V put(K key, V value))|| 根据指定的 k 查找对应的 v,没有找到用默认值代替 (V get(Object k))|| 判断是否包含 key (boolean containsKey(Object key))
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("国民女神","高圆圆");
map.put("及时雨","宋江");
String ret = map.getOrDefault("及时雨","博哥");
System.out.println(ret);
boolean flg = map.containsKey("国民女神2");
System.out.println(flg);
}
将所有键值对返回(Set<Map.Entry<K, V>> entrySet())|| 判断是否包含 key(boolean containsKey(Object key))|| 判断是否包含 value(boolean containsValue(Object value))
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("及时雨","宋江");
map.put("国民女神","高圆圆");
System.out.println(map);
System.out.println("====================");
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for( Map.Entry<String, String> entry : entrySet) {
System.out.println("key: "+entry.getKey()+" value:"+entry.getValue());
}
}
public static void main(String[] args) {
TreeMap<String,String> map2 = new TreeMap<>();
map2.put("及时雨","宋江");
map2.put("国民女神","高圆圆");
System.out.println(map2);
HashMap<String,String> map = new HashMap<>();
map.put("及时雨","宋江");
map.put("国民女神","高圆圆");
System.out.println(map);
}