集合体学习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
         
    }
}
相关推荐
万粉变现经纪人几秒前
如何解决 pip install -r requirements.txt 无效可编辑项 ‘e .‘(-e 拼写错误)问题
开发语言·python·r语言·beautifulsoup·pandas·pip·scipy
say_fall16 分钟前
精通C语言(2.结构体)(内含彩虹)
c语言·开发语言·windows
潇凝子潇16 分钟前
在使用Nacos作为注册中心和配置中心时,如何解决服务发现延迟或配置更新不及时的问题
开发语言·python·服务发现
泉城老铁29 分钟前
springboot实现对接poi 导出excel折线图
java·spring boot·后端
金銀銅鐵1 小时前
[Java] 如何自动生成简单的 Mermaid 类图
java·后端
纵横八荒1 小时前
Java基础加强13-集合框架、Stream流
java·开发语言
稚辉君.MCA_P8_Java1 小时前
kafka解决了什么问题?mmap 和sendfile
java·spring boot·分布式·kafka·kubernetes
乄bluefox1 小时前
保姆级docker部署nacos集群
java·docker·容器
欣然~1 小时前
百度地图收藏地址提取与格式转换工具 说明文档
java·开发语言·dubbo
William_cl2 小时前
C# MVC 修复DataTable时间排序以及中英文系统的时间筛选问题
开发语言·c#·mvc