2016-11-16 42 views
1

我的問題有點不同。我有一個包含一些路由的ArrayList。對於前:查找ArrayList中最常見的字符串

[ACD, CDE, DEB, EBJ, BJK, JKO, ACD, CDE, DEX, EXB, XBJ, BJK, JKO, KOL] 

當我用HashMap計數,只打印了我一個字符串:

Most common route: ACD 
This route repeats 2 times. 

這是正確的,但是,琴絃CDE,BJK和JKO也重複2次。由於我是編程初學者,你會如此友善地告訴我在代碼中必須更改什麼,以便打印所有最常用的路由(字符串)。
下面的代碼:

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

    for(String string: listaRuta) 
    { 
     Integer count = stringsCount.get(string); 
     if(count == null) count = new Integer(0); 
     count++; 
     stringsCount.put(string,count); 
    } 

    Map.Entry<String,Integer> mostRepeated = null; 
    for(Map.Entry<String, Integer> e: stringsCount.entrySet()) 
    { 
     if(mostRepeated == null || mostRepeated.getValue()<e.getValue()) 
      mostRepeated = e; 
    } 
    if(mostRepeated != null) 
     System.out.println("Most common route: " + mostRepeated.getKey()); 
     System.out.println("This route repeats " + mostRepeated.getValue() + " times."); 
+1

[查找ArrayList()中最常見的字符串的可能的副本)(http://stackoverflow.com/questions/22989806/find-the-most-common-string-in-arraylist) – Joe

+0

@Joe OP要求對於這個問題中最常見的字符串(如果它們出現的話,會有多個字符串)。 – user6904265

回答

1
package de.ninjaneers; 

import java.util.Arrays; 
import java.util.Map; 
import java.util.Objects; 
import java.util.stream.Collectors; 

public class Main { 

    public static void main(String[] args) { 

     String[] entries = {"ABC", "ABC", "DEF", "DEF", "DD", "MM"}; 
     Map<String, Long> counts = Arrays.stream(entries) 
       .collect(Collectors.groupingBy(e -> e, Collectors.counting())); 
     Long max = counts.entrySet().stream() 
       .map(Map.Entry::getValue) 
       .max(Long::compare) 
       .orElse(0L); 
     Map<String, Long> onlyTheMax = counts.entrySet().stream() 
       .filter(e -> Objects.equals(max, e.getValue())) 
       .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); 

    } 
} 

首先,你指望所有條目,第二個你得到的最大值和最後你過濾掉所有未滿最大值。對不起,第一個簡短的例子。

+0

這會正確構建hasmap,但不會按OP所要求的提供最常用的原始列表字符串。 – user6904265

+0

在這種情況下,它打印出BJK是最常見的4次重複。這是不正確的。 @Christoph Grimmer-Dietrich – szentmihaly

+0

@szentmihaly在這個答案中有一個不同的方法來建立與字符串和計數器(你也正確地建立了地圖)的散列圖。沒有實現如何找到原始列表中最常見的字符串。 – user6904265

0

的溶液而不流和lambda表達式(更容易理解對初學者):

Test類

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

public class Counter { 

    public static void main(String[] args) { 
    List<String> test = new ArrayList<>(); 
    test.add("a"); 
    test.add("b"); 
    test.add("b"); 
    test.add("c"); 
    test.add("c"); 
    test.add("c"); 

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

    for (String s : test) { 
     if (stringsCount.get(s) == null) { 
     stringsCount.put(s, 1); 
     } else { 
     stringsCount.replace(s, stringsCount.get(s) + 1); 
     } 
    } 

     for (Map.Entry entry : stringsCount.entrySet()) { 
      System.out.println(entry.getKey() + ", " + entry.getValue()); 
     } 
    } 
} 

輸出

一個,1 B,2 C, 3

1

另外我更喜歡lamba解決方案,以建立hasmap。一旦你的地圖,這是你的目標共同使用:

int maxValue=(Collections.max(stringsCount.values())); // This will return max value in the Hashmap 
for (Entry<String, Integer> entry : stringsCount.entrySet()) { // Iterate through the hashmap 
    if (entry.getValue()==maxValue) { 
     System.out.println(entry.getKey());  // Print the key with max value 
    } 
} 

或與Java 8流和蘭巴一個更緊湊的方式:

long maxValue=(Collections.max(stringsCount.values())); 
stringsCount.entrySet() 
    .stream() 
    .filter(s -> s.getValue() == maxValue).forEach(System.out::println); 
2

下面是一個使用解決方案BagtopOccurrencesEclipse Collections

MutableList<String> list = 
     Lists.mutable.with("ACD", "CDE", "DEB", "EBJ", "BJK", "JKO", 
       "ACD", "CDE", "DEX", "EXB", "XBJ", "BJK", "JKO", "KOL"); 
MutableList<ObjectIntPair<String>> pairs = 
     list.toBag().topOccurrences(1); 
System.out.println(pairs); 

輸出:

[BJK:2, JKO:2, ACD:2, CDE:2] 

Bag就像一個Map<Key, Integer>其中值是關鍵字的計數。從外部看,Bag的接口就像是Collection<Key>。 Eclipse Collections具有原始集合,所以在內部,HashBag實現使用ObjectIntHashMap來提高效率。

的方法topOccurrencesBag具有邏輯來處理的關係,這也是爲什麼超過一個ObjectIntPair的結果List返回。

注意:我是Eclipse集合的提交者。

相關問題