集合体学习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
         
    }
}
相关推荐
蓝黑20207 分钟前
Java知识点小结3:内存回收
java·gc
KookeeyLena87 分钟前
如何限制任何爬虫爬取网站的图片
开发语言·c++·爬虫
yanyanwenmeng27 分钟前
matlab基础
开发语言·算法·matlab
Yz987627 分钟前
Hadoop里面MapReduce的序列化与Java序列化比较
java·大数据·jvm·hadoop·分布式·mapreduce·big data
凯哥Java29 分钟前
优化批处理流程:自定义BatchProcessorUtils的设计与应用
java·数据库·mysql
njnu@liyong38 分钟前
AOP-前置原理-怎么判断和拦截?
java·aop·拦截
末央&43 分钟前
【C++】内存管理
java·开发语言·c++
不是仙人的闲人1 小时前
Qt日志输出及QsLog日志库
开发语言·数据库·qt
八了个戒1 小时前
【TypeScript入坑】TypeScript 的复杂类型「Interface 接口、class类、Enum枚举、Generics泛型、类型断言」
开发语言·前端·javascript·面试·typescript
心之语歌1 小时前
设计模式 享元模式(Flyweight Pattern)
java·设计模式·享元模式