一、数组排序
int[] nums = {100,70,65,80,90};
//升序
Arrays.sort(nums);
System.out.println("升序输出=" + Arrays.toString(nums));
//降序
int k = 0;
int[] nums2 = new int[nums.length];
for (int i = nums.length - 1 ; i >= 0 ; i--) {
nums2[k++] = nums[i];
}
System.out.println("降序输出=" + Arrays.toString(nums2));
升序输出=[65, 70, 80, 90, 100]
降序输出=[100, 90, 80, 70, 65]
二、 List集合排序
2.1 基础数据类型
方式一 重写比较器
List<Integer> list = new ArrayList<>();
for (int x : nums) {
list.add(x);
}
// 升序
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
});
System.out.println("list升序=" + list);
// 降序
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
System.out.println("list降序=" + list);
list升序=[65, 70, 80, 90, 100]
list降序=[100, 90, 80, 70, 65]
方式二 利用Collections.sort()方法
Collections.sort(list);
System.out.println("list升序=" + list);
Collections.sort(list,Comparator.reverseOrder());
System.out.println("list降序=" + list);
2.2 引用数据类型
方式一 重写比较器
Stu s1 = new Stu("wang",175,150);
Stu s2 = new Stu("zhang",175,160);
Stu s3 = new Stu("liu",181,171);
Stu s4 = new Stu("chen",168,130);
Stu s5 = new Stu("chen",168,130);
Stu s6 = new Stu("tom",175,160);
List<Stu> al = new ArrayList<>();
al.add(s1);
al.add(s2);
al.add(s3);
al.add(s4);
al.add(s5);
al.add(s6);
//要求: 按身高序,升高相等按体重剩余,体重相等按名字的字典降序;
al.sort(new Comparator<Stu>() {
@Override
public int compare(Stu o1, Stu o2) {
if (o1.height != o2.height) {
return o1.height - o2.height;
} else if (o1.weight != o2.weight) {
return o1.weight - o2.weight;
}
return o2.name.compareTo(o1.name);
}
});
System.out.println("al=" + al);
其中Stu类:
static class Stu {
String name;
int height;
int weight;
public Stu(String name, int height, int weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "["+this.name+","+this.height+","+this.weight+"]";
}
输出:
al=[[chen,168,130], [chen,168,130], [wang,175,150], [zhang,175,160], [tom,175,160], [liu,181,171]]
方式二 利用Collections.sort()方法
三、Set排序
3.1 基础数据类型
Set集合的类大多是无序的,对于基础数据类型有序输出,一般用TreeSet
方式一 利用TreeSet的自带排序或按要求重写比较器(仅适合TreeSet,如hashSet是无序的,不存在比较器的重写,重写也会报错)
String[] strs = {"adc","ab","zero","hello","world"};
Set<String> set = new TreeSet<>();
for (String x : strs) {
set.add(x);
}
System.out.println("set升序=" + set);
Set<String> set2 = new TreeSet<>(Comparator.reverseOrder());
for (String x : strs) {
set2.add(x);
}
System.out.println("set降序=" + set2);
输出:
set升序=[ab, adc, hello, world, zero]
set降序=[zero, world, hello, adc, ab]
当然如果需要自定义排序或者不用Comparator.reverseOrder(),可以具体重写比较器,将比较器传递到TreeSet,如上面的降序可以这样写,本质是一样的:
Comparator<String> cmp = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
};
Set<String> set2 = new TreeSet<>(cmp);
for (String x : strs) {
set2.add(x);
}
System.out.println("set降序=" + set2);
方式二 利用Steam流方式重写sorted()中的比较器转换为List处理(hashSet TreeSet等通用)
List<String> lists = set.stream().sorted().collect(Collectors.toList());
List<String> lists2 = set.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
System.out.println("set升序=" + lists);
System.out.println("set降序=" + lists2);
同样的,如果自定义排序,同方式一类似重写sorted方法中的Comparator即可
3.2 引用数据类型
上面List的例子中,name=chen的同学重复输出,如果用Set去改进呢(当然也可以用List改进)
可以类似基础数据类型重写比较器或者利用Steam流方式重写比较器转换为List
需要注意的是引用数据类型比较时候如果使用 TreeSet时候必须指定比较方式,如不指定会编译保持
Stu s1 = new Stu("wang",175,150);
Stu s2 = new Stu("zhang",175,160);
Stu s3 = new Stu("liu",181,171);
Stu s4 = new Stu("chen",168,130);
Stu s5 = new Stu("chen",168,130);
Stu s6 = new Stu("tom",175,160);
Set<Stu> sets = new HashSet<>();
sets.add(s1);
sets.add(s2);
sets.add(s3);
sets.add(s4);
sets.add(s5);
sets.add(s6);
//要求: 按身高序,升高相等按体重剩余,体重相等按名字的字典降序;
System.out.println("sets="+sets);
Comparator<Stu> compare = new Comparator<Stu>() {
@Override
public int compare(Stu o1, Stu o2) {
if (o1.height != o2.height) {
return o1.height - o2.height;
} else if (o1.weight != o2.weight) {
return o1.weight - o2.weight;
}
return o2.name.compareTo(o1.name);
}
};
List<Stu> al = sets.stream().sorted(compare).collect(Collectors.toList());
System.out.println("al=" + al);
其中Stu重写equal方法
static class Stu {
String name;
int height;
int weight;
public Stu(String name, int height, int weight) {
this.name = name;
this.height = height;
this.weight = weight;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "["+this.name+","+this.height+","+this.weight+"]";
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if ( obj == null || !(obj instanceof Stu)) {
return false;
} else {
Stu s = (Stu)obj;
return s.name.equals(name) && s.height == height && s.weight == weight;
}
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return Objects.hash(name,height,weight);
}
}
输出:
sets=[[wang,175,150], [tom,175,160], [zhang,175,160], [liu,181,171], [chen,168,130]]
al=[[chen,168,130], [wang,175,150], [tom,175,160], [zhang,175,160], [liu,181,171]]
三 Map排序
map的排序可以转换为List进行排序,利用list的sort方法去重写比较器
需要注意的是如果结果要求输出的是Map对象,应当使用LinkedHashMap去存储处理后的List中数据
(LinkedHashMap底层是链表结构,可以保证按读取的顺序写入到Map集合中)
Map<String, Stu> maps = new HashMap<>();
maps.put(s1.name, s1);
maps.put(s2.name, s2);
maps.put(s3.name, s3);
System.out.println(maps);
//如果要求按照升高降序,体重升序,最后按key的name字典升序呢
List<Entry<String, Stu>> ls = new ArrayList<>(maps.entrySet());
ls.sort(new Comparator<Entry<String, Stu>>() {
@Override
public int compare(Entry<String, Stu> o1, Entry<String, Stu> o2) {
if (o1.getValue().height != o2.getValue().height) {
return o2.getValue().height - o1.getValue().height;
} else if (o1.getValue().weight != o2.getValue().weight) {
return o1.getValue().weight - o2.getValue().weight;
}
return o1.getKey().compareTo(o2.getKey());
}
});
System.out.println("ls=" + ls);
Map<String, Stu> maps2 = new LinkedHashMap<>();
for (Entry<String, Stu> en : ls ) {
maps2.put(en.getKey(), en.getValue());
}
System.out.println("maps2=" + maps2);
输出:
ls=[liu=[liu,181,171], wang=[wang,175,150], zhang=[zhang,175,160]]
maps2={wang=[wang,175,150], liu=[liu,181,171], zhang=[zhang,175,160]}