实现Comparator接口和Comparable接口,以及Map按value排序 ,map遍历

继承Comparator接口,重写compare()方法
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
 
 
class Student implements Comparator<Student>{
	String name;
	int age;
	int id;
	public Student(){}
	public Student(String name,int age,int id)
	{
		this.name=name;
		this.age=age;
		this.id=id;
	}
	@Override
	public int compare(Student o1, Student o2) {
		// TODO Auto-generated method stub
		return o1.age-o2.age;
	}
}
 
public class Test {
 
	public static void main(String[] args) {
		Random rand=new Random();
		List<Student> list=new ArrayList<Student>();
		for(int i=0;i<20;i++)
		{
			Student ss=new Student("long-jing-wen-"+i,rand.nextInt(100),rand.nextInt(1000));
			list.add(ss);
			
		}
		
		Student student=new Student();
		Collections.sort(list, student);

  

继承Comparable,重写compareTo()方法
package thread;


public class stu implements  Comparable<stu>{
	public  int id;
	public stu() {
		

	}
	public stu(int id) {
	
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Override
	public String toString() {
		return "stu [id=" + id + "]";
	}


	public int compareTo(stu o1, stu o2) {
		// TODO Auto-generated method stub
		return o1.id-o2.id;
	}

	@Override
	public int compareTo(stu o) {
		// TODO Auto-generated method stub
		return this.id-o.id;
	}

}

		Random rand=new Random();		
		stu[] stu=new stu[20];
		
		
		for(int i=0;i<20;i++)
		{
			stu ss2=new stu(rand.nextInt(100));
			stu[i]=ss2;
		}
		
		
		Arrays.sort(stu);
		
		
		
		
		for(int i=0;i<stu.length;i++)
		{
			System.out.print(stu[i]+" ");
		}
		System.out.println();

  

Map按value排序
HashMap<String, Long> map = new HashMap<String, Long>();
	
		map.put("A", (long) 99);
		map.put("B", (long) 67);
		map.put("C", (long) 109);
		map.put("D", (long) 2);
 
		System.out.println("unsorted map: " + map);
 
		List<Map.Entry<String, Long>> list = new ArrayList<>(map.entrySet());
		Collections.sort(list, new Comparator<Map.Entry<String, Long>>() {
			public int compare(Map.Entry<String, Long> o1,
					Map.Entry<String, Long> o2) {
				return (int) (o2.getValue()-o1.getValue() );
			}
		});
 
		System.out.println("results: " + list);

  

Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
//遍历map中的键 
for (Integer key : map.keySet()) { 
  System.out.println("Key = " + key); 
} 
//遍历map中的值 
for (Integer value : map.values()) { 
  System.out.println("Value = " + value); 
}

该方法比entrySet遍历在性能上稍好(快了10%),而且代码更加干净。


使用泛型:
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); 
while (entries.hasNext()) { 
  Map.Entry<Integer, Integer> entry = entries.next(); 
  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
}

不使用泛型:
Map map = new HashMap(); 
Iterator entries = map.entrySet().iterator(); 
while (entries.hasNext()) { 
  Map.Entry entry = (Map.Entry) entries.next(); 
  Integer key = (Integer)entry.getKey(); 
  Integer value = (Integer)entry.getValue(); 
  System.out.println("Key = " + key + ", Value = " + value); 
}

最常见的并且在大多数情况下也是最可取的遍历方式。在键值都需要时使用。
Map<Integer, Integer> map = new HashMap<Integer, Integer>(); 
for (Map.Entry<Integer, Integer> entry : map.entrySet()) { 
  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
}

  

转载于:https://www.cnblogs.com/qinyios/p/10464850.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值