小技巧:set的排序
这里有个例子是关系set的排序
import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.Set; import java.util.TreeSet; import com.itextpdf.text.log.SysoCounter; public class test7 { public static void main( String[] args ) { Set<String> set = new HashSet <>(); set.add("201801"); set.add("201803"); set.add("201802"); set.add("201805"); set.add("201808"); set.add("201807"); // setSort(set); System.out.println("顺序"+setSort(set,"des")); System.out.println("倒序"+setSort(set,"desc")); } //set排序 set对象,排序des或空顺序,desc倒序 public static Set<String> setSort(Set<String> set,String orderby){ Set<String> newSet=new HashSet<>(); if(orderby.equals("")||orderby.equals("des")){//顺序 newSet = new TreeSet<String>(); for(String str:set){ newSet.add(str); } } if(orderby.equals("")||orderby.equals("desc")){//倒叙 newSet = new TreeSet<String>(new MyComparator()); for(String str:set){ newSet.add(str); } } return newSet; } } class MyComparator implements Comparator<String>{ @Override public int compare(String o1, String o2) { return o2.compareTo(o1);//降序排列 } }