【问题标题】:How do I create a dictionary in java similar to the python dictionary and input multiple values at once?如何在java中创建类似于python字典的字典并一次输入多个值?
【发布时间】:2021-04-04 14:15:36
【问题描述】:
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() 函数?没有更好的方法吗?另外,解释为什么在变量 values 上使用 clear()remove() 函数会导致它从变量 graph 中删除。我希望结构是可变的。

【问题讨论】:

    标签: java collections hashmap hashset


    【解决方案1】:

    首先,它看起来不像 python 字典。字典的python字典将是

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

    元组字典是

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

    在 Java 中,您可以表达为 Map&lt;Integer, Map&lt;Character, Character&gt;&gt;,(或者如果您的意思是 Map&lt;Integer, List&lt;Character&gt;&gt;),但这会导致非惯用的、繁琐的代码。 p>

    由于您的数据结构是连续整数的映射,因此更好的表示形式是数组:

    // 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 公式更易于使用(且更高效)的方式。

    【讨论】:

      【解决方案2】:

      以下 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&lt;Integer, Set&lt;String&gt;&gt;,不像 Python,结果是可变的。

      打印出来,看起来像这样,因为MapHashMap,所以顺序发生了变化:

      {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]}
      

      【讨论】:

        【解决方案3】:

        由于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,你可以使用这样的语句,结果是modifiable

        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() copies object or reference to object?
        Invert a Map with redundant values to produce a multimap

        【讨论】:

          猜你喜欢
          • 2019-03-15
          • 2017-10-26
          • 2021-05-23
          • 1970-01-01
          • 2016-09-30
          • 1970-01-01
          • 2021-05-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多