List,map排序

来源:互联网 发布:六韬淘宝天猫托管 编辑:程序博客网 时间:2024/05/17 08:47

一、List排序
List排序要求每个元素必须实现comparable接口,具体Java的8中包装类型都已经实现了该接口。
两种方法:Collections.sort(Collection c)
Collections.sort(Collection c,Comparator cmp)
对汉字进行按照拼音排序
Collections.sort(list,Collator.getInstance(Locale.CHINA));

1、List排序中对一般字段排序,使用默认的方法直接进行排序:

    /**     * list的默认排序方法     * @param list  要排序的list     * @param type  升序/降序     */    public static void defaultListSort(ArrayList<String> lists,TypeEnum type){        switch (type) {        case ASC: Collections.sort(lists); //升序            break;        case DESC:Collections.reverse(lists); //降序        default:Collections.reverse(lists); //降序            break;        }        for (String list : lists) {            System.out.println(list);        }    }    /**     * 枚举,规定type的取值     * @author zhaojw_420     *     */    public enum TypeEnum{        ASC,DESC;    }

2、对某一个实体类进行排序,需要重写实体类的compareTo()方法:

public class CollectionSort {    /**     * 对实体类people进行排序     * @param lists     * @param type     */    public static void customListSort(ArrayList<People> lists,TypeEnum type){        switch (type) {        case ASC: Collections.sort(lists); //升序            break;        case DESC:Collections.reverse(lists); //降序        default:Collections.reverse(lists); //降序            break;        }        for (People list : lists) {            System.out.println(list.toString());        }    }/**     * 枚举,规定type的取值     * @author zhaojw_420     *     */    public enum TypeEnum{        ASC,DESC;    }    }class People implements Comparable<People>{    private String name;    private int age;    public People() {    }    public People(String name, int age) {        this.name = name;        this.age = age;    }    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;    }    @Override    public String toString() {        return "People [名字=" + name + ", 年龄=" + age + "]";    }    //先以年龄排序,后以名字排序    public int compareTo(People people) {        // TODO Auto-generated method stub        if(this.age!=people.age){            return this.age-people.age;        }        return this.name.compareTo(people.name);    }}

二、Map排序
1、可以使用TreeMap进行排序,TreeMap 里面的元素会自动排序,排序要求里面的元素的key是实现了comparable接口,也就是可以比较。Key一般都是String,所以一般map排序都是按照字符串的顺序。

     /**     * map按key来排序     * @param map  要排序的map     * @param type 升序/降序     * 若map的可以得类型是一个people类,需要实现people的compareTo方法,再进行比较     */    public static void mapSortByKey(HashMap<String, String> map,final TypeEnum type){        if (map!=null&&!map.isEmpty()) {            Map<String, String> sortMap=new TreeMap<String,String>(new Comparator<String>() {                public int compare(String o1,String o2) {                    int key1=0;int key2=0;                    key1=Integer.parseInt(isNumeric(o1)?o1:"0");                    key2=Integer.parseInt(isNumeric(o2)?o2:"0");                    int result=0;                    switch (type) {                    case ASC:                        result=key1-key2;                        break;                    case DESC:                        result=key2-key1;                        break;                    default:result=key1-key2;                        break;                    }                    return result;                }            });            sortMap.putAll(map);            System.out.println(map);        }    }/**     * 判断是否为数字     * @param str     * @return     */    public static boolean isNumeric(String str){         Pattern pattern = Pattern.compile("[0-9]*");         return pattern.matcher(str).matches();         }     /**     * 枚举,规定type的取值     * @author zhaojw_420     *     */    public enum TypeEnum{        ASC,DESC;    }

2、map按照value进行排序:

     /**     * map按value值排序     * @param map     * @param type     */    public static void mapSortByValue(HashMap<String, String> map ,final TypeEnum type){        Map<String, String> sortMap=new LinkedHashMap<String, String>();        if (map!=null&&!map.isEmpty()) {            List<Map.Entry<String, String>> entryList=new ArrayList<Map.Entry<String,String>>(map.entrySet());            Collections.sort(entryList,new Comparator<Map.Entry<String, String>>() {                public int compare(Entry<String, String> o1, Entry<String, String> o2) {                    // TODO Auto-generated method stub                    int v1=0;int v2=0;                    v1=Integer.parseInt(isNumeric(o1.getValue())?o1.getValue():"0");                    v2=Integer.parseInt(isNumeric(o2.getValue())?o2.getValue():"0");                    int result=0;                    switch (type) {                    case ASC:                        result=v1-v2;                        break;                    case DESC:                        result=v2-v1;                        break;                    default:result=v1-v2;                        break;                    }                    return result;                }            });            Iterator<Map.Entry<String, String>> iterator=entryList.iterator();            Map.Entry<String, String> tmpMap=null;            while(iterator.hasNext()){                tmpMap=iterator.next();                sortMap.put(tmpMap.getKey(), tmpMap.getValue());            }        }    }    /**     * 判断是否为数字     * @param str     * @return     */    public static boolean isNumeric(String str){         Pattern pattern = Pattern.compile("[0-9]*");         return pattern.matcher(str).matches();         }     /**     * 枚举,规定type的取值     * @author zhaojw_420     *     */    public enum TypeEnum{        ASC,DESC;    }
0 0
原创粉丝点击