2016-05-18 88 views
0

下面是我已經實現的代碼。我的疑問是:當我試圖在字符串中打印第一大和第二大值時,我得到的輸出大小爲[second biggest, first biggest]。 這裏是什麼,我得到了下面的代碼輸出:打印字符串中第一大和第二大元素

The output of the map is: real--6 
The output of the map is: to--2 
The output of the map is: world--1 
The output of the map is: hello--0 
The list after insertion is: [to, real] 
The list inserted as [biggest,secondBiggest] after calling main is: [to, real] 
...... 

,但是,我想The list after insertion to be: [real, to]

public class ReadString { 
    static String input = "This is a real project with real code to do real things to solve real problems in real world real"; 

    public static void main(String[] args) { 
     List<String> lst = ReadString.RepeatedString("This is a real project with real " 
       + "code to do real things to solve real " + "problems in real world real"); 
     System.out.println("The list inserted as [biggest,secondBiggest] after calling main is: " + lst); 
    } 

    public static List<String> RepeatedString(String s) { 

     String[] s2 = input.split(" "); 
     String[] key = { "real", "to", "world", "hello" }; 
     int count = 0; 
     Integer biggest = 0; 
     Integer secondBiggest = 1; 
     Map<String, Integer> map = new HashMap<String, Integer>(); 
     for (int j = 0; j < key.length; j++) { 
      count = 0; 
      for (int i = 0; i < s2.length; i++) { 
       if (s2[i].equals(key[j])) { 
        count++; 
       } 
      } 
      map.put(key[j], count); 
      System.out.println("The output of the map is: " +key[j] + "--" + count); 

     } 

     /* 
     * To find the top two most repeated values. 
     */ 
     List<Integer> values = new ArrayList<Integer>(map.values()); 
     Collections.sort(values); 
     for (int n : map.values()) { 
      if (biggest < n) { 
       secondBiggest = biggest; 
       biggest = n; 
      } else if (secondBiggest < n) 
       secondBiggest = n; 
     } 
     /* To get the top most repeated strings. */ 

     List<String> list = new ArrayList<String>(); 
     for (String s1 : map.keySet()) { 
      if (map.get(s1).equals(biggest)) 
       list.add(s1); 
      else if (map.get(s1).equals(secondBiggest)) 
       list.add(s1); 
     } 
     System.out.println("The list after insertion is: " +list); 
     return list; 
    } 
} 

回答

0

問題出現在將項目添加到列表中時。在迭代map.keySet()時,不能保證你會首先獲得最大的項目。我會做的最小的變化是在列表中添加最大的項目。

for (String s1 : map.keySet()) { 
     if (map.get(s1).equals(biggest)) 
      list.add(0, s1); 
     else if (map.get(s1).equals(secondBiggest)) 
      list.add(s1); 
    }
這樣,如果先添加secondBiggest,則最大值將位於列表頂部。

+0

我正準備發佈這個解決方案。但是,它不應該是'list.add(0,s1);'代替?在索引1處插入它仍然會將其插入到第二個字符串之後(如果遇到第一個字符串)。 – endorphins

+0

感謝您的糾正。 –

+0

它的工作。非常感謝.. :) – user6348718

0

如果我們將wordcount提取到一個簡單的POJO中,我們可以簡化您的方法。類似的,

static class WordCount implements Comparable<WordCount> { 
    String word; 
    int count; 
    WordCount(String word, int count) { 
     this.word = word; 
     this.count = count; 
    } 
    @Override 
    public int compareTo(WordCount o) { 
     return Integer.compare(count, o.count); 
    } 
} 

然後我們可以在repeatedString中使用它。首先,計算String中的單詞;然後構建(s)的List。排序它(因爲它是Comparable它有自然排序)。然後構建List以通過將WordCount(s)的排序List反向(對於兩個項目)來返回。像,

static List<String> repeatedString(String s) { 
    Map<String, Integer> map = new HashMap<>(); 
    for (String word : s.split("\\s+")) { 
     map.put(word, !map.containsKey(word) ? 1 : 1 + map.get(word)); 
    } 
    List<WordCount> al = new ArrayList<>(); 
    for (Map.Entry<String, Integer> entry : map.entrySet()) { 
     al.add(new WordCount(entry.getKey(), entry.getValue())); 
    } 
    Collections.sort(al); 
    List<String> ret = new ArrayList<>(); 
    for (int i = al.size() - 1; i >= al.size() - 2; i--) { 
     ret.add(al.get(i).word); 
    } 
    return ret; 
} 

最後,您main方法應該用你的static input(或static input應該被刪除)

static String input = "This is a real project with real code to do " 
     + "real things to solve real problems in real world real"; 
public static void main(String[] args) { 
    List<String> lst = repeatedString(input); 
    System.out.println("The list inserted as [biggest," 
      + "secondBiggest] after calling main is: " + lst); 
} 

我也得到(的要求)

The list inserted as [biggest,secondBiggest] after calling main is: [real, to] 
0

如果你只關注最大和第二大, 你可以參考下面的代碼。
我不是直接創建列表,而是創建了一個數組,並在指定的位置添加了所需的元素。 (這樣它變得更可讀)
並最終將數組轉換爲列表。

 /* To get the top most repeated strings. */ 
     String[] resultArray = new String[2]; 
     for (String s1 : map.keySet()) { 
      if (map.get(s1).equals(biggest)) 
      resultArray[0]=s1; 
      else if (map.get(s1).equals(secondBiggest)) 
      resultArray[1]=s1; 
     } 
     List<String> list = Arrays.asList(resultArray); 
相關問題