栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 字符串、数组、map排序案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 字符串、数组、map排序案例

1、字符串排序

import java.util.*;

public class SortString {
    public static void main(String[] args) {

        reverseByAsckString();
    }


    //按照字符串按长度,由小到大排序输出
    //输入
    // 3
    //adfasdf
    //asdf
    //adsfadfasdf
    //输出
    //asdf
    //adfasdf
    //adsfadfasdf
    //Collections.sort的使用
    public static void reverseByLengthString() {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        ArrayList list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String line = scanner.nextLine();
            if (line.equals("stop")) {
                break;
            } else {
                list.add(line);
            }
        }

        Collections.sort(list, new Comparator() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.length() < o2.length()) {
                    return -1;
                } else {
                    return 1;
                }
            }
        });
        list.forEach(ele -> {
            System.out.println(ele);
        });
    }


    //将多个按照字符串asck码,由小到大排序输出
    //输入
    // 3
    //asdf
    //asdf
    //qwerqwe
    //输出
    //asdf
    //asdf
    //qwerqwe

    public static void reverseByAsckString() {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        ArrayList list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String line = scanner.nextLine();
            if (line.equals("stop")) {
                break;
            } else {
                list.add(line);
            }
        }

        Collections.sort(list, new Comparator() {
            @Override
            public int compare(String o1, String o2) {
                if (o1.compareTo(o2)<0) {
                    return -1;
                } else {
                    return 1;
                }
            }
        });
        list.forEach(ele -> {
            System.out.println(ele);
        });
    }


    //将一个字符串按照字符asck码的顺序升序排序后输出
    // 输入bacd
    // 输出abcd
    //Arrays.sort的使用
    public static void reverseByInnerAsckString() {
        Scanner scanner = new Scanner(System.in);
        String line = scanner.nextLine();
        char[] chars = line.toCharArray();
        Character[] character= new Character[chars.length];
        for (int i = 0; i < chars.length; i++) {
            character[i]=chars[i];
        }

        Arrays.sort(character, new Comparator() {
            @Override
            public int compare(Character o1, Character o2) {
                return o1-o2;
            }
        });
        for (int i = 0; i < character.length; i++) {
            chars[i]=character[i];
        }
        System.out.print(chars);

    }

}

2、map排序

import java.util.*;

public class SortMap {
    public static void main(String[] args) {
//        Map stringIntegerMap = sortMapByKey();
//
//        Set> entries = stringIntegerMap.entrySet();
//        entries.forEach(ele->{
//            System.out.println(ele.getKey()+','+ele.getValue());
//        });
        sortMapByValue();

    }


    public static Map sortMapByKey() {
        Map stu = new TreeMap<>(new Comparator() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        //传进来一个key的比较器对象来构造treemap
        stu.put("apple", 55);
        stu.put("boy", 32);
        stu.put("cat", 22);
        stu.put("dog", 12);
        stu.put("egg", 11);
        return stu;
    }

    public static Map sortMapByValue() {
        Map stu = new HashMap();
        //传进来一个key的比较器对象来构造treemap
        stu.put("apple", 55);
        stu.put("boy", 32);
        stu.put("cat", 22);
        stu.put("dog", 22);
        stu.put("egg", 11);
        Set> entries = stu.entrySet();
        ArrayList> arrayList = new ArrayList<>(entries);
        Collections.sort(arrayList, new Comparator>() {
            @Override
            public int compare(Map.Entry o1, Map.Entry o2) {
                if(o1.getValue()>o2.getValue()){
                    return 1;
                }
                if(o1.getValue().equals(o2.getValue())){
                    if(o1.getKey().compareTo(o2.getKey())>0){
                        return 1;
                    }
                    if(o1.getKey().compareTo(o2.getKey())==0){
                        return 0;
                    }
                    if(o1.getKey().compareTo(o2.getKey())<0){
                        return -1;
                    }
                }
                if(o1.getValue(){
            System.out.println(ele.getKey()+','+ele.getValue());
        });

        return stu;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937707.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号