集合:Collection

集合

Collection:

public class cc1Collection {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();//实现子类的方法
        //像ArrayList里面的add添加,contains判断,删除等一系列操作
        collection.add("a");
        collection.remove("a");//删除元素a
         boolean a = collection.contains("a");//存在为true。
        System.out.println(collection);
    }

Iterator: 迭代器,适用于集合元素的获取
正常的集合像ArrayList这种下标有序的集合,我们可以用for循环进行遍历获取元素,但是像有些集合的元素下标不是那种有序的,这个时候就要用到迭代器进行获取,或者还可以用增强for进行获取。

//next 获取下一个元素
//hasNext() 判断迭代器中是否更多的元素,返回true
public class cc2Iterator {
    public static void main(String[] args) {
        Collection<String > collection = new ArrayList<>();
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add("d");
        System.out.println(collection);
        Iterator<String> iterator = collection.iterator(); //迭代器容器创建
       // String next=iterator.next();
       // System.out.println(next);  //获取下一个元素 ,重下标0开始
        // hashNext() 返回值为true 所以定义为布尔值
        while (iterator.hasNext()) {
            String next =  iterator.next();
            System.out.println(next);
        }
        //foreach 增强for
        for (String s : collection) {
            System.out.println(s);
        }
    }
}

Lsit 与LinkedList:大体上都属于Collection的子类但是拥有一些特殊的方法。

//List
import java.util.*;
public class cc3List {
    public static void main(String[] args) {
    List<String> list=new ArrayList<>();
    list.add("1");
    list.add("w");
    list.add(1,"2");//进行特定的下标位置添加元素
    list.remove(2); //进行特定的下标元素删除
    }
}
//------------
//------------
//LinkedList
import java.util.LinkedList;

public class cc4LinkedList {
    public static void main(String[] args) {
        LinkedList<String> linkedList= new LinkedList<>();
        linkedList.addFirst("a");
        linkedList.addLast("c");//在列表的头、尾进行添加
        linkedList.getFirst();//获取列表首个元素,Lsat获得列表末尾元素
        linkedList.getLast();
        linkedList.removeFirst();
        linkedList.removeLast(); // 删除列表首尾元素
    }
}

Collection分为两个不同的子类集合List与Set,主要的 区别是List能够元素重复,而Set不能元素重复

Collection<String > collection = new ArrayList<>();
        collection.add("a");
        collection.add("b");
        collection.add("c");
        collection.add("d");
        System.out.println(collection);
        Collection<String> ppt = new HashSet<>();//哈希覆盖!
        ppt.add("a");
        ppt.add("b");
        ppt.add("a");
        ppt.add("b");
        System.out.println(ppt);

在这里插入图片描述这里的输出结果就可以看出来二者的区别。

TreeSet怎样进行一定规则有序的元素获取:
两种方法一种是写一个类进行Comparable接口的实现
利用compareTo方法进行比较判断

//创建一个类Student实现接口Comparable里的方法compareTo
import java.util.HashMap;

public class Student implements Comparable<Student> {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Student(){};

    public Student(String name){this.name=name;}

    @Override
    public int compareTo(Student o) {
        int i = this.name.compareTo(o.name);//this.name指插入元素,o.name指存在的元素
        return i;
    }

}

//创建一个Test进行实现
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        Student zl = new Student("zs");
        Student zs = new Student("ls");
        // 去重 元素
        TreeSet<Student> students = new TreeSet<>();
        students.add(zs);
        students.add(zl);
        for (Student student : students) {//使用增强for获取
            System.out.println(student.getName() );
        }

    }
}

在这里插入图片描述
很明显可以看出首字母L在Z的前面,所以ls排在zs前面。

另外一种获取一定规则元素排序的方法是Comparetor

import java.util.Comparator;
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet<Student> students = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int i = o1.getAge() - o2.getAge();
                return i;
            }
        });
        Student student = new Student(10);
        Student student1 = new Student(11);
        Student student2 = new Student(12);
        Student student3 = new Student(1);
        students.add(student);
        students.add(student1);
        students.add(student2);
        students.add(student3);
        for (Student student4 : students) {
            System.out.println(student4.getAge());
        }
    }
}

其实两种方法的思想都是一样的,只不过一个是在类中一个是直接在main函数中直接写。
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值