Java类集框架基础-Collection

集合类又被称作容器,常见集合:List集合,Set集合和Map集合,而List集合和Set集合继承Collection集合。
Collection集合
是进行单对象保存的最大父接口,每次只能保存一个对象。通常来说无法进行直接使用,但是提供了多种方法:add(),remove(),isEmpty(),iterator(),size()。由于List接口和Set接口都继承于Collection接口,所以是通用的方法。
使用while方法输出

 public static void main(String[] args){
        Collection<String> col=new ArrayList<String>();
        col.add("阿珍");
        col.add("爱上了");
        col.add("阿强");
        Iterator<String> ite=col.iterator();
        while (ite.hasNext()){
            String str=ite.next();
            System.out.print(str);

        }



    }

程序结果:阿珍爱上了阿强
List接口
List接口常见的有ArrayList和LinkList,并且可以保存重复数据。相比Collection接口,LIst接口扩充了几个方法:get() 取得指定索引编号内容,set() 修改指定索引位置 ,ListIterator() 实例化ListIterrator。

  public static void main(String[] args){
        List<String> col=new ArrayList<String>();
        col.add("阿珍");
        col.add("爱上了");
        col.add("阿强");
        System.out.println("是否为空:"+col.isEmpty()+"长度:"+col.size());
        for (int y=0;y<col.size();y++){
            String str=col.get(y);
            System.out.print(str);
            if (str.equals("阿强")){
                System.out.print("\n");
            }
        }

        System.out.println("获得索引为2的元素:"+col.get(2));
        System.out.println("设置索引为2的元素:"+col.set(2,"啊明"));
        for (int y=0;y<col.size();y++){
            String str=col.get(y);
            System.out.print(str);
        }




    }

程序结果:
是否为空:false长度:3
阿珍爱上了阿强
获得索引为2的元素:阿强
设置索引为2的元素:阿强
阿珍爱上了啊明
索引位置与数组相同从0开始。
大部分的操作都是保存系统给定的类型,但是也可以保存自己定义的类型。


class Book{
    private String title;
    private double price;
    public Book(String title,double price){
        this.title=title;
        this.price=price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        return Double.compare(book.price, price) == 0 &&
                Objects.equals(title, book.title);
    }

    @Override
    public int hashCode() {
        return Objects.hash(title, price);
    }
    @Override
    public String toString(){
        return "书名:"+this.title+"价格:"+this.price;
    }
}
public class TestDemo2 {
    public static void main(String[] args){
        List<Book> col=new ArrayList<Book>();
        col.add(new Book("Java学习",25.9));
        col.add(new Book("JS学习",79.9));
        col.add(new Book("Java Web学习",22.5));
        col.remove(new Book("Java Web学习",22.5) );
        System.out.println(col);


    }
}

程序结果:[书名:Java学习价格:25.9, 书名:JS学习价格:79.9]
set接口
Set接口同样继承于Collection接口,但是没有方法扩充,无法使用get()方法,数据不允许重复。Set()接口有两个常用实现类:HashSet()和TreeSet()。HashSet有哈希表支持,不能保持数据有序存放,TreeSet则可以有序的,默认字母递增。
HashSet()

public static void main(String[] args){
     Set<String> set=new HashSet<String>();
     set.add("阿珍");
     set.add("爱上了");
     set.add("阿强");
     System.out.println(set);



    }

程序结果:[阿强, 爱上了, 阿珍]
属于无序排列。

 public static void main(String[] args){
     Set<String> set=new TreeSet<>();
     set.add("B");
     set.add("A");
     set.add("C");
     System.out.println(set);



    }

程序结果:[A, B, C]
默认按照字母顺序升序排列。
TreeSet()排序可以是定义的类对象,排序与消除重复元素需要比较器Comparable。


class Book implements Comparable<Book>{
    private String title;
    private double price;
    public Book(String title,double price){
        this.title=title;
        this.price=price;
    }

    @Override
    public int compareTo(Book o) {
        if (this.price>o.price){
            return  1;
        }else if (this.price<o.price){
            return -1;
        }else {
            return this.title.compareTo(o.title);
        }
    }
    @Override
    public String toString(){
        return "书名:"+this.title+"价格:"+this.price;
    }
}
public class TestDemo2 {
    public static void main(String[] args){
        Set<Book> set=new TreeSet<Book>();
        set.add(new Book("Java学习",25.9));
          set.add(new Book("Java学习",25.9)); //重复元素
        set.add(new Book("Java Web学习",89.9));
        set.add(new Book("JavaScript学习",80));
        System.out.println(set);



    }

程序结果:[书名:Java学习价格:25.9, 书名:JavaScript学习价格:80.0, 书名:Java Web学习价格:89.9]
TrseeSet排序方法只适合支持排序类集操作环境,其他需要hashCode(),equls()方法(鼠标右键,Generate,Override,选中自动生成)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值