简体   繁体   中英

Java Unicode Characters

I'm familiar with problems with ascii. The problem is I have no experience with same problems in unicode characters. For example, how to return the word that occurs most frequently given a string array containing words? Thanks in advance!

ps: You can always use an array which length is "256" to represent all the characters in ASCII while you can't do that when it comes to unicode. Is HashMap a must and the best way to solve the problem? I heard that there are better ways to solve it. Below is what I can think of:

    String str = "aa df ds df df"; // assume they are Unicode
    String[] words = str.split(" ");
    HashMap<String, Integer> map  = new HashMap<String, Integer>();
    for (String word : words){
        if (map.containsKey(word)){
            int f = map.get(word);
            map.put(word, f+1);
        } else{
            map.put(word, 1);
        }
    }

    int max = 0;
    String maxWord = ""; 

    for (String word : words){
        int f = map.get(word);
        if (f > max){
            max = f;
            maxWord = word;
        }
    }

    System.out.println(maxWord + " " +max);
// Inspired by GameKyuubi. It can be solved using array sort and count the most frequently used word using constatnt space. 
    Arrays.sort(words);
    int max = 0;
    int count = 0;
    String maxWord = "";
    String prev = "";
    for (String word : words){
        if (prev.equals("") || word.equals(prev)){
            count++;
        } else{
            count = 1;
        }

        if (max < count){
            max = count;
            maxWord = word;
        }
        prev = word;

    }
    System.out.println(maxWord + " " +max);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM