简体   繁体   English

如果我有两个值相等的键,我如何返回一个 Set<string> 有点(值+“-”+键“,”+键)?</string>

[英]If I have two keys with equal value, how do I return a Set<String> kinda (value + " - " + key ", " + key)?

This code works only when I have a pair like name(value) + number(key), but I'm stuck with situation like - name + number, number.此代码仅在我有一对名称(值)+数字(键)时才有效,但我遇到了诸如-名称+数字,数字之类的情况。 It returns name + number1, name + number2它返回名称+编号1,名称+编号2

 public Set<String> getAllContacts() {
        TreeSet<String> contacts = new TreeSet<>();
        if (phoneBook.isEmpty()) {
            return new TreeSet<>();
        } else {
            for (Map.Entry<String, String> entry : phoneBook.entrySet()) {
                contacts.add(entry.getValue() + " - " + entry.getKey());
            }
        }
        return contacts;
    }

Basically you want to invert key and value of your phoneBook map.基本上你想反转你的电话簿phoneBookkeyvalue Since one name can have multiple number s according to your example the output structure should be Map<String, List<String>> .由于根据您的示例,一个name可以有多个number ,因此 output 结构应该是Map<String, List<String>> With this name oriented phone book you can easily create your contact collection.使用这个以姓名为导向的电话簿,您可以轻松地创建您的联系人集合。 Eg例如

Map<String, List<String>> phoneBookByName = new HashMap<>();
phoneBook.forEach((k, v) -> {
    List<String> numbers = phoneBookByName.computeIfAbsent(k, s -> new ArrayList<>());
    numbers.add(v);
});
TreeSet<String> contacts = new TreeSet<>();
phoneBookByName.forEach((k, v) -> contacts.add(k + " - " + String.join(", ", v)));
return contacts;

or using streams或使用流

Map<String, List<String>> phoneBookByName = phoneBook.entrySet().stream()
    .collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
return phoneBookByName.entrySet().stream()
    .map(entry -> entry.getKey() + " - " + String.join(", ", entry.getValue()))
    .collect(Collectors.toCollection(TreeSet::new));

暂无
暂无

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

相关问题 给定哈希表中的键,如果我的值有多种数据类型,我如何返回值 - Given Key in HashTable how do I return the value if my value have multiple data types 如何将String变量设置为与滑块上的值相等? - How do I set a String variable equal to that of the value on a slider? 如何确定两个ImmutableMaps中具有完全相同的(键,值)对? - How do I confirm that two ImmutableMaps have the exact same (key,value) pairs in them? 如何在JFace ComboViewer中设置键和值? - How do I Set a Key and a Value in a JFace ComboViewer? 如何将 json 值转换为键? 基本上我想将一个键的值用作“键”,将另一个键的值用作值 - How to convert json values to keys? Basically I want to use value of one key as a “key” and value of other as value 如何查找数组是否有任何两个元素 (i+j) 等于给定的键? - How do I find if the array have any two elements (i+j) which is equal to my given key? 如何将{key1 = value1,key2 = value2}之类的字符串转换为json字符串或Jsonobject? - how do I convert a String like {key1=value1, key2=value2} into a json String or Jsonobject? 如果我有钥匙,如何从地图中获取值? - How can I get a value from a map if I have the key? Hashmap-键:字符串,值:布尔值。 如何在if语句中测试值? - Hashmap - Key: String, Value: Boolean. How do I test the value within an if statement? 我如何通过 Jsoup 查询 html 键/值对的值 - How do i Jsoup query for the value of a html key/value pair
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM