简体   繁体   English

我如何遍历HashMap <String, Set<String> &gt;并返回哪个Set <String> 是最大的?

[英]How do I iterate through a HashMap<String, Set<String>> and return which Set<String> is the largest?

I am trying to figure out how to return the largest Set in the Hashmap. 我试图弄清楚如何在哈希图中返回最大的Set。 I know the basic way of doing it but I cant get it right. 我知道这样做的基本方法,但我做对了。

I tried iterating over all the keys in the hashmap by doing the following code. 我尝试通过执行以下代码来遍历哈希图中的所有键。 I also just realized that by doing this code, I will only be able to return the size of the largest set not the actual set itself. 我也刚刚意识到,通过执行此代码,我将只能返回最大集合的大小,而不是实际集合本身。 Is there a much better way? 有更好的方法吗? I will greatly appreciate any help! 我将不胜感激任何帮助! Thank you! 谢谢!

Map<String, Set<String>> category = new Hashmap<String, Set<String>>();
int largest = //someInt;
for(String d : category.keySet()) {
    if (category.get(d).size() > //largest)
        largest = category.get(d).size();
} return largest;

While other answers have shown how you can fix your code, here is another way to do it in a single statement: 尽管其他答案已经说明了如何修复代码,但这是在单个语句中执行此操作的另一种方法:

Set<String> largestSet = category.values().stream()
        .max(Comparator.comparingLong(Set::size))
        .orElse(null);

You can try this 你可以试试这个

Map<String, Set<String>> category = new Hashmap<String, Set<String>>();
int largest = 0;
Set<String> largeSet = new HashSet<String>();
for(String d : category.keySet()) {
    if (category.get(d).size() > largest)
        largest = category.get(d).size();
        largeSet = category.get(d);
}
return largeSet; 

You can just do so: 您可以这样做:

Map<String, Set<String>> category = new HashMap<>();
Set<String> set = new HashSet<>();
int largest = 0;
for(String d : category.keySet()) {
    if (category.get(d).size() > largest) {
        largest = category.get(d).size();
        set = category.get(d);
    }
}
return set;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何设置ArrayList <HashMap<String, String> &gt;在AlertDialog中? - How to set ArrayList<HashMap<String, String>> in AlertDialog? Java集 <String> 在HashMap中 - Java Set<String> in a HashMap 如何迭代Arraylist <HashMap<String,String> &gt;? - How to iterate Arraylist<HashMap<String,String>>? 如何遍历hashmap <String ArrayList<String> &gt;并使用key String和arraylist中的节点创建xml文件名 - how to iterate through hashmap<String ArrayList<String>> and create xml file name with key String and nodes from arraylist 字符串与流到HashMap在Java 8中的字符串 - Set of String with Stream to HashMap in Java 8 我如何遍历字符串以找到特定字符? - how do i iterate through a string of characters to find a specific character? 如何同时返回 HashMap<String, String> 和 HashMap<String, User> - How to return both HashMap<String, String> and HashMap<String, User> 如何将我从文件中读取的数据放入哈希图<String, Set<String> &gt;? - How to put data i read from file to hashmap <String, Set<String>>? 我正在尝试遍历Hashmap <LinkedList<String> ,int [] &gt;&gt;。 有比我正在做的更简单的方法吗? - I am trying to iterate through a Hashmap<LinkedList<String>, int[]>>. Is there a simpler way to do it than what I am doing? 迭代一个字符串映射并在 Bean 中设置 - Iterate a String map and set in Bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM