如何在java中创建一个类似于python字典的字典并一次输入多个值?

bbmckpt7  于 2021-06-29  发布在  Java
关注(0)|答案(3)|浏览(571)
graph = {1:{'a','b'},2:{'c','d'},3:{'e','f'},4:{'g','h'},5:{'b','d'},6:{'b','e'},7:{'a','c'},8:{'f','h'},9:{'e','g'}}

我所做的:

public class Try {
    public static void main(String[] args) {
        Set<Character> universal = new HashSet<>();
        Collections.addAll(universal, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
        // System.out.println(universal);
        HashMap<Integer, HashSet<Character>> graph =
                new HashMap<Integer, HashSet<Character>>();
        HashSet<Character> values = new HashSet<Character>();
        Collections.addAll(values, 'a', 'b');

        System.out.println(values);
        // Output : [a, b]

        graph.put(1, values);
        System.out.println(graph.get(1));
        // Output : [a, b]

        System.out.println(graph);
        // Output : {1=[a, b]}

        values.removeAll(values);
        System.out.println(graph.get(1));
        // Output : []

        // Why did the value clear out from the graph?
        System.out.println(graph);
        // Output : {1=[]}
    }
}

如何一次输入这些值而不是使用 put() 一直在工作?难道没有更好的办法吗?另外,解释一下原因 clear() 或者 remove() 对变量值的函数用法会导致它从变量图中删除。我希望结构是可变的。

y53ybaqx

y53ybaqx1#

Java 9 可以这样实现,结果可以修改:

Map<Integer, Set<Character>> graph = new HashMap<>(Map.of(
        1, new HashSet<>(Set.of('a', 'b')),
        2, new HashSet<>(Set.of('c', 'd')),
        3, new HashSet<>(Set.of('e', 'f')),
        4, new HashSet<>(Set.of('g', 'h')),
        5, new HashSet<>(Set.of('b', 'd')),
        6, new HashSet<>(Set.of('b', 'e')),
        7, new HashSet<>(Set.of('a', 'c')),
        8, new HashSet<>(Set.of('f', 'h')),
        9, new HashSet<>(Set.of('e', 'g'))));

语句较短,但结果不可修改:

Map<Integer, Set<Character>> immutable = Map.of(
        1, Set.of('a', 'b'),
        2, Set.of('c', 'd'),
        3, Set.of('e', 'f'),
        4, Set.of('g', 'h'),
        5, Set.of('b', 'd'),
        6, Set.of('b', 'e'),
        7, Set.of('a', 'c'),
        8, Set.of('f', 'h'),
        9, Set.of('e', 'g'));

在早期版本中,自从 Java 1.5 ,您可以使用这样的语句,结果是可修改的:

Map<Integer, Set<Character>> graph = new HashMap();

graph.put(1, new HashSet(Arrays.asList('a', 'b')));
graph.put(2, new HashSet(Arrays.asList('c', 'd')));
graph.put(3, new HashSet(Arrays.asList('e', 'f')));
graph.put(4, new HashSet(Arrays.asList('g', 'h')));
graph.put(5, new HashSet(Arrays.asList('b', 'd')));
graph.put(6, new HashSet(Arrays.asList('b', 'e')));
graph.put(7, new HashSet(Arrays.asList('a', 'c')));
graph.put(8, new HashSet(Arrays.asList('f', 'h')));
graph.put(9, new HashSet(Arrays.asList('e', 'g')));

语句较短,结果也可修改:

Map<Integer, Set<Character>> graph = new HashMap() {{
    put(1, new HashSet(Arrays.asList('a', 'b')));
    put(2, new HashSet(Arrays.asList('c', 'd')));
    put(3, new HashSet(Arrays.asList('e', 'f')));
    put(4, new HashSet(Arrays.asList('g', 'h')));
    put(5, new HashSet(Arrays.asList('b', 'd')));
    put(6, new HashSet(Arrays.asList('b', 'e')));
    put(7, new HashSet(Arrays.asList('a', 'c')));
    put(8, new HashSet(Arrays.asList('f', 'h')));
    put(9, new HashSet(Arrays.asList('e', 'g')));
}};

另请参见:
•system.arraycopy()复制对象或对对象的引用?
•使用冗余值反转贴图以生成多重贴图

db2dz4w8

db2dz4w82#

首先,这看起来不像python字典。python词典

{1: {'a': 'b'}, 2: {'c': 'd'}}

一本元组词典

{1: ('a', 'b'), 2: ('c', 'd')}

在java中,您可以将 Map<Integer, Map<Character, Character>> ,(或者可能是 Map<Integer, List<Character>> 如果这是你的意思),但这会导致非惯用的,繁琐的代码。
由于数据结构是连续整数的Map,因此更好的表示形式是数组:

// Using Java 14 record types ...
public record Pair (char first, char second) {}

Pair[] theMapping = new Pair[]{
   null,
   new Pair('a', 'b'),
   new Pair('c', 'd'),
   // etc
};

唯一的问题是 null ... 这是必需的,因为java数组索引从零开始。
这也可以表示为 char[][] 或者在其他方面,这将更容易使用(和更有效)比 MapMap 配方。

d4so4syb

d4so4syb3#

下面的python语句:

graph = {1:{'a','b'},2:{'c','d'},3:{'e','f'},4:{'g','h'},5:{'b','d'},6:{'b','e'},7:{'a','c'},8:{'f','h'},9:{'e','g'}}

可以在java 9+中实现如下:

graph = Map.of(1, Set.of("a", "b"),
               2, Set.of("c", "d"),
               3, Set.of("e", "f"),
               4, Set.of("g", "h"),
               5, Set.of("b", "d"),
               6, Set.of("b", "e"),
               7, Set.of("a", "c"),
               8, Set.of("f", "h"),
               9, Set.of("e", "g"));

结果是不可变的 Map<Integer, Set<String>> ,与python不同,python的结果是可变的。
印刷,看起来像这样,因为 Map 是一个 HashMap :

{4=[g, h], 3=[e, f], 2=[c, d], 1=[a, b], 9=[e, g], 8=[f, h], 7=[a, c], 6=[b, e], 5=[b, d]}

相关问题