集合体学习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
         
    }
}
相关推荐
AD钙奶-lalala1 小时前
Mac OS上搭建 http server
java
TomCode先生2 小时前
c#动态树形表达式详解
开发语言·c#
高-老师2 小时前
基于R语言的物种气候生态位动态量化与分布特征模拟
开发语言·r语言·物种气候
大翻哥哥3 小时前
Python 2025:量化金融与智能交易的新纪元
开发语言·python·金融
weixin_437830943 小时前
使用冰狐智能辅助实现图形列表自动点击:OCR与HID技术详解
开发语言·javascript·ocr
鹿鹿学长4 小时前
2025年全国大学生数学建模竞赛(C题) 建模解析|婴儿染色体数学建模|小鹿学长带队指引全代码文章与思路
c语言·开发语言·数学建模
zhousenshan4 小时前
Python爬虫常用框架
开发语言·爬虫·python
皮皮林5515 小时前
SpringBoot 全局/局部双模式 Gzip 压缩实战:14MB GeoJSON 秒变 3MB
java·spring boot
weixin_456904275 小时前
Spring Boot 用户管理系统
java·spring boot·后端
趁你还年轻_5 小时前
异步编程CompletionService
java