Java - How to create new Entry (key, value)

前端 未结 11 1539
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 09:55

I\'d like to create new item that similarly to Util.Map.Entry that will contain the structure key, value.

The problem is that

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 10:01

    Example of AbstractMap.SimpleEntry:

    import java.util.Map; 
    import java.util.AbstractMap;
    import java.util.AbstractMap.SimpleEntry;
    

    Instantiate:

    ArrayList> arr = 
        new ArrayList>();
    

    Add rows:

    arr.add(new AbstractMap.SimpleEntry(2, 3));
    arr.add(new AbstractMap.SimpleEntry(20, 30));
    arr.add(new AbstractMap.SimpleEntry(2, 4));
    

    Fetch rows:

    System.out.println(arr.get(0).getKey());
    System.out.println(arr.get(0).getValue());
    System.out.println(arr.get(1).getKey());
    System.out.println(arr.get(1).getValue());
    System.out.println(arr.get(2).getKey());
    System.out.println(arr.get(2).getValue());
    

    Should print:

    2
    3
    20
    30
    2
    4
    

    It's good for defining edges of graph structures. Like the ones between neurons in your head.

提交回复
热议问题