Collection是单列集合的祖宗,它规定的方法(功能)是全部单列集合都会继承的
public class Work1 {
public static void main(String[] args) {
//简单认识一下Collection集合的特点
ArrayList list = new ArrayList<>();//有序 可重复 有索引
list.add("java1");
list.add("java2");
list.add("java1");
list.add("java2");
System.out.println(list);
System.out.println(list.get(1));
HashSet set = new HashSet<>();//无序 不重复 无索引
set.add("java1");
set.add("java2");
set.add("java1");
set.add("java2");
set.add("java3");
System.out.println(set);
}
}
1,public boolean add(E e):添加元素,添加成功返回true
Collection c = new ArrayList<>();//多态写法
c.add("java1");
c.add("java1");
c.add("java2");
c.add("java2");
c.add("java3");
System.out.println(c);//[java1,java1,java2,java2,java3]
2,public void clear():清空集合的元素
c.clear();
System.out.println(c);//[]
3,public boolean isEmpty():判断集合是否为空,是空返回true,反之
System.out.println(c.isEmpty());//flase
4,public int size():获取集合的大小
System.out.println(c.size());//5
5,public boolean contains(Objects obj):判断集合中是否包含某个元素
System.out.println(c.contains("java1"));//true
6,public boolean remove(E e):删除某个元素,删除成功返回true :如果有多个重复元素默认删除前面的第一个!
System.out.println(c.remove("java1"));
System.out.println(c);//[java1,java2,java2,java3]
7,public Object[] toArray():把集合转换成数组
Object[] arr = c.toArray();//把集合转换成数组
System.out.println(Arrays.toString(arr));
//转化成指定类型的数组形式
String[] arr1 = c.toArray(new String[c.size()]);
System.out.println(Arrays.toString(arr1));
8,把一个集合的全部数据倒入到另一个集合中去
Collection c1 = new ArrayList<>();
c1.add("java1");
c1.add("java2");
Collection c2 = new ArrayList<>();
c2.add("java3");
c2.add("java4");
c1.addAll(c2);//把c2集合的全部数据倒入(拷贝)到c1集合中去
System.out.println(c1);//[java1, java2, java3, java4]
System.out.println(c2);//[java3, java4]
迭代器 遍历数组
迭代器概述:迭代器是用来遍历集合的专用方式(数组没有迭代器),在Java中迭代器的代表是Iterator
import java.util.*;
public class Work1 {
public static void main(String[] args) {
Collection c = new ArrayList<>();//多态写法
c.add("赵敏");
c.add("小昭");
c.add("素素");
c.add("灭绝");
System.out.println(c);
//使用迭代器遍历数组
//1,从集合中获取迭代器对象
Iterator it = c.iterator();
//2,使用循环结合迭代器遍历集合
while (it.hasNext()){
String ele = it.next();
System.out.println(ele);
}
}
}
增强for循环 遍历数组
即能遍历循环,又能遍历数组
import java.util.*;
public class Work1 {
public static void main(String[] args) {
Collection c = new ArrayList<>();//多态写法
c.add("赵敏");
c.add("小昭");
c.add("素素");
c.add("灭绝");
System.out.println(c);
//增强for遍历集合
for (String ele : c){
System.out.println(ele);
}
System.out.println("..............................");
//增强for循环遍历数组
String[] names = {"简隋英","李玉","洛羿","温小辉"};
for (String name:names){
System.out.println(name);
}
}
}
Lambda表达式 遍历集合
import java.util.*;
import java.util.function.Consumer;
public class Work1 {
public static void main(String[] args) {
Collection c = new ArrayList<>();//多态写法
c.add("娘娘腔");
c.add("附加遗产");
c.add("你却爱着一个傻逼");
c.add("职业替身");
System.out.println(c);
c.forEach(new Consumer() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
}
}
案例:遍历集合中的自定义对象
需求:展示多部小说信息
分析:1,每本小说都是一个对象,多部小说要使用集合装起来
2,遍历集合中的三个小说对象,输出每部电影的详情信息
import java.util.*;
public class Work1 {
public static void main(String[] args) {
Collection movies = new ArrayList<>();
//往集合中添加对象.................................
movies.add(new movie("《人鱼陷落》",9.5,"兰波"));
movies.add(new movie("《德萨罗人鱼》",9.3,"德萨罗"));
movies.add(new movie("《垂耳执事》",9.5,"言逸"));
System.out.println(movies);
//使用 增强for循环 遍历集合..........................
for (movie m : movies) {
System.out.println("名称:"+m.getName());
System.out.println("评分:"+m.getScore());
System.out.println("人物:"+m.getActor());
}
}
}
class movie{
private String name;
private double score;
private String actor;
public movie() {
}
public movie(String name, double score, String actor) {
this.name = name;
this.score = score;
this.actor = actor;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getActor() {
return actor;
}
//重写toString方法....................
@Override
public String toString() {
return "movie{" +
"name='" + name + '\'' +
", score=" + score +
", actor='" + actor + '\'' +
'}';
}
}