Collections的一点总结

   Collections类是用于操作集合的一个工具类。可以通过它对集合元素进行排序,查询,修改等操作。

首先,Collections类没有构造方法,所以只能用Collections.方法来调用方法。

package com.a;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class  Test5
{
	
	public static void main(String[] args) 
	  {
              
              List<Integer> list=new ArrayList(); 
              for(int i=0;i<10;i++){
            	  list.add(i);
              }
              
              Collections.reverse(list);//对list进行反转操作
              System.out.print("反转后: ");
              for(int a:list){
            	  System.out.print(a+" ");
              }
              System.out.print("\n");
              Collections.shuffle(list);  //对list进行随机排序
              System.out.print("随机后: ");
              for(int a:list){
            	  System.out.print(a+" ");
              }
              System.out.print("\n");
              Collections.sort(list);  //对list进行自然排序(从小到大)
              System.out.print("自然排序后: ");
              for(int a:list){
            	  System.out.print(a+" ");
              }
              System.out.print("\n");
              Collections.swap(list,3,5);  //对list进行换顺序
              System.out.print("交换后: ");
              for(int a:list){
            	  System.out.print(a+" ");
              }
              System.out.print("\n");
				
			
		}

   
}

以上为使用Collections对list集合进行排序的操作,此外,还能对集合进行查找,等功能。


最重要的,对于自定义类型,比如一个Person类,里边有name,age,如何根据age对集合进行排序

首先需要新建一个类,继承comparator接口,重写其compare方法,然后调用collections方法中的sotr(List list,comparator c)方法进行排序。

package com.a;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
class comp implements Comparator{

	@Override
	public int compare(Object arg0, Object arg1) {
		// TODO Auto-generated method stub
		Person1 p1=(Person1)arg0;
		Person1 p2=(Person1)arg1;
		if(p1.getAge()>p2.getAge()) return 1;
		else if(p1.getAge()<p2.getAge()) return -1;
		
		else return 0;
	}
	
}
public class  Test3 
{
	

     public static void main(String[] args)
	{
    	 ArrayList<Person1> list=new ArrayList();
    	 list.add(new Person1("jiengyh",30));
    	 list.add(new Person1("jiengyh1",29));
    	 list.add(new Person1("huazehui",25));
    	comp comparator=new comp();       
    	 Collections.sort(list, comparator);
    	 for(Person1 p:list){System.out.println(p.getName()+"  "+p.getAge());}

    	 
    	 
    	 
			
	
	}



}
class Person1{
	private String name;
	private int age;
	public Person1(String name,int age){
		this.name=name;
		this.age=age;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getAge() {
		return age;
	}
}


最后,Collections还提供了集合线程安全的解决办法 比如

List list=Collections.synchronizedList(new ArrayList);

这样建立起来的集合就是线程安全的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值