回顾用(集合collection)

Collection

Collection中的主要方法

1.add

功能说明:向集合中添加元素

		Collection <String> coll = new ArrayList<>();
        System.out.println(coll);
        coll.add("张三");
        coll.add("李四");
        coll.add("王武");
        coll.add("赵六");
        System.out.println(coll);//已经重写了toString方法

2.remove

功能说明:用来将集合中的某项元素移除

		Collection <String> coll = new ArrayList<>();
		coll.remove("王武");
		System.out.println(coll);

3.contains (这里返回的是一个布尔值)

功能说明:用来验证集合中是否含有某项元素

		boolean flag = coll.contains("王武");
        System.out.println(flag);
        boolean flag2 = coll.contains("赵六");
        System.out.println(flag2);

4.isEmpty

功能说明:用来判断是否为空,如果为空返回ture,否则返回false

		boolean flag3 = coll.isEmpty();
        System.out.println(flag3);

5.size

功能说明:用来记录集合的大小,即其中所含元素的个数

		int size = coll.size();
        System.out.println(size);

6.toArray

功能说明:用来将集合转化为数组

  		Collection <String> coll = new ArrayList<>();
        System.out.println(coll);
        coll.add("张三");
        coll.add("李四");
        coll.add("王武");
        coll.add("赵六");
		Object[] arr = coll.toArray();
        System.out.print("[");
        for (int i = 0; i <arr.length ; i++) {
            if (i==arr.length-1){
                System.out.print(arr[i]);
            }else{
                System.out.print(arr[i]+",");
            }
        }
        System.out.print("]");

    }

测试结果:
在这里插入图片描述

Collections

Collections中的主要方法

1.addAll

功能描述:该方法是向集合中一次添加多个元素

  		List <Integer> list =new ArrayList<>();
        Collections.addAll(list,1,2,3,4,5);
        System.out.println(list);

2.shuffle

功能描述:用来打乱集合中的元素,使其随机排列,一般比如斗地主之类将牌随机打乱等会用到该方法

		Collections.shuffle(list);
        System.out.println(list);

3.sort

功能描述:该方法为排序方法,将集合内的元素按照一定的顺序排序,不过要注意的是该方法内部已经实现了Comparable接口, 如果自己使用sort方法比较其他的数据类型的时候,应该实现Comparable接口

 		Collections.sort(list);
        System.out.println(list);

若是想要比较自己创建的引用类型,需要实现Comparable接口,重写compareTo方法,例如下面直接建立一个Person类进行测试

package com.massz;
import java.io.Serializable;
import java.util.Objects;
/**
 * @Author: mcc
 * @Date: 2021/7/30
 * @Description: com.massz
 * @version: 1.0
 */
public class Person implements Serializable,Comparable<Person> {
   private int age;
   private String name;

    public Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Person() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age;
    }

    @Override
    public int hashCode() {
        return Objects.hash(age);
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
	/*
	*按照年龄排序的自定义方法
	*/
    @Override
    public int compareTo(Person o) {
        return this.getAge()-o.getAge();
    }
}

Test类:

 List <Person> list =new ArrayList<>();
        list.add(new Person(56,"zs"));
        Collections.addAll(list,new Person(12,"zhangsan"),new Person(23,"listi"));
        Collections.sort(list);
        System.out.println(list);

        for (Person person : list) {
            System.out.println(person);
        }

测试结果,可以按照年龄正确排序
在这里插入图片描述

简述Comparable和Comparator两个接口的区别。

Comparable:强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的compareTo方法 被称为它的自然比较方法。只能在类中实现compareTo()一次,不能经常修改类的代码实现自己想要的排序。实现 此接口的对象列表(和数组)可以通过Collections.sort(和Arrays.sort)进行自动排序,对象可以用作有序映射中 的键或有序集合中的元素,无需指定比较器。
Comparator强行对某个对象进行整体排序。可以将Comparator 传递给sort方法(如Collections.sort或 Arrays.sort),从而允许在排序顺序上实现精确控制。还可以使用Comparator来控制某些数据结构(如有序set或 有序映射)的顺序,或者为那些没有自然顺序的对象collection提供排序。

集合的遍历(迭代器的使用)

		Collection<String> coll = new ArrayList<>();
        coll.add("张三");
        coll.add("李四");
        coll.add("王武");
        coll.add("赵六");
        System.out.print("[");
        Iterator<String> it = coll.iterator();
        while (it.hasNext()){
            String next = it.next();
            System.out.print(next);
        }

集合的遍历也可以使用增强for

		Collection<String> coll = new ArrayList<>();
        coll.add("张三");
        coll.add("李四");
        coll.add("王武");
        coll.add("赵六");
        for (String s : coll) {
            System.out.println(s);
        }

Collection与Collections有什么区别?

1.Collection是最基本的集合接口,Collection派生了两个子接口,List和Set,分别定义了两种不同的存储方式

2.Collectons是一个包装类,他包含各种有关集合操作的静态方法
3.此类不能实例化,就像是一个工具类,服务于Collection框架

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sli_Pink

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值