如何将一组转换为地图

时间:2017-03-21 08:02:21

标签: java guava

我有一个集合,我想将其转换为map,以便稍后在guava的Maps.difference()中使用它。我只关心差异中的关键 想出了这个版本:

private <T> Map<T, T> toMap(Set<T> set) {
  return set.stream().collect(Collectors.toMap(Function.identity(), Function.identity()));
}

但是,我知道通常,一个集合有一个map的支持字段。这是我用来创建地图的方法:

public static <E> Set<E> newConcurrentHashSet() {
  return Collections.newSetFromMap(new ConcurrentHashMap<E, Boolean>());
}

因为我只需要我想的钥匙,所以我可以以某种方式获得这个领域的视图。任何想法?

5 个答案:

答案 0 :(得分:8)

我最终得到了一个相当简单的Java 8系统解决方案,如下所示:

Map<String, Foo> map = fooSet.stream().collect(Collectors.toMap(Foo::getKey, e -> e));
  • fooSet是一组Foo类型的对象,即Set<Foo> fooSet
  • Foo有一个名为getKey的getter,它返回一个String

答案 1 :(得分:3)

您可以将url = urlopen("http://www.test.com/file.zip") zipfile = ZipFile(StringIO(url.read())) 转换为Set(相同的键和值来自Map的元素),如下所示:

Set

答案 2 :(得分:1)

来自comment

  

我想知道哪些项目只在左边,哪些只在右边,哪个是共同的(类似于地图差异)

使用removeAll()和[retainAll()][3]

示例:

Set<Integer> set1 = new HashSet<>(Arrays.asList(1,3,5,7,9));
Set<Integer> set2 = new HashSet<>(Arrays.asList(3,4,5,6,7));

Set<Integer> onlyIn1 = new HashSet<>(set1);
onlyIn1.removeAll(set2);

Set<Integer> onlyIn2 = new HashSet<>(set2);
onlyIn2.removeAll(set1);

Set<Integer> inBoth = new HashSet<>(set1);
inBoth.retainAll(set2);

System.out.println("set1: " + set1);
System.out.println("set2: " + set2);
System.out.println("onlyIn1: " + onlyIn1);
System.out.println("onlyIn2: " + onlyIn2);
System.out.println("inBoth : " + inBoth);

输出

set1: [1, 3, 5, 7, 9]
set2: [3, 4, 5, 6, 7]
onlyIn1: [1, 9]
onlyIn2: [4, 6]
inBoth : [3, 5, 7]

现在,如果你想知道所有的值以及它们的位置,你可以这样做(Java 8):

Set<Integer> setA = new HashSet<>(Arrays.asList(1,3,5,7,9));
Set<Integer> setB = new HashSet<>(Arrays.asList(3,4,5,6,7));

Map<Integer, String> map = new HashMap<>();
for (Integer i : setA)
    map.put(i, "In A");
for (Integer i : setB)
    map.compute(i, (k, v) -> (v == null ? "In B" : "In Both"));

System.out.println("setA: " + setA);
System.out.println("setB: " + setB);
map.entrySet().stream().forEach(System.out::println);

输出

setA: [1, 3, 5, 7, 9]
setB: [3, 4, 5, 6, 7]
1=In A
3=In Both
4=In B
5=In Both
6=In B
7=In Both
9=In A

答案 3 :(得分:0)

查看类似的答案here

假设您的原始集合是一组值(原始数据中没有键!),您需要为新创建的地图指定键。番石榴Maps.uniqueIndex可能会有所帮助(见here

否则,如果原始集是您要保留的一组键(原始数据中没有值!),则需要为新创建的映射指定默认值或特定值。番石榴Maps.toMap在这里可能会有所帮助。 (查看更多here

答案 4 :(得分:0)

developer的答案的改进:

.flights__types
        max-width: 1120px
        margin: 30px auto
        &__title
            font-size: 22px
            font-weight: bold
        &__container
            width: 100%
            display: flex
            flex-direction: row
            justify-content: space-between
            flex-wrap: wrap
            margin-top: 13px
            +r(1439)
                margin-top: 23px
            .flight__item__companies,
            .flight__item__direct-flights,
            .flight__item__air-alliances,
            .flight__item__transplantation,
            .flight__item__types-of-aircraft,
            .flight__item__airports
                width: 45%
                padding: 19px 20px 0px 20px
                background-color: #ffffff
                margin-top: 30px
                height: auto
                .title
                    font-size: 18px
                    font-weight: bold
                .decript
                    margin-top: 3px
                    font-size: 14px
                    +r(767)
                        margin-top: 4px
            .flight__item__companies,
            .flight__item__direct-flights
                margin-top: 0

或者如果您静态导入Map<String, Foo> map = fooSet.stream().collect(Collectors.toMap(Foo::getKey, Function.identity())); Collectors.toMap

Function.identity
相关问题