如何从映射中获取偶数值,然后将值键添加到列表中?

huangapple 未分类评论45阅读模式
英文:

How to get even values from map and then adding value key to list?

问题

public static List<String> onlyEvenWordsList(List<String> words) {
    Map<String, Integer> wordsWithCount = new HashMap<>();
    List<String> onlyEvenWords = new ArrayList<>();

    for (String word : words) {
        Integer count = wordsWithCount.get(word);
        if (count == null) {
            count = 0;
        }
        wordsWithCount.put(word, count + 1);
    }

    for(Integer value: wordsWithCount.values()){
        if(value % 2 == 0){
            onlyEvenWords.add(value);  // Add the word to the onlyEvenWords list
        }
    }
    return onlyEvenWords;
}
英文:

Alright, so my problem here right now is that I can get all the words from a list, find the occurrence and then add key and value pairs to map, but since I need to return a list of words which frequency is even, I get stuck. Any help?

public static List&lt;String&gt; onlyEvenWordsList(List&lt;String&gt; words) {
    Map&lt;String, Integer&gt; wordsWithCount = new HashMap&lt;&gt;();
    List&lt;String&gt; onlyEvenWords = new ArrayList&lt;&gt;();

    for (String word : words) {
        Integer count = wordsWithCount.get(word);
        if (count == null) {
            count = 0;
        }
        wordsWithCount.put(word, count + 1);
    }

    for(Integer value: wordsWithCount.values()){
        if(value % 2 == 0){
            ....
            }
    }
    return onlyEvenWords;
}

答案1

得分: 0

public static List<String> onlyEvenWordsList(List<String> words) {
    Map<String, Integer> wordsWithCount = new HashMap<>();
    List<String> onlyEvenWords = new ArrayList<>();

    for (String word : words) {
        Integer count = wordsWithCount.get(word);
        if (count == null) {
            count = 0;
        }
        wordsWithCount.put(word, count + 1);
    }

    for (Map.Entry<String, Integer> entry : wordsWithCount.entrySet()) {
        if (entry.getValue() % 2 == 0) {
            onlyEvenWords.add(entry.getKey());
        }
    }
    return onlyEvenWords;
}

// 在主函数中打印列表
System.out.println(Arrays.toString(onlyEvenWordsList(words).toArray()));
英文:
public static List&lt;String&gt; onlyEvenWordsList(List&lt;String&gt; words) {
    Map&lt;String, Integer&gt; wordsWithCount = new HashMap&lt;&gt;();
    List&lt;String&gt; onlyEvenWords = new ArrayList&lt;&gt;();

    for (String word : words) {
        Integer count = wordsWithCount.get(word);
        if (count == null) {
            count = 0;
        }
        wordsWithCount.put(word, count + 1);
    }

    for(Map.Entry&lt;String, Integer&gt; entry: wordsWithCount.entrySet()){
        if(entry.getValue()%2==0){
            onlyEvenWords.add(entry.getKey());
        }
    }
    return onlyEvenWords;
}

And to print the list in main

System.out.println(Arrays.toString( onlyEvenWordsList(words).toArray()));

huangapple
  • 本文由 发表于 2020年6月29日 18:26:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/62636119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定