云南百度小程序开发站长之家seo工具包
集合体系结构
Collection 单列集合
包含List Set
List 包含ArrayList LinkedList
Set包含HashSet TreeSet
HashSet包含LinkedHashSet
List系列集合:添加的元素是有序的、可重复、有索引
Set系列集合:添加的元素是无序的、不重复、无索引
Collection是单列集合的祖宗接口,它的功能是全部单列集合都可以继承使用的
public static void main(String[] args) {//Collection是一个接口,我们不能直接创建它的对象Collection<String> coll=new ArrayList<>();//1.添加元素/*细节1.如果王List里添加元素,方法总是返回true 因为List允许元素重复细节2.如果往Set李添加元素,不存在返回true,存在返回falseSet系列集合不允许重复* */coll.add("aaa");coll.add("bbb");coll.add("ccc");coll.add("ddd");System.out.println(coll);//2.清空 // coll.clear(); // System.out.println(coll);//3.删除//因为Collection里面定义的方法是共性的方法,所以不能通过索引进行删除,智能通过元素的对象进行删除/*方法会有一个布尔类型的返回值,true删除成功,false失败* */coll.remove("aaa");System.out.println(coll);//4.判断元素是否包含//底层是依赖equals方法进行判断是否存在的//所以要判断自定义对象是否存在时候,要重新equals方法System.out.println(coll.contains("bbb"));//5.判空boolean empty = coll.isEmpty();System.out.println(empty);//6.获取集合的长度int size = coll.size();System.out.println(size);}
Collection的遍历方式
迭代器遍历
特点:迭代器不依赖索引
迭代器在Java中的类是Iterator,迭代器是集合专用的遍历方式
Collection<Integer> collection= new ArrayList<>(); collection.add(1); collection.add(2); collection.add(3); Iterator<Integer> it=collection.iterator(); while (it.hasNext()){int i=it.next();System.out.print(i+" "); }
细节注意:
1.报错NoSuchElementException
2.迭代器遍历完毕,指针不会复位
3.循环中只能用一次next方法
4,迭代器遍历时,不能用集合的方法进行添加或者删除
增强for遍历
增强for的底层就是迭代器,为了简化迭代器的代码书写的
所有的单列集合和数组才能用增强for遍历
格式:
for(元素的数据类型 变量名:数组或者集合){
}
for(String s : list){
}
public static void main(String[] args) {Collection<String> coll = new ArrayList<>();coll.add("zhangsan");coll.add("lisi");coll.add("wangwu");for (String s : coll) {System.out.println(s);} }
细节:
修改增强for中的变量,不会改变集合中原来的数据
Lambda表达式遍历
public static void main(String[] args) {Collection<String> coll = new ArrayList<>();coll.add("zhangsan");coll.add("lisi");coll.add("wangwu");/* coll.forEach(new Consumer<String>() {@Override//s 是以此表示集合的每一个数据public void accept(String s) {System.out.println(s);}});*/coll.forEach(s -> System.out.println(s));} }
List中常见的方法和遍历方式
//List集合中的两个删除的方法 //1.直接删除元素 //2.通过素银进行删除//1.创建集合并添加元素
//1.创建一个集合 List<String> list =new ArrayList<>();//2.添加元素 list.add("a"); list.add("b"); list.add("c");//void add(int index,E element);在指定索引位置添加指定的元素 //细节: /* 原来索引上的元素会依次往后移 * */ list.add(1,"QQQ");//remove String remove = list.remove(0); System.out.println(remove);//set String aaa = list.set(0, "aaa"); System.out.println(aaa);//get String s = list.get(0); System.out.println(s);//3.打印集合 System.out.println(list);
List集合的五种遍历方式
//创建集合List<String> list = new ArrayList<>();list.add("aaa");list.add("bbb");list.add("ccc");list.add("ddd");list.add("eee");/* //1.迭代器遍历Iterator<String> iterator = list.iterator();while (iterator.hasNext()){String next = iterator.next();System.out.println(next);}*///2.增强for/* for (String str : list) {System.out.println(str);}*///3.Lambda表达式 /* list.forEach(s-> System.out.println(s));*///4.普通for/* for (int i = 0; i < list.size(); i++) {String s = list.get(i);System.out.println(s);}*///5.列表迭代器//获取一个列表迭代器对象,同样的里面的指针也是指向零索引ListIterator<String> it = list.listIterator();while (it.hasNext()){String next = it.next();if ("bbb".equals(next)){it.add("qqq");}System.out.println(next);}System.out.println(list);
遍历方式比较
迭代器遍历:在遍历的过程中需要删元素,请使用迭代器
列表迭代器:在遍历的时候需要添加元素,请使用列表迭代器
增强for,Lambda:仅仅想遍历
普通for:如果遍历的时候想操作索引
Set系列集合
无序:存取顺序不一致
不重复:可以去除重复
无索引:没有带索引的方法,所以不能使用普通for遍历,也不能通过索引获取元素
Set集合的实现类
HashSet:无序,不重复,无索引
LinkedHashSet:有序,不重复,无索引
TreeSet:可排序,不重复,无索引
//1.创建Set集合对象Set<String> s = new HashSet<>();//2.添加元素s.add("aaa");s.add("bbb");s.add("ccc");s.add("ddd"); // s.add("aaa");3.遍历//迭代器遍历Iterator<String> iterator = s.iterator();while (iterator.hasNext()) {String next = iterator.next();System.out.println(next);}//增强forfor (String s1 : s) {System.out.println(s1);}//Lambda表达式s.forEach(a -> System.out.println(a));
HshSet底层采取哈希表存储数据
TreeSet集合默认的规则:对于字符、字符串类型,按照ASCII码表中的顺序进行排序
TreeSet的两种排序方式
1.自然排序
类里面实现Comparable接口里面的方法
@Override public int compareTo(Student o) {//指定排序的规则//按照年龄的升序进行排序return this.getAge()-o.getAge();}
2.比较器排序
TreeSet<String> ts = new TreeSet<>((o1, o2) -> {int i = o1.length() - o2.length();i = i == 0 ? o1.compareTo(o2) : i;return i; });
TreeSet<String> ts = new TreeSet<>(new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {int i = o1.length() - o2.length();i = i == 0 ? o1.compareTo(o2) : i;return i;} });
使用场景
1.如果想要集合中的元素可重复
用ArrayList基于数组的
2.如果想要集合中的元素可重复,而且当前的增删操作明显多于查询
用LinkedList,基于链表的
3.如果想对集合中的元素去重
用HashSet,基于哈希表的
4.如果想对集合中的元素去重,而且保重存取顺序
用LinkedHashList,基于哈希表和双链表,效率低于HashSet
5.如果想对集合中的元素进行排序
用TreeSet,基于红黑树,后续可以用List集合实现排序