概述
集合是类似数组可存储数据,但存储的数据容量可以随时发生改变
无论哪一个集合,在未存放数据之前,存储空间都为0,当正在操作存储第一个数据时,才会初始化存储空间。
集合分为单列集合和双列集合。Collection便是单列集合的顶层接口,它提供了更为具体的子接口去实现如List、Set。而List底下又由ArrayList、LinkedList类实现;而Set底下由HashSet、TreeSet类实现。
Collection集合的常用方法
方法名 | 说明 |
boolean add(E e) | 添加元素 |
boolean remove(Object o) | 从集合中移除指定的元素 |
void clear() | 清空集合中的元素 |
boolean contains(Object o) | 判断集合中是否存在指定的元素 |
boolean isEmpty() | 判断集合是否为空 |
int size() | 集合的长度,也就是集合中元素的个数 |
//如下
public static void main(String[] args) {
Collection<String> c = new ArrayList<String>();
//添加元素:boolean add(E e)
c.add("hello");
c.add("world");
c.add("java");
//输出集合对象
System.out.println(c);
}
Collection集合的遍历
Collection实现了Iterable,而在Iterable存在迭代器方法。所以Collection以及子接口,实现类等都可以使用迭代器。
使用迭代器遍历:Iterator<类型>
public static void main(String[] args) {
Collection<String> c = new ArrayList<String>();
//添加元素:boolean add(E e)
c.add("hello");
c.add("world");
c.add("java");
//输出集合对象
Iterator<String> it = c.iterator();
//用while循环改进元素的判断和获取
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
输出:
hello
world
java
List集合的基本使用
List集合存储学生对象遍历的三种方式
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public static void main(String[] args) {
//创建List集合对象
List<Student> list = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("林青霞", 30);
Student s2 = new Student("张曼玉", 35);
Student s3 = new Student("王祖贤", 33);
//把学生添加到集合
list.add(s1);
list.add(s2);
list.add(s3);
//迭代器:集合特有的遍历方式
Iterator<Student> it = list.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
//普通for:带有索引的遍历方式
for(int i=0; i<list.size(); i++) {
Student s = list.get(i);
System.out.println(s.getName()+","+s.getAge());
}
System.out.println("--------");
//增强for:最方便的遍历方式
for(Student s : list) {
System.out.println(s.getName()+","+s.getAge());
}
}
ArrayList和LinkedList
ArrayList集合底层是由数组结构实现,对于数组而言,查询快,增删慢
ArrayList的初始化存储空间为10,当数据存满了,则扩容1.5倍。
LinkedList集合底层是由链表结构实现,对于链表而言,查询慢,增删快
LinkedList的常用方法
方法名 | 说明 |
public void addFirst(E e) | 在该列表开头插入指定的元素 |
public void addLast(E e) | 将指定的元素追加到此列表的末尾 |
public E getFirst() | 返回此列表中的第一个元素 |
public E getLast() | 返回此列表中的最后一个元素 |
public E removeFirst() | 从此列表中删除并返回第一个元素 |
public E removeLast() | 从此列表中删除并返回最后一个元素 |
Set的基本使用
public static void main(String[] args) {
//创建集合对象
Set<String> set = new HashSet<String>();
//添加元素
set.add("hello");
set.add("world");
set.add("java");
//不包含重复元素的集合
set.add("world");
//遍历
for(String s : set) {
System.out.println(s);
}
}
set数据不重复是由哈希值而言。同一个对象多次调用hashcode方法,返回的哈希值是一致的。
默认情况下(不重写hashCode方法)不同对象的哈希值是不同的。
HashSet集合
特点
底层数据结构是哈希表
对集合的迭代顺序不作任何保证,也就是说不保证存储和取出的元素顺序一致
没有带索引的方法,所以不能使用普通for循环遍历
由于是Set集合,所以是不包含重复元素的集合
HashSet集合
1.根据对象的哈希值计算存储位置
如果当前位置没有元素则直接存入
如果当前位置有元素存在,则进入第二步
2.当前元素的元素和已经存在的元素比较哈希值
如果哈希值不同,则将当前元素进行存储
如果哈希值相同,则进入第三步
3.通过equals()方法比较两个元素的内容
如果内容不相同,则将当前元素进行存储
如果内容相同,则不存储当前元素
如果哈希值相同,内容不同也能进行存放。而数组的存储空间初始化为16,当达到75%存储空间时,则会进行扩容,每一次扩容为原本的2倍。
LinkedHashSet集合
特点
哈希表和链表实现的Set接口,具有可预测的迭代次序
由链表保证元素有序,也就是说元素的存储和取出顺序是一致的
由哈希表保证元素唯一,也就是说没有重复的元素
TreeSet集合
概述
元素有序,可以按照一定的规则进行排序,具体排序方式取决于构造方法
TreeSet():根据其元素的自然排序进行排序
TreeSet(Comparator comparator) :根据指定的比较器进行排序
没有带索引的方法,所以不能使用普通for循环遍历
由于是Set集合,所以不包含重复元素的集合
Set集合排序:自然排序、比较器排序
自然排序:
元素所属的类实现Comparable接口,重写compareTo(T o)方法
比较器排序:
- 用TreeSet集合存储自定义对象,带参构造方法使用的是比较器排序对元素进行排序的
- 比较器排序,就是让集合构造方法接收Comparator的实现类对象,重写compare(T o1,T o2)方法
- 重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写