關於HashMap和TreeMap的一些注意點

題目:字符統計

對字符中的 各個英文字符(大小寫分開統計),數字,空格進行統計,並按照統計個數由多到少輸出,如果統計的個數相同,則按照ASII碼由小到大排序輸出 。如果有其他字符,則對這些字符不用進行統計。

輸入例子:
aadddccddc
輸出例子:
dca


關於HashMap和TreeMap的一些注意:

TreeMap構造方法中的Comparator只能對key排序,不能對Map.Entry排序。

Collectons.sort()方法只能對List排序,不能對Set排序。

import java.util.*;
public class Main{
    public static HashMap<Character,Integer> map=new HashMap();
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        while(in.hasNext()){
            String s=in.nextLine();
            HashMap<Character,Integer> map=new HashMap();
            for(int i=0;i<s.length();i++){
                if(s.charAt(i) >= 'A'&&s.charAt(i) <= 'Z'||s.charAt(i) >= 'a'&&s.charAt(i) <= 'z'||s.charAt(i) >= '0'&&s.charAt(i) <= '9'||s.charAt(i) == ' '){
                    if(map.containsKey(s.charAt(i))){
                        map.put(s.charAt(i),map.get(s.charAt(i))+1);
                    }else{
                        map.put(s.charAt(i),1); 
                    }
                }
            }
            Set<Map.Entry<Character,Integer>> set=map.entrySet();
            ArrayList<Pair> array=new ArrayList();
            for(Map.Entry<Character,Integer> entry: set){
                array.add(new Pair(entry.getKey(),entry.getValue()));
            }
         Collections.sort(array,new Comparator<Pair>(){
            public int compare(Pair first,Pair second){
                if(first.count!=second.count){
                    return second.count-first.count;
                }else{
                    return first.c-second.c;
                }
            } 
         });
            StringBuilder sb=new StringBuilder();
          for(Pair pair: array){
              sb.append(pair.c);
          }
            System.out.println(sb.toString());
        }
        in.close();
    }
    static class Pair{
        char c;
        int count;
        public Pair(char c,int count){
            this.c=c;
            this.count=count;
        }
    }
}

題目:合併表記錄

輸入描述:

先輸入鍵值對的個數 然後輸入成對的index和value值,以空格隔開

輸出描述:

輸出合併後的鍵值對(多行)

輸入例子:
4
0 1
0 2
1 2
3 4
輸出例子:
0 3
1 2
3 4
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        while(in.hasNext()){
            int n=in.nextInt();
            TreeMap<Integer,Integer> map=new TreeMap();
            for(int i=0;i<n;i++){
                int key=in.nextInt();
                int value=in.nextInt();
                if(!map.containsKey(key)){
                    map.put(key,value);
                }else{
                    map.put(key,map.get(key)+value);
                }
            }
            Set<Map.Entry<Integer,Integer>> set=map.entrySet();
            for(Map.Entry<Integer,Integer> entry: set){
                System.out.println(entry.getKey()+" "+entry.getValue());
            }
        }
        in.close();
    }
}
這道題就是注意map視圖的方法

Set<Map.Entry<Integer,Integer>> set=map.entrySet();





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章