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

时间:2023-03-09 16:42:30
实现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());
}