单列集合Collection(实现类ArrayList)

Collection接口

        java.util.Collection是所有单列集合的父接口,因此在Collection接口中定义了单列集合List和Set通用的一些方法,如下:

方法说明功能描述
boolean add(Object o)向集合中添加一个元素
boolean addAll(Collection c) 增加一个集合(多个元素)
void clear()清空集合
boolean remove(Object o)删除集合中的一个对象(元素)
boolean removeAll(Collection c)删除一个集合(多个元素)
boolean isEmpty()是不是空的
boolan contains(Object o)判断集合中有没有这个元素
boolean containAll(Collection c)判断集合中有没有参数集合
Iterator iterator()返回一个遍历迭代器
int size()返回集合的元素个数

(1)Collection List Set Map都为接口;

 (2)其中Collection为单列集合接口,List和Set继承Collection,其实现类有ArrayList(非线程安全,推荐单线程中使用)、Vector(线程安全)、LinkedList、HashSet、TreeSet

 (3)Map为双列集合接口,其实现类有HashMap非线程安全,推荐单线程中使用)、Hashtable(线程安全)、TreeMap

List接口实现类ArrayList

        List接口继承自Collection接口,是单列集合的一个重要分支。ArrayList是List接口的一个实现类,相当于动态数组,其遍历查询数据效率快。

        实例化ArrayList以及添加、删除、判断等方法的使用

public class ArrayList2 {
    public static void main(String[] args) {
//实例化ArrayList对象
        List<Integer> list = new ArrayList<>();
        //添加(ArrayList添加删除效率低)
        list.add(1000);
        list.addAll(List.of(11, 22, 33));
        list.addAll(0, List.of(111, 222, 333));
        list.addAll(0, List.of(111, 222, 333));
        list.addAll(0, List.of(111, 222, 333));
        System.out.println(list);
        System.out.println(list.size());
        //判断
        System.out.println(list.contains(11));//true
        System.out.println(list.indexOf(11));//7
        System.out.println(list.indexOf(112));//-1
        System.out.println(list.containsAll(List.of(118, 22, 33)));//判断一个集合在不在另一个集合中
        System.out.println(list.lastIndexOf(11));
        //修改
        list.set(0, 1100);
        list.set(2, 2);
        System.out.println(list);
        //list.replaceAll(e->e+10);
        list.replaceAll(e -> e == 222 ? 555 : e);
        System.out.println(list);
        //删除
        //list.cleaar();
        int id = 2;
        list.remove(id);//删除索引位置
        Integer ids = 222;
        list.remove(ids);//删除对象
        System.out.println(list);
        list.removeAll(List.of(222));//删除所以对象222
        System.out.println(list);
        list.removeIf(e -> e == 111 || e == 333);//lambda表达式删除所有的111 333
        list.removeIf(e -> Math.random() > .5);//随机删除元素
        System.out.println(list);
        //list.removeIf(e -> true);//清空
        //System.out.println(list);

         ArrayList集合的遍历:

//遍历
//增强for 自动拆箱
for (int i : list) {
    System.out.println(i);
}
System.out.println("==========================");
//enhanced for循环
for (Integer integer : list) {
    System.out.println(integer);
}
//for循环
System.out.println("==========================");
for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i));
}
//倒着循环输出
System.out.println("==========================");
for (int i = list.size() - 1; i >= 0; i--) {
    System.out.println(list.get(i));
}
System.out.println("==========================");
//lambda表达式,通过foreach()方法
list.forEach(System.out::println);
System.out.println("==========================");
list.forEach(e ->
{
    if (e == 555) {
        return;//forEach()是方法,不是循环语句,不可以使用break continue,但可以使用return
    }
    System.out.println(e);
});

ArrayList集合的排序:

(1)数字直接使用Collections工具类中的sort()方法进行数字排序,将排序规则写在sort(规则),对于数字使用lambda表达式排序时用(a,b)->b-a表示降序排列。

(2)字符串

public class ArrayList4 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>(List.of("java", "php", "javascript", "c"));
        System.out.println(list);
        //洗牌
        Collections.shuffle(list);
        System.out.println(list);
        //升序根据字母顺序
        Collections.sort(list);
        System.out.println(list);
        //降序根据字母顺序
       // Collections.sort(list, (a, b) -> b.compareTo(a));
        Collections.sort(list, Comparator.reverseOrder());
        System.out.println(list);
        //按字符串字符个数升序排序
        Collections.sort(list,(a,b)->a.length()-b.length());
        Collections.sort(list, Comparator.comparingInt(String::length));
        System.out.println(list);
        //按字符串字符个数降序排序
        Collections.sort(list,(a,b)->b.length()-a.length());
        System.out.println(list);

    }
}

运行结果:

(3)自定义对象(如果对象使用集合工具的排序方法,此类必须实现 Comparable<Student> 接口,并实现comparaTo()方法)

User类:

public class User implements Comparable<User>{
    private int id;
    private String name;
    private int age;
    private LocalDate brith;
    public User() {
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", brith=" + brith +
                '}';
    }
    public User(int id, String name, int age, LocalDate brith) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.brith = brith;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public LocalDate getBrith() {
        return brith;
    }
    public void setBrith(LocalDate brith) {
        this.brith = brith;
    }
    @Override
    public int compareTo(User o) {
        return id-o.id;
    }
}

ArrayList5对自定义对象排序:

public class ArrayList5 {
    public static void main(String[] args) {
        List<User> list = new ArrayList<>(List.of(new User(1, "李四", 18, LocalDate.of(2001, 6, 1)), new User(15, "万物", 28, LocalDate.of(2031, 3, 11)), new User(41, "王五", 15, LocalDate.of(2029, 4, 23)), new User(21, "赵柳", 1, LocalDate.of(2003, 1, 31)), new User(11, "周八", 10, LocalDate.of(2011, 9, 22))));
        System.out.println(list);
        //洗牌
        Collections.shuffle(list);
        System.out.println(list);
        //按id升序排序
        Collections.sort(list);
        System.out.println(list);
        //按年龄升序排序
        Collections.sort(list, (a, b) -> a.getAge() - b.getAge());
        System.out.println(list);
        //按年龄降序排序
        Collections.sort(list, (a, b) -> b.getAge() - a.getAge());
        System.out.println(list);
        //按日期升序排序
        Collections.sort(list, (a, b) -> b.getBrith().compareTo(a.getBrith()));
        System.out.println(list);
    }
}

运行结果:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值