A Java collection of value pairs? (tuples?)

后端 未结 19 2294
名媛妹妹
名媛妹妹 2020-11-22 05:43

I like how Java has a Map where you can define the types of each entry in the map, for example .

What I\'m looking for is a type

相关标签:
19条回答
  • 2020-11-22 05:57

    just create a class like

    class tuples 
    { 
    int x;
    int y;
    } 
    

    then create List of this objects of tuples

    List<tuples> list = new ArrayList<tuples>();
    

    so you can also implement other new data structures in the same way.

    0 讨论(0)
  • 2020-11-22 05:57

    Spring has a Pair<S,T> type in the Data Utils package org.springframework.data.util

    Pair<String,Integer> pair = Pair.of("Test", 123);
    System.out.println(pair.getFirst());
    System.out.println(pair.getSecond());
    
    0 讨论(0)
  • 2020-11-22 06:00

    Map.Entry

    These built-in classes are an option, too. Both implement the Map.Entry interface.

    • AbstractMap.SimpleEntry
    • AbstractMap.SimpleImmutableEntry

    0 讨论(0)
  • 2020-11-22 06:00

    What about "Apache Commons Lang 3" Pair class and the relative subclasses ?

        import org.apache.commons.lang3.tuple.ImmutablePair;
        import org.apache.commons.lang3.tuple.Pair;
        ...
        @SuppressWarnings("unchecked")
        Pair<String, Integer>[] arr = new ImmutablePair[]{
                ImmutablePair.of("A", 1),
                ImmutablePair.of("B", 2)};
    
        // both access the 'left' part
        String key = arr[0].getKey();
        String left = arr[0].getLeft();
    
        // both access the 'right' part
        Integer value = arr[0].getValue();
        Integer right = arr[0].getRight();
    

    ImmutablePair is a specific subclass that does not allow the values in the pair to be modified, but there are others implementations with different semantic. These are the Maven coordinates, if you need them.

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.4</version>
            </dependency>
    
    0 讨论(0)
  • 2020-11-22 06:00

    This is based on JavaHelp4u 's code.

    Less verbose and shows how to do in one line and how to loop over things.

    //======>  Imports
    import java.util.AbstractMap.SimpleEntry;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map.Entry;
    
    //======>  Single Entry
    SimpleEntry<String, String> myEntry = new SimpleEntry<String, String>("ID", "Text");
    System.out.println("key: " + myEntry.getKey() + "    value:" + myEntry.getValue());
    System.out.println();
    
    //======>  List of Entries
    List<Entry<String,String>> pairList = new ArrayList<>();
    
    //-- Specify manually
    Entry<String,String> firstButton = new SimpleEntry<String, String>("Red ", "Way out");
    pairList.add(firstButton);
    
    //-- one liner:
    pairList.add(new SimpleEntry<String,String>("Gray", "Alternate route"));  //Ananomous add.
    
    //-- Iterate over Entry array:
    for (Entry<String, String> entr : pairList) {
        System.out.println("Button: " + entr.getKey() + "    Label: " + entr.getValue());
    }
    
    0 讨论(0)
  • 2020-11-22 06:02

    AbstractMap.SimpleEntry

    Easy you are looking for this:

    java.util.List<java.util.Map.Entry<String,Integer>> pairList= new java.util.ArrayList<>();
    

    How can you fill it?

    java.util.Map.Entry<String,Integer> pair1=new java.util.AbstractMap.SimpleEntry<>("Not Unique key1",1);
    java.util.Map.Entry<String,Integer> pair2=new java.util.AbstractMap.SimpleEntry<>("Not Unique key2",2);
    pairList.add(pair1);
    pairList.add(pair2);
    

    This simplifies to:

    Entry<String,Integer> pair1=new SimpleEntry<>("Not Unique key1",1);
    Entry<String,Integer> pair2=new SimpleEntry<>("Not Unique key2",2);
    pairList.add(pair1);
    pairList.add(pair2);
    

    And, with the help of a createEntry method, can further reduce the verbosity to:

    pairList.add(createEntry("Not Unique key1", 1));
    pairList.add(createEntry("Not Unique key2", 2));
    

    Since ArrayList isn't final, it can be subclassed to expose an of method (and the aforementioned createEntry method), resulting in the syntactically terse:

    TupleList<java.util.Map.Entry<String,Integer>> pair = new TupleList<>();
    pair.of("Not Unique key1", 1);
    pair.of("Not Unique key2", 2);
    
    0 讨论(0)
提交回复
热议问题