新星计划 Day3【JavaSE】 集合 Part1
👩💻博客主页:京与旧铺的博客主页
✨欢迎关注🖱点赞🎀收藏⭐留言✒
🔮本文由京与旧铺原创,csdn 首发!
😘系列专栏:java 学习
💻首发时间:🎞2022 年 4 月 27 日🎠
🎨你做三四月的事,八九月就会有答案,一起加油吧
🀄如果觉得博主的文章还不错的话,请三连支持一下博主哦
🎧最后的话,作者是一个新人,在很多方面还做的不好,欢迎大佬指正,一起学习哦,冲冲冲
🛒导航小助手🎪
[TOC]
🥡511 集合框架与项目的对比与概述
一,集合框架的概述
1.集合,数组都是对多个数组进行存储操作的结构,简称 java 容器
说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中)
2.1、数组在存储多个数据方面的特点:
1. 一旦初始化以后,长度就确定了
2. 数组一旦定义好,其元素的类型也就确定了,我们也就只能操作指定类型的数据了
比如:String[ ] arr;
int[ ] arr1;
2.2 数组在存储多个数据方面的缺点
1.一旦初始化以后,其长度就不可以修改、
2.数组中提供的方法非常有限,对于添加,删除,插入数据等操作,非常不便,同时效率不高
3.获取数组中的实际元素的个数的需求,数组没有现成的属性或方法可用
4.数组存储数组的特点:有序,可重复,对于无序和不能重复的需求,数组不能满足
🥠512 集合框架涉及到的 API
Java 集合可分为Collection和Map两种体系
Collection 接口:单列数据,定义了存取一组对象的方法的集合
单列集合,用来存储一个一个的对象
List:元素有序、可重复的集合 “动态”数组
Set:元素无序、不可重复的集合 高中讲的集合
Map接口:双列数据,保存具有映射关系“key-value 对”的集合 高中函数:y=f(x)
双列集合,用来存储一对(key-value)一对的数据
Collection 接口继承树
Map 接口继承树
🍜513 Collections 接口中的常用方法 1
public class CollectionTest{ public void test1{ Collection coll=new Arraylist(); //add(Object e):将元素e添加到集合coll中 coll.add("AA"); coll.add("BB"); coll.add(123);//自动装箱 coll.add(new Date()); //size():获取添加的元素的个数 System.out.println(coll.size());//4 //addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中 Collection coll1=new Arraylist(); coll.add(456); coll.add("CC"); coll.addAll(coll1); System.out.println(coll1.size()); System.out.println(coll); //clear():清空集合元素 coll.clear(); //isEmpty():判断当前集合是否为空 System.out.println(coll.isEmpty()); }}
复制代码
🥂518 Collections 接口中的常用方法 2
测试类
向 Collections 接口的实现类的对象中添加数据 obj 时,要求 obj 所在类中重写 equals()
public class CollectionTest{ public void test1(){ Collection coll=new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(false); coll.add(new Person("Jerry",20)); //1.contains(Object obj):判断当前集合中是否包含obj //我们在判断时,会调用obj对象所在类的equals() boolean contains=coll.contains(123); System.out.println(contains); System.out.println(coll.contains(new String("Tom"))); System.out.println(coll.contains(new Person("Jerry",20))); //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中 Collection coll1=Arrays.asList(123,456); System.out.println(colls.containsAll(coll1)); }}
复制代码
Person 类
public class Person{ private String name; private int age; public Person(){ } public Person(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; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object o) { System.out.println("Person equals()...."); if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); }
@Override public int hashCode() {
return Objects.hash(name, age); }}
复制代码
🍣519 Collections 接口中的常用方法 2
测试类
# 🍱519 Collections接口中的常用方法3```javapublic void test2{ Collection coll=new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(false); coll.add(new Person("Jerry",20)); //hashCode():返回当前对象的哈希值 System.out.println(coll.hashCode()); //8.集合----数组 Object[] arr=coll.toArray(); for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } //拓展:数组---集合:调用Arrays类的静态方法asList() List<String> list=Arrays.asList(new String[]{"AA","BB","CC"}); System.out.println(list); //iterator():返回Iterator接口的实例,用于遍历集合元素}
复制代码
🎫521 使用 iterator 遍历 Collection
Iterator 对象称为迭代器(设计模式的一种),主要用于遍历 Collection 集合中的元素。GOF 给迭代器模式的定义为:提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节。迭代器模式,就是为容器而生。类似于“公交车上的售票员”、“火车上的乘务员”、“空姐”。Collection 接口继承了 java.lang.Iterable 接口,该接口有一个 iterator()方法,那么所有实现了 Collection 接口的集合类都有一个 iterator()方法,用以返回一个实现了 Iterator 接口的对象。Iterator 仅用于遍历集合,Iterator 本身并不提供承装对象的能力。如果需要创建 Iterator 对象,则必须有一个被迭代的集合。集合对象每次调用 iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。
public void IteatorTest{ public void test1{ Collection coll=new ArrayList(); coll.add(123); coll.add(456); coll.add(new String("Tom")); coll.add(false); coll.add(new Person("Jerry",20)); Iterator iterator=coll.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } }}
复制代码
👝522 迭代器 iterator 的执行原理
⚾523 iterator 遍历集合的两种错误写法
/** * 集合元素的遍历操作,使用迭代器Iterator接口 * 1.内部的方法:hasNext()和 next() * 2.集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。 */public class IteratorTest {
@Test public void test2(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false);
//错误方式一:// Iterator iterator = coll.iterator();// while(iterator.next() != null){// System.out.println(iterator.next());// }
//错误方式二: //集合对象每次调用iterator()方法都得到一个全新的迭代器对象,默认游标都在集合的第一个元素之前。 while(coll.iterator().hasNext()){ System.out.println(coll.iterator().next()); } }}
复制代码
🦺524 Iterator 迭代器 remove()的使用
public void test2(){ Collection coll = new ArrayList(); coll.add(123); coll.add(456); coll.add(new Person("Jerry",20)); coll.add(new String("Tom")); coll.add(false); //删除集合中的Tom Iterator iterator=coll.iterator(); while(iterator.hasNext()){ Object obj=iterator.next(); if("Tom".equals(obj)){ iterator.remove(); } } while(iterator.hasNext()){ System.out.println(iterator.next()); }}
复制代码
内部定义了 remove(),可以在遍历的过程,删除集合中的元素。此方法不同于集合直接调用 remove()
如果还未调用 next()或在上一次调用 next 方法之后已经调用了 remove 方法,再调用 remove 都会报 IllegalStateException、
好的,集合的内容我们就进行到这里,在后面的内容中呢我们也会应用到它,现在先对它有一个基础的了解
下期预告:新星计划 Day3 【JavaSE】集合 Part2🍩
觉得文章写的不错的亲亲们,点赞评论走一波,爱你们哦!🥗
评论