集合
一、集合类概述
•
面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,
Java
就提供了集合类
。
•
与数组的区别:
数组虽然也可以存储对象,但长度是固定的,数组中可以存储基本数据类型;
集合只用于存储对象,长度是可变的,还可以存储不同类型的对象。
•
java中集合类的关系图:
二、
Collection
接口概述
•
Collection
层次结构中
的根接口。
Collection
表示一组对象,这些对象也称为
collection
的元素。一些
collection
允许有重复的元素,而另一些则不允许。一些
collection
是有序的,而另一些则是无序的。
•
Collection接口成员方法
boolean
add(E e
)//添加某个对象;booleanaddAll(Collection c)//添加一个集合
boolean
remove(Object o
)//移除某个元素;booleanremoveAll(Collection c)//移除一个集合
void
clear
()//清空集合;booleanretainAll(Collection c)//去掉集合C以外的元素
boolean
contains(Object o
)//集合是否包含某个对象;booleancontainsAll(Collection c)//集合是否包含某个集合
int
size()//集合大小;boolean isEmpty()//集合是否为空;
Object []toArray()//把集合转成数组,可以实现集合的遍历;Iteratoriterator()//迭代器,集合的专用遍历方式
•
Iterator接口概
对
collection
进行迭代的迭代
器,
依赖于集合而存在
Iterator接口成员方法
boolean
hasNext
()//判断是否还有下一个元素;Enext()//获取下一个元素
2.1 List
接口概述
•
有序的
collection
(也称为序列)。此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素
。与set不同,列表通常允许重复的元素。
•List接口方法
void add(intindex,Eelement)//在指定位置插入元素;E remove(int index)//移除指定位置的元素并返回该元素
E get(
int
index
)//获取指定位置的元素;
E set(
int
index,E
element)//修改指定位置的元素
•ListIterator
listIterator
()//List集合的迭代器,成员方法:booleanhasPrevious()//前面是否还有元素;E previous()//取出前一个元素
2.1.1 ArrayList
类概述及使用
•
底层数据结构是数组,查询快,增删慢
•
线程不安全,效率高
【案例】
import java.util.ArrayList;
import java.util.Iterator;
/*
* 单例集合的应用
*/
public class SingleRow {
public static void main(String[] args) {
ArrayList array = new ArrayList();// 创建集合对象
array.add("javase");// 创建元素对象,并添加元素
array.add("javame");
array.add("javaee");
array.add("android");
array.add("IOS");
Iterator it = array.iterator();// 迭代器遍历
while (it.hasNext()) {
System.out.println((String) it.next());
/*
javase
javame
javaee
android
IOS
*/
}
array.remove("javame");// 移除元素boolean remove(Object o)//继承Collection
String s = (String) array.remove(0);// E remove(int index)//继承List
for (int i = 0; i < array.size(); i++) {// for循环和List的 E get(int index)方法实现遍历
System.out.println(((String) array.get(i)));
/*
javaee
android
IOS
*/
}
array.set(2, s);// 将第三个元素修改为新javase,继承List
Object[] obj = array.toArray();// 继承Collection
for (int i = 0; i < 3; i++) {
System.out.println(obj[i]);
/*
javaee
android
javase
*/
}
}
}
2.1.2 LinkedList
类概述及使用
•
底层
数据结构是链表,查询慢,增删快。
线程不安全,效率高
•LinkedList
类
特有功能
public void
addFirst
(E
e)
及
addLast
(E
e
)//在最前面机在最后面添加一个数据
public E
getFirst
()
及
getLast
()//获取集合第一个和最后一个元素
public E
removeFirst
()
及
public E
removeLast
()//移除集合第一个元素和最后一个元素的值并返回被移除的值
2.2
Set
接口概述
•
一个不包含重复元素的
collection
。
2.2.1 HashSet
类概述
•
不保证
set
的迭代
顺序。
特别是
它不保证该顺序恒久不变
。
•
保证元素唯一性:底层数据结构是哈希表
(
元素是链表的数组
);哈希表依赖于哈希值存储;添加功能底层依赖两个方法:inthashCode()和boolean equals(Object obj)
2.2.2 TreeSet类概述
•
使用元素的自然顺序对元素进行
排序
•
或者
根据创建
set
时提供的
Comparator
进行
排序
•
具体
取决于使用的构造方法。
•
底层数据结构是红黑树
(
红黑树是一种自平衡的二叉树
),保证元素的排序和唯一性的
三、
Map接口概述l
Map
接口
概述
•
一
个映射不能包含重复的
键;
每个
键最多只能映射到一个
值
•
与Collection的区别:Map
是双列的,
Collection
是单列的;
Map
的键唯一,
Collection
的子体系
Set
是唯一的;
Map
集合的数据结构值针对键有效,跟值无关;
Collection集合的数据结构是针对元素有效
•Map接口成员方法
V put(K key,V value)//向集合添加键和值;V remove(Object key)/移除某个键元素
void clear()//清空集合中所有元素;booleanisEmpty()//集合是否为空;int size()//返回集合大小
boolean containsKey(Object key)//是否包含某个键元素;booleancontainsValue(Object value)//是否包含某个值元素
V get(Object key)//返回某个键元素;Set<K>keySet()//获取键集合;Collection<V> values()//获取值集合;Set<Map.Entry<K,V>>entrySet()//获取键值对集合
3.1
HashMap
类概述
•
键是哈希表结构,可以保证键的唯一性
3.2 LinkedHashMap类概述
•
Map
接口的哈希表和链接列表实现,具有可预知的迭代顺序。
3.3
TreeMap
类概述
•
键
是红黑树结构,
可以保证键
的排序和唯一性
3.4
Map
集合遍历
•
方式1:根据键找值
获取所有键的集合;
遍历键的集合,获取到每一个键;
根据键找值
•
方式2:根据键值对对象找键和值
获取所有键值对对象的集合;
遍历键值对对象的集合,获取到每一个键值对对象;
根据键值对对象找键和值
【案例】
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/* 为了更符合要求:
这次的数据就看成是学员对象。
javaee+hadoop
辅导班
张三---22
李四---23
高级班
德艺双馨---24
珍珍---16
java+android
辅导班
肖霞---24
飒飒---22
高级班
index80---25
janjan---18*/
public class DoubleRow {
public static void main(String[] args) {
// 创建大集合
HashMap<String, HashMap<String, ArrayList<Trainee>>> bigMap = new HashMap<String, HashMap<String, ArrayList<Trainee>>>();
// 辅导班数据
HashMap<String, ArrayList<Trainee>> tutorial = new HashMap<String, ArrayList<Trainee>>();
ArrayList<Trainee> array1 = new ArrayList<Trainee>();
array1.add(new Trainee("张三", 22));
array1.add(new Trainee("李四", 23));
ArrayList<Trainee> array2 = new ArrayList<Trainee>();
array2.add(new Trainee("德艺双馨", 24));
array2.add(new Trainee("珍珍", 16));
tutorial.put("辅导班", array1);
tutorial.put("高级班", array2);
bigMap.put("javaee+hadoop", tutorial);
// 高级班数据
HashMap<String, ArrayList<Trainee>> Senior = new HashMap<String, ArrayList<Trainee>>();
ArrayList<Trainee> array3 = new ArrayList<Trainee>();
array3.add(new Trainee("肖霞", 24));
array3.add(new Trainee("飒飒", 22));
ArrayList<Trainee> array4 = new ArrayList<Trainee>();
array4.add(new Trainee("index80", 25));
array4.add(new Trainee("janjan", 18));
Senior.put("辅导班", array3);
Senior.put("高级班", array4);
bigMap.put("java+android", Senior);
// 遍历集合
Set<String> heimaMapSet = bigMap.keySet();
for (String heimaKey : heimaMapSet) {
System.out.println(heimaKey);
HashMap<String, ArrayList<Trainee>> heimaMapValue = bigMap
.get(heimaKey);
Set<String> heimaValueSet = heimaMapValue.keySet();
for (String heimaValueKey : heimaValueSet) {
System.out.println("\t" + heimaValueKey);
ArrayList<Trainee> heimaMapValueValue = heimaMapValue
.get(heimaValueKey);
for (Trainee s : heimaMapValueValue) {
System.out.println("\t\t" + s.getName() + "---"
+ s.getAge());
}
}
}
}
}
//学员类
class Trainee {
private String name;
private int age;
public Trainee(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
四、Collections
类概述和成员方法
•
针对集合操作的工具类
•
Collections成员方法
public static <T> void sort(List<T> list)//排序
public
static<T>
int
binarySearch
(List<?>
list,T
key
)//二分法查找
public static <T> T
max(Collection<?>
coll
)//最大值
public static void
reverse(List<
?
>
list
)//反转元素
public static void shuffle(List<?> list)//打乱顺序