集合体学习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
         
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值