目录
一、什么是集合
Java
集合框架
Java Collection Framework
,又被称为容器
container
,是定义在
java.util
包下的一组接口 interfaces 和其实现类 classes
。 其主要表现为将多个元素 element
置于一个单元中,用于对这些元素进行快速、便捷的存储
store
、检索retrieve 、管理
manipulate
,即平时我们俗称的增删查改
CRUD
类和接口的总览
注:此处只是列举了重要的部分
二、接口interface
1.Collection常用方法说明
方法名称 | 说明 |
boolean add(E e) | 将元素e放入集合中 |
void clear() | 删除集合中的所有元素 |
boolean isEmpty() | 判断集合中是否有任何元素,俗称空集合 |
boolean remove(Object e) | 如果元素e出现在集合中,删除其中一个 |
int size() | 返回集合中的元素个数 |
Object[] toArray() |
返回一个装有所有集合中元素的数组
|
一些使用示例:
(1)
import java.util.Collection;
public class TestDemo {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<String>();//当前的ArrayList只能放String类型的参数-->泛型
//第二个<>中可以不写,编辑器会根据第一个<>推导出第二个<>中的内容
//<>里面放的一定要是类类型,不能是基本数据类型,如<int>× <Integer>√
collection.add("hello");
System.out.println(collection);
}
}
编译并运行该代码,输出如下:
[hello]
collect是一个接口类型,应该打印的是一个地址,所以由该结果可知其Collect实现的ArrayList重写了toString方法
(2)(3)
import java.util.Collection;
public class TestDemo {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<String>();
collection.add("hello");
collection.clear();
System.out.println(collection);
System.out.println(collection.isEmpty());
}
}
编译并运行该代码,输出如下:
[]
true
(6)
import java.util.Arrays;
import java.util.Collection;
public class TestDemo {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<String>();
collection.add("hello");
collection.add("world");
Object[] objects = collection.toArray();
//String[] objects = (String[])collection.toArray();//error不建议进行整体强制类型转换,整体上强转了,但是数组里面的元素并没有转换;更细处说,所有类类型的父类都是Object,数组当然也是一种类类型,因此String[]和Object[]都是Object类型的子类,它们是平级关系,不存在父子类关系(运行时异常ClassCastExceptioon出现试图将一个对象强制转换为一个并非是自己或自己的子类的对象的时候),所以会抛出错误
System.out.println(Arrays.toString(objects));
}
}
编译并运行该代码,输出如下:
[hello,world]
总结:以下这些写法都是被允许的
public class TestDemo {
public static void main1(String[] args) {
String[] strings = new String[10];
Object o1 = new String[10];
Object[] o2 = new String[10];//Object是根对象,所有的对象都从它派生。所以任何对象数组都可以认为是object数组
}
}
这种写法是不被允许的
public class TestDemo {
public static void main1(String[] args) {
String[] strings = new Object[10];//error
String[] strings = (String[])new Object[10];//error
}
}
2.Map接口使用示例常用方法说明
方法名称 | 说明 |
V get(Object k)
|
根据指定的
k
查找对应的
v
|
V getOrDefault(Object k, V defaultValue)
|
根据指定的
k
查找对应的
v
,没有找到用默认值代替
|
V put(K key, V value)
|
将指定的
k-v
放入
Map
|
boolean containsKey(Object key) |
判断是否包含
key
|
boolean containsValue(Object value) |
判断是否包含
value
|
Set<Map,Entry<K,V>>entrySet() |
将所有键值对返回
|
boolean isEmpty() |
判断是否为空
|
int size() |
返回键值对的数量
|
一些使用示例:
(1)
import java.util.*;
public class TestDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("国民女神","高圆圆");
map.put("及时雨","宋江");
String ret = map.get("及时雨");
System.out.println(ret);
}
}
编译并运行该代码,输出如下:
宋江
(2)
import java.util.*;
public class TestDemo {
public static void main(String[] args) {
Map<String,String> map = new HashMap<>();
map.put("国民女神","高圆圆");
map.put("及时雨","宋江");
String ret = map.getOrDefault("下雨","糟糕");
System.out.println(ret);
}
}
编译并运行该代码,输出如下:
糟糕
(6)
import java.util.*;
public class TestDemo {
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());
}
}
}
编译并运行该代码,输出如下:
{国民女神=高圆圆, 及时雨=宋江}
--------------------------
key:国民女神value:高圆圆
key:及时雨value:宋江