Java 对容器的排序整理

对于Map进行排序:

扑克牌排序
将键值对放到List容器中


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Map<String,Integer> mp=new HashMap<>();
        int n=13;
        for (int i = 0; i < n; i++) {
            String s=sc.next();
            char c=s.charAt(0);
            String x= s.substring(1);
            if(x.equals("J"))  x="11";
            else if(x.equals("10")) x="10";
            else if(x.equals("Q"))  x="12";
            else if(x.equals("K"))  x="13";
            else if(x.equals("A"))  x="14";
            int x1=Integer.parseInt(x);
            if(c=='S')
                x1+=60;
            else if(c=='H')
                x1+=40;
            else if(c=='C')
                x1+=20;
            mp.put(s,x1);
            //System.out.println(x1);
        }
        List<Map.Entry<String,Integer>> l1=new ArrayList<Map.Entry<String,Integer>>(mp.entrySet());
        Collections.sort(l1, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                    return o2.getValue()-o1.getValue();
            }
        });
        for(Map.Entry<String,Integer> x:l1)
            System.out.print(x.getKey()+" ");
        System.out.println();
    }
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        Map<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();
        for(int i=1;i<=n;i++)
        {
            String s=""+i;
            StringBuffer s1 = new StringBuffer(s);
            String s2=s1.reverse().toString();
            int tmp= Integer.parseInt(s2);
            map.put(i,tmp);
        }
        List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
            
            public int compare(Map.Entry<Integer, Integer> o1,
                               Map.Entry<Integer, Integer> o2) {
                if(o1.getValue()==o2.getValue())
                    return o1.getKey()-o2.getKey();
                return o2.getValue()-o1.getValue();
            }

        });
        for(Map.Entry<Integer,Integer> mapping:list){
            System.out.print(mapping.getKey()+" ");
        }
        System.out.println();
    }
}

Map存值,list升序排列

用sc.hasNext不断读取值,ctrl+D可强行中断程序,不影响提交。


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Map<String,Integer> mp=new HashMap<>();
        int n=sc.nextInt();
        while(sc.hasNext()){
            String s=sc.next();
            //System.out.println(s);
            Integer val=mp.get(s);
            if(val==null)
                mp.put(s,1);
            else{
                mp.put(s,val+1);
            }
        }
        List<String> l1=new ArrayList<>();
        for (Map.Entry<String,Integer> x:mp.entrySet()){
            if(x.getValue()==n){
                l1.add(x.getKey());
            }
        }
        Collections.sort(l1,(x1,x2)->x1.compareTo(x2));
        if(l1.size()==0){
            System.out.println("None");
        }else{
            for(String x:l1)
                System.out.println(x);
        }
    }
}

你可能感兴趣的:(Java,java,开发语言,算法)