集合,迭代器和for循环加强
集合的特点
提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变
同时会介绍三种集合输出方式;
列表迭代器Iterator
和ListIterator
(区别ListIterator属于List,可以避免并发) ,for(加强版),一般使用增强版for遍历。
我的集合都是String类型,String也可以看成一对象,懒得定义相关代码了
Collection集合概述
是单例集合的顶层接口,可以看作C语言队列这种存储结构,它表示一组对象,这些对象也称为Collection的元素 JDK不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现
Collection为接口,所以可以使用多态进行实例化Collection<String> collection = new ArrayList<String>();
下述方法通过Iterator
遍历。
Iterator(),迭代器简述
- 迭代器,集合的专用遍历方式
- 集合.iterator(),将集合进行封装为
Iterator<String> iterator = collection.iterator();
Collection<String> collection = new ArrayList<String>();
// 天啊及对象”111“
collection.add("111");
// 删除集合内的“111对象
collection.remove("111");
// 判断是否存在”11111“
collection.contains("11111");
// 返回值为布尔类型,判断集合是否为空
collection.isEmpty();
//长度
collection.size();
collection.clear();
Iterator<String> iterator = collection.iterator();
while (iterator.hasNext()){
String next = iterator.next();
System.out.println("对象:"+ next);
}
**
List集合专属迭代器ListIterato
有序集合(也称为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元
素,并搜索列表中的元素。
与Set集合不同,列表通常允许重复的元素
List集合特点
- 有索引 可以通过集合对象.get(索引)调用
- 可以存储重复元素
- 元素存取有序
方法使用,因为List是ArrayList的父类,方法有相似,可以看ArrayList的方法使用。主要讲数List专属的迭代器ListIterator
使用原因是terator 遍历时,如果对集合队形进行修改可能会造成并发问题,前后数据不一致,导致遍历出错。当然可以使用for加强版
List<String> strings = new ArrayList<String>();
strings.add("1111");
ListIterator<String> li = strings.listIterator();
while (li.hasNext()){
String next = li.next();
System.out.println(next);
if(next.equals("1111")){
System.out.println("空");
}
}
ArrayList
实现List接口,特点一样功能加强。底层是数组实现的,长度可以变化,构造方法ArrayList<Student> students = new ArrayList<Student>();
常用方法
代码演示
ArrayList<String> students = new ArrayList<String>();
students.add("1111");
students.add("2222");
students.add("3333");
students.add(3,"66666");
//students.size()集合中存储String对象个数
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i));
// 修改索引位置1,改为4444,输出就表示没问题
if (i == 1){
students.set(1,"4444");
System.out.println("索引位置为1"+students.get(1));
}
// students.remove()传入的参数列表可以是索引位置,
// 也可以是对象如students.remove(students.get(i))尽量传入对象,规范书写
}
Set集合 增强for进行遍历
set集合的特点:首先我们要了解set不能存储重复元素,元素并称不是有序排列,只能通过迭代器或增强for循环遍历进行遍历。先在来解释一下原因,set集合是通过add(对象),内部会将对象通过hashcode返回的int数据进行存储,所以是无序排列。
方法:通过对象.可以查看提示方法,通过返回值和传入参数可以理解一些常用方法。
哈希值set集合的原理是通过hashcode根据对象的地址或者字符串或者数字算出来的int类型的数值,同一个对象多次调用hashCode()方法返回的哈希值是相同。对象也可以通过哈希输出int类型的数据。
Set<String> set = new HashSet<String>();
set.add("1111");
set.add("2222");
set.add("3333");
set.add("4444");
for (String s : set){
System.out.println("set:"+s);
}
HashSet
简述hashset是set的子类,所以该集合也有对象元素唯一原则,通过hashcode计算存储地址,而且在源代码中
public class HashSetDemo02 {
public static void main(String[] args) {
//创建HashSet集合对象
HashSet<Student> hs = new HashSet<Student>();
//创建学生对象
Student s1 = new Student("林青霞", 30);
Student s2 = new Student("张曼玉", 35);
Student s3 = new Student("王祖贤", 33);
Student s4 = new Student("王祖贤", 33);
//把学生添加到集合
hs.add(s1);
hs.add(s2);
hs.add(s3);
hs.add(s4);
//遍历集合(增强for)
for (Student s : hs) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}
LinkedHashSet
LinkedHashSet也是实现set接口,具有可预测的迭代次序是通过链表进行存储,同时具有set的特点
//创建集合对象
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<String>();
//添加元素
linkedHashSet.add("hello");
linkedHashSet.add("world");
linkedHashSet.add("java");
linkedHashSet.add("world");
//遍历集合
for(String s : linkedHashSet) {
System.out.println(s);
}
ThreeSet
threeset集合可以有序进行排序,但是是通过有参构造器,中的TreeSet(Comparator comparator)
惊醒排序比较。没有带索引的方法,所以不能使用普通for循环遍历,仍具有set集合的不重复元素特点。
首先了解一下comparator两种比较方法。遍历迭代器和增强for。
//顺序排序
@Override
public int compareTo(Student s) {
// return 0;
// return 1;
// return -1;
//按照年龄从小到大排序
int num = this.age - s.age;
// int num = s.age - this.age;
//年龄相同时,按照姓名的字母顺序排序
int num2 = num==0?this.name.compareTo(s.name):num;
return num2;
}
//比较排序
@Override
public int compare(Student s1, Student s2) {
//this.age - s.age
//s1,s2
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()): num;
return num2;
}