簡體   English   中英

如何根據ascii值對ArrayList的元素進行排序?

[英]How to sort the elements of an ArrayList according to their ascii values?

我已經使用Scanner讀取了一個文件,然后使用HashMap和ArrayList根據單詞出現次數(升序和降序)對單詞進行排序,一切正常。 但是我希望輸出的排序方式是首先顯示數字然后是大寫,然后是小寫。

以下是我的相同代碼:

`Scanner scanner = new Scanner(new File("file"));
    Map<String, Integer> map = new HashMap<String, Integer>();
    int count=0;
    String whole="";
    while (scanner.hasNext())
        {
        count++;
        String word = scanner.next();
        whole=whole + " " + word;
        if (map.containsKey(word))
            {
            map.put(word, map.get(word)+1);
            }
        else
            {
            map.put(word, 1);
            }
        }

    List<Map.Entry<String, Integer>> entries = new ArrayList<Entry<String,Integer>>( map.entrySet());

    Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {

        @Override
        public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
            return a.getValue().compareTo(b.getValue());
        }
    });

    System.out.println("Count: " +count);
    System.out.print("Output 1(Ascending): ");
    for(int j = 0; j < map.size(); j++){
        System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
    }

    System.out.print("Output 2(Descending): ");
    for(int i = 0; i < map.size(); i++){
        System.out.println(entries.get(entries.size() - i - 1).getKey()+" "+entries.get(entries.size() - i - 1).getValue());
    }`

我的意見是:

我有10只狗,所有的狗都有不同的大小

我的輸出是:

數:12
輸出1(升序):全1
1
大小1
是1
和1
1
有1
我1
不同1
10 1
狗2
輸出2(降序):狗2
10 1
不同1
我1
有1
1
和1
是1
大小1
1
全部1

期望的輸出:

狗2 \\因為它出現的次數比任何其他單詞多
10 1 \\因為它是一個數字
我1 \\因為它是一個大寫字母
有1 \\后跟所有小寫單詞

我編寫了一個代碼片段,以下內容適用於按升序打印輸入。

String input = "I have 10 dogs and all the dogs are of different size";
String [] inputSplit = input.split(" ");

Map<String, Integer> map = new HashMap<>();

for (int i=0; i < inputSplit.length; i++) {
    String word = inputSplit[i];
    if (map.containsKey(word)) {
        map.put(word,  map.get(word) + 1);
    }
    else {
        map.put(word, 1);
    }
}

List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
    @Override
    public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
        int compareWordCount = a.getValue().compareTo(b.getValue());

        if (compareWordCount == 0) {
            return a.getKey().compareTo(b.getKey());
        }
        return compareWordCount;
    }
});

for (int j=0; j < entries.size(); j++) {
    System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}

結果

10 1
I 1
all 1
and 1
are 1
different 1
have 1
of 1
size 1
the 1
dogs 2

使用降序排序更新

String input = "I have 10 dogs and all the dogs are of different size";

String [] inputSplit = input.split(" ");

Map<String, Integer> map = new HashMap<>();

for(int i = 0; i < inputSplit.length; i++){
    String word = inputSplit[i];
    if(map.containsKey(word)){
        map.put(word,  map.get(word) + 1);
    }
    else{
        map.put(word, 1);
    }
}

List<Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());

Comparator <Entry<String, Integer>> ascComparator = new Comparator<Entry<String, Integer>>(){

    @Override
    public int compare(Entry<String, Integer> a, Entry<String, Integer> b) {

        int compareWordCount = a.getValue().compareTo(b.getValue());

        if(compareWordCount == 0){
            return a.getKey().compareTo(b.getKey());
        }
        return compareWordCount;
    }

};

Comparator <Entry<String, Integer>> descComparator = new Comparator<Entry<String, Integer>>(){

    @Override
    public int compare(Entry<String, Integer> a, Entry<String, Integer> b) {

        int compareWordCount = a.getValue().compareTo(b.getValue());

        if(compareWordCount == 0){
            return b.getKey().compareTo(a.getKey());
        }
        return compareWordCount;
    }

};

System.out.println("Ascending Sort");
Collections.sort(entries, ascComparator);       
for(int j = 0; j < entries.size(); j++){
    System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}

System.out.println("\nDescending Sort");
Collections.sort(entries, descComparator);

for(int j = 0; j < entries.size(); j++){
    System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
}

結果

Ascending Sort
10 1
I 1
all 1
and 1
are 1
different 1
have 1
of 1
size 1
the 1
dogs 2

Descending Sort
the 1
size 1
of 1
have 1
different 1
are 1
and 1
all 1
I 1
10 1
dogs 2

您可以擴展比較邏輯以添加ascii排序邏輯

        public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
            int i = a.getValue().compareTo(b.getValue());
            if(i==0) {
                i = b.getKey().compareTo(a.getKey());
            }
            return i;
        }

希望這可以幫助。

只需更新比較方法部分:

    Scanner scanner = new Scanner(new File("file"));
    Map<String, Integer> map = new HashMap<String, Integer>();
    int count=0;
    String whole="";
    while (scanner.hasNext())
        {
        count++;
        String word = scanner.next();
        whole=whole + " " + word;
        if (map.containsKey(word))
            {
            map.put(word, map.get(word)+1);
            }
        else
            {
            map.put(word, 1);
            }
        }

    List<Map.Entry<String, Integer>> entries = new ArrayList<Entry<String,Integer>>( map.entrySet());

    Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {

        @Override
        public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
            int compareResult = a.getValue().compareTo(b.getValue());
            if (compareResult != 0) {
                return compareResult;
            } else {
                //Here, when the occurance numbers are equal, then compare
                //with the key values where the texts are stored
                return a.getKey().compareTo(b.getKey());
            }
        }
    });

    System.out.println("Count: " +count);
    System.out.print("Output 1(Ascending): ");
    for(int j = 0; j < map.size(); j++){
        System.out.println(entries.get(j).getKey()+" "+entries.get(j).getValue());
    }

    System.out.print("Output 2(Descending): ");
    for(int i = 0; i < map.size(); i++){
        System.out.println(entries.get(entries.size() - i - 1).getKey()+" "+entries.get(entries.size() - i - 1).getValue());
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM