集合体学习01

集合体系结构

Collection 单列集合 Map 双列集合

Collection

1.List
1.ArrayList
2.LinkedList
3.Vector
2.Set
1.HashSet
1.LinkedHashSet
2.TreeSet

其中Collection,List,Set 为接口,其余为实现类。

List系列集合:添加的元素是有序,可重复,有索引

Set系列集合:添加的元素是无序,不可重复,无索引

Collection是单列集合的祖宗接口,它的功能是全部单列集合都可以继承的。

复制代码
public boolean add(E e)    把给定对象添加到集合中
复制代码
public void clear()       清空集合中所有元素
复制代码
public boolean remove(E e)   把给定的目标在当前集合中删除
复制代码
public boolean contains(Object obj)    判断当前集合中是否包含给定对象
复制代码
public boolean isEmpty()     判断当前集合是否为空
复制代码
public int size()      返回集合中的元素个数/集合长度            

注意点:Collection是一个接口,不能直接创建他的对象,所以在学习他的方法时,只能创建他的实现类的对象。

1.添加元素

复制代码
public class Collection{
    public static void main(String[]args){
        Collection<String> coll=new ArrayList<>();
        
        
        coll.add("aaa");
        System.out.println(coll);//[aaa]
         
    }
}

返回值细节:

1.如果我们在往List系列集合中添加数据,那么永远返回true,因为List系列的元素是可重复的。

2.如果我们往Set系列集合中添加数据,如果当前添加的数据不存在则返回值为true,如果数据已经存在则返回值为false,因为Set系列集合是不允许重复的。

2.清空元素

复制代码
public class Collection{
    public static void main(String[]args){
        Collection<String> coll=new ArrayList<>();
        
        
        coll.add("aaa");
        coll.add("bbb");
        
        coll.clear();
        System.out.println(coll);//[]
         
    }
}

3.删除元素

复制代码
public class Collection{
    public static void main(String[]args){
        Collection<String> coll=new ArrayList<>();
        
        
        coll.add("aaa");
        coll.add("bbb");
        coll.add("ccc");
        
        coll.remove("aaa");
        System.out.println(coll);//[bbb ccc]
         
    }
}

方法会有一个布尔类型的返回值,删除成功返回true,删除失败返回false

如果要删除的元素不存在则删除失败

4.判断元素是否包含

复制代码
public class Collection{
    public static void main(String[]args){
        Collection<String> coll=new ArrayList<>();
        
        
        coll.add("aaa");
        coll.add("bbb");
        coll.add("ccc");
        coll.remove("aaa");
        
        boolean result=coll.contains("aaa");
        System.out.println(result);//false
         
    }
}

contains方法底层是依赖equals方法进行判断是否存在,所以如果集合存在自定义对象,也想通过contains进行判断是否存在,那么需要在javabean中重写equals方法。

复制代码
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 this.name;
    }
    
    public int getAge(){
        return this.age;
    }
    
    public void setName(String name){
        this.name=name;
    }
    
    public void setAge(int age){
        this.age=age;
    }
    
}
复制代码
import java.util.Collection;
​
public class Collection{
    public static void main(String[]args){
        //1.创建集合对象
        Collection<String> coll=new ArrayList<>();
        
        //2.创建三个学生对象
        Student s1=new Student("zhangsan",23);
        Student s2=new Student("lisi",24);
        Student s3=new Student("wanwu",25);
        
        //3.把学生对象添加到集合中
        coll.add(s1);
        coll.add(s2);
        coll.add(s3);
        
        //4.判断集合中某一个学生对象是否存在
        Student s4=new Student("zhangsan",23);
        //如果同姓名同年龄,就认为是同一个学生
        
       System.out.println(coll.conains(s4));//false
        
        //这里返回值为false,contains方法用equals方法判断对象是否一致
        //如果是自定义对象,没有重写equals方法,那么默认使用Object类中的equals方法进行判断
        //Object类中的equals是依赖地址值进行判断的
        //所以要在自定义的javabean中重写equals类
        
        //重写之后
        System.out.println(coll.contains(s4));//true
        
    }
}
复制代码
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 this.name;
    }
    
    public int getAge(){
        return this.age;
    }
    
    public void setName(String name){
        this.name=name;
    }
    
    public void setAge(int age){
        this.age=age;
    }
    
    
    //equals重写
    
    public boolean equals(Object o){
        if(this==o){
            return true;
        }
        if(o==null||getClass()!=o.getClass()){
            return false;
        }
        Student student=(Student) o;
        return age==student.age &&Object.equals(name,student.name);
    }
    
    
    
    
    
}

5.判断集合是否为空

复制代码
public class Collection{
    public static void main(String[]args){
        Collection<String> coll=new ArrayList<>();
        
        
        coll.add("aaa");
        coll.add("bbb");
        coll.add("ccc");
        
        boolean result=coll.isEmpty();
        System.out.println(result);//false
         
    }
}

6.获取集合长度

复制代码
public class Collection{
    public static void main(String[]args){
        Collection<String> coll=new ArrayList<>();
        
        
        coll.add("aaa");
        coll.add("bbb");
        coll.add("ccc");
        
        int size=coll.size();
        System.out.println(size);//3
         
    }
}
相关推荐
Sylvia-girl2 小时前
Java——抽象类
java·开发语言
Yana.nice4 小时前
Bash函数详解
开发语言·chrome·bash
Touper.5 小时前
Redis 基础详细介绍(Redis简单介绍,命令行客户端,Redis 命令,Java客户端)
java·数据库·redis
m0_535064605 小时前
C++模版编程:类模版与继承
java·jvm·c++
虾条_花吹雪6 小时前
Using Spring for Apache Pulsar:Message Production
java·ai·中间件
tomorrow.hello6 小时前
Java并发测试工具
java·开发语言·测试工具
Moso_Rx6 小时前
javaEE——synchronized关键字
java·java-ee
晓13136 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊6 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
张小洛6 小时前
Spring AOP 是如何生效的(入口源码级解析)?
java·后端·spring