Sveta Tulova
Sveta Tulova

Reputation: 51

Collection for storing elements such as key-value-value

Which collection can be used to store items, such as key-value-value ? For example i need something like this:

  elements.add (0, "first", 1);
  elements.add(1, "second", 2);

Upvotes: 0

Views: 1047

Answers (3)

LuCio
LuCio

Reputation: 5193

Define a class Triple which will take three parameters.

public class Triple<K, V1, V2> {

  private K key;
  private V1 value1;
  private V2 value2;

  public Triple(K a, V1 value1, V2 value2) {
    this.key = a;
    this.value1 = value1;
    this.value2 = value2;
  }

  public K getKey() {
    return key;
  }

  public V1 getValue1() {
    return value1;
  }

  public V2 getValue2() {
    return value2;
  }
}

Then add another class TripleList which will serve as a collection where you can add instances of Triple:

  public  class TripleList<K, V1, V2> implements Iterable<Triple<K, V1, V2>> {

    private List<Triple<K, V1, V2>> triples = new ArrayList<>();

    public void add(K key, V1 value1, V2 value2) {
      triples.add(new Triple<>(key, value1, value2));
    }

    @Override
    public Iterator<Triple<K, V1, V2>> iterator() {
      return triples.iterator();
    }
  }

Using them you can do the following:

  public static void main(String[] args) {
    List<Triple<Integer, String, Integer>> list = new ArrayList<>();
    list.add(new Triple<Integer, String, Integer>(0, "first", 1));
    list.add(new Triple<Integer, String, Integer>(1, "second", 2));

    TripleList<Integer, String, Integer> elements = new TripleList<>();
    elements.add(0, "first", 1);
    elements.add(1, "second", 2);

    for (Triple<Integer, String, Integer> triple : elements) {
       System.out.println(triple.getKey() + "," + triple.getValue1() + "," + triple.getValue2());  
    }
  }

You asked for a Collection. TripleList isn't actually a Collection as it doesn't implement Collection. But this should be possible by delegating to the methods of the inner list triples.

Upvotes: 2

Debapriya Biswas
Debapriya Biswas

Reputation: 1349

if you are using JDK9

Map<Integer,Map.Entry<String,Integer>> map=new HashMap<>();

Now you can create Map.Entry using static method entry(K k,V v) in Map interface https://docs.oracle.com/javase/9/docs/api/java/util/Map.html#entry-K-V-

Life will be fun now for java programmers to return two values from a method using return Map.entry("firstValue","secondValue");

Upvotes: 2

davidxxx
davidxxx

Reputation: 131496

You should define your own class to define a value and use Map<Integer, MyValue> as overall structure.

Example :

public class MyValue{
    public MyValue(String string, int i){
       ...
    }
}

And use it :

Map<Integer, MyValue> elements = new HashMap<>();
elements.put(0, new MyValue("first", 1));

You have as alternative a List as value but a generic List relies on a specific type such as List<String> or List<Integer>. So in your case I would avoid this way as you mix types in the values.

You have also other alternatives that don't require to introduce custom classes but in general this is often not clear for people that have to read/maintain code : javafx.util.Pair<K,V> or java.util.AbstractMap.SimpleImmutableEntry<K, V> are example of them.

Upvotes: 4

Related Questions