Collections 常用方法以及静态方法的使用

  • Collection:单列集合类的根接口,用于存储一系列符合某种规则的元素,它有两个重要的子接口,分别是java.util.Listjava.util.Set
    • List的特点是元素有序、元素可重复。List接口的主要实现类有java.util.ArrayListjava.util.LinkedList
    • Set的特点是元素无序,而且不可重复。Set接口的主要实现类有java.util.HashSetjava.util.TreeSet

常用方法

public boolean add(E e)			把给定的对象添加到当前集合中 。
public void clear()				清空集合中所有的元素。
public boolean remove(E e)  	把给定的对象在当前集合中删除。
public boolean contains(E e)	判断当前集合中是否包含给定的对象。
public boolean isEmpty() 		判断当前集合是否为空。
public int size() 				返回集合中元素的个数。
public Object[] toArray() 		把集合中的元素,存储到数组中。

import java.util.ArrayList;
import java.util.Collection;

public class Demo1Collection {
    public static void main(String[] args) {
		// 创建集合对象 
    	// 使用多态形式
    	Collection<String> coll = new ArrayList<String>();
		//Collection<String> coll = new HashSet<String>(); 也是可以使用,并且不需要更改下面的代码
    	// 使用方法
    	// 添加功能  boolean  add(String s)
    	coll.add("小李广");
    	coll.add("扫地僧");
    	coll.add("石破天");
    	System.out.println(coll);

    	// boolean contains(E e) 判断o是否在集合中存在
    	System.out.println("判断  扫地僧 是否在集合中"+coll.contains("扫地僧"));

    	//boolean remove(E e) 删除在集合中的o元素
    	System.out.println("删除石破天:"+coll.remove("石破天"));
    	System.out.println("操作之后集合中元素:"+coll);
    	
    	// size() 集合中有几个元素
		System.out.println("集合中有"+coll.size()+"个元素");

		// Object[] toArray()转换成一个Object数组
    	Object[] objects = coll.toArray();
    	// 遍历数组
    	for (int i = 0; i < objects.length; i++) {
			System.out.println(objects[i]);
		}

		// void  clear() 清空集合
		coll.clear();
		System.out.println("集合中内容为:"+coll);
		// boolean  isEmpty()  判断是否为空
		System.out.println(coll.isEmpty());  	
	}
}

静态方法: 直接使用 Collections 调用

public static <T> boolean addAll(Collection<T> c, T... elements)    往集合中添加一些元素。
public static void shuffle(List<?> list) 							打乱顺序`:打乱集合顺序。
public static <T> void sort(List<T> list) 							将集合中元素按照默认规则排序。
当使用自定义对象时,使用 sort 方法必须在类中实现 Comparable 接口,重写 CompareTo 方法
升序:this.get方法 - o.get方法

public static <T> void sort(List<T> list,Comparator<? super T> )  	将集合中元素按照指定规则排序。
升序 o1 -o2
定义一个 Person 类 
省略 构造方法以及 get/set方法(实际代码中需要写)
public class Person implement comperable<Person>{
	private name;
	private age;

	//重写comperTo
	@Override
    public int compareTo(Person o) {
        return this.getAge() - o.getAge();
    }
}
 
main 方法
public class CollectionsDemo {
    public static void main(String[] args) {
		//定义一个ArrayList<Integer>集合,Collecton 是一个接口,ArrayList 属于他的实现类
        ArrayList<Integer> arrayListInt = new ArrayList<>();
        //调用Collections.addAll
        Collections.addAll(arrayListInt,1, 2, 3, 4, 5, 6,7);
        System.out.println(arrayListInt);

        //调用Collections.shuffle()
        Collections.shuffle(arrayListInt);
        System.out.println(arrayListInt);
        //调用Collections.sort,排序整型List
        Collections.sort(arrayListInt);
        System.out.println(arrayListInt);

        //调用Collections.sort,排序Person list
        ArrayList<Person> arrayListPerson = new ArrayList<>();
        arrayListPerson.add(new Person("张三",15));
        arrayListPerson.add(new Person("李四",18));
        arrayListPerson.add(new Person("王五",16));

        System.out.println("排序前:" + arrayListPerson);
        Collections.sort(arrayListPerson);
        System.out.println("排序后:" + arrayListPerson);

        //扰乱排序,测试 comperator 方法
        Collections.shuffle(arrayListPerson);
        System.out.println("扰乱后:" + arrayListPerson);

        Collections.sort(arrayListPerson, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        System.out.println("comperator方法排序后:" + arrayListPerson);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值