【JAVA8】Set排序四种写法-倒序
生活随笔
收集整理的這篇文章主要介紹了
【JAVA8】Set排序四种写法-倒序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
工作中遇到,寫了很久以前的寫法,師兄給了很多建議,于是整理了一下。孔子曰:“你知道茴香豆的茴字有幾種寫法嗎?”
首先,要知道Set的特性是有序不重復,Jdk中使用了HashMap的Key作為Set的容器。
?
第一部分,基本類型&String
第一種,平常的寫法:
public class App {public static void main( String[] args ) {Set<String> set = new HashSet<>();set.add("1");set.add("2");set.add("5");set.add("4");set.add("3");System.out.println(set.toString());Set<String> sortSet = new TreeSet<String>(new Comparator<String>() {@Overridepublic int compare(String o1, String o2) {return o2.compareTo(o1);//降序排列}});sortSet.addAll(set);System.out.println(sortSet.toString());} }第二種,lambda:
public class App {public static void main( String[] args ) {Set<String> set = new HashSet<>();set.add("2");set.add("1");set.add("5");set.add("3");set.add("4");System.out.println(set.toString());Set<String> sortSet = new TreeSet<String>((o1, o2) -> o2.compareTo(o1));sortSet.addAll(set);System.out.println(sortSet.toString());} }第三種:JDK api
public class App {public static void main( String[] args ) {Set<String> set = new HashSet<>();set.add("1");set.add("2");set.add("3");set.add("5");set.add("4");System.out.println(set.toString());Set<String> sortSet = new TreeSet<String>(Comparator.reverseOrder());sortSet.addAll(set);System.out.println(sortSet.toString());} }第四種:java8流的方式:
public class App {public static void main( String[] args ) {Set<String> set = new HashSet<>();set.add("1");set.add("2");set.add("3");set.add("4");set.add("5");System.out.println(set.toString());//這里后續用有序的list處理即可,因為流中進行了倒序處理,收集成set后會重排List<String> collect = set.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());System.out.println(collect);} }?
?
第二部分,對象排序
package stream;import java.util.HashSet; import java.util.Set; import java.util.TreeSet;/*** 事例2,對象排序*/ public class Test2 {public static void main( String[] args ) {PersonObj person = new PersonObj(1L,"A",10);PersonObj person1 = new PersonObj(2L,"B",20);PersonObj person2 = new PersonObj(3L,"CC",30);PersonObj person3 = new PersonObj(4L,"DD",40);PersonObj person4 = new PersonObj(5L,"EE",50);PersonObj person5 = new PersonObj(6L,"FF",60);//對象組裝,組裝后內容無序Set<PersonObj> set = new HashSet<>();set.add(person3);set.add(person4);set.add(person5);set.add(person);set.add(person1);set.add(person2);System.out.println(set.toString());//第一種方式,Person類實現 Comparable并重寫compareTo方法//set = new TreeSet<>(set);//System.out.println(set.toString());//第二種方式TreeSet<PersonObj> objects = new TreeSet<>(((o1, o2) ->Integer.compare(o2.getAge(), o1.getAge())));objects.addAll(set);System.out.println(objects);}}/*** 被排序的對象實現Comparable*/ class PersonObj implements Comparable{private Long id;private String name;private int age;@Overridepublic int compareTo(Object o) {PersonObj student =(PersonObj)o;//將當前this.age值與傳入的參數中的o.age對比,返回正數則升序,返回負數則降序return Integer.compare(student.age, this.age);}public PersonObj(Long id, String name, int age) {this.id = id;this.name = name;this.age = age;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return this.name+"_"+this.id+"_"+this.age;}@Overridepublic int hashCode() {return super.hashCode();}@Overridepublic boolean equals(Object obj) {return super.equals(obj);}}?
總結
以上是生活随笔為你收集整理的【JAVA8】Set排序四种写法-倒序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android基础入门教程——7.3.3
- 下一篇: psp2000