'3-Dimension List or Map

I need something like a 3-dimension (like a list or a map), which I fill with 2 Strings and an Integer within a loop. But, unfortunately I don't know which data structure to use and how.

// something like a 3-dimensional myData
for (int i = 0; i < 10; i++) {
    myData.add("abc", "def", 123);
}


Solution 1:[1]

Create an object that encapsulates the three together and add them to an array or List:

public class Foo {
    private String s1;
    private String s2; 
    private int v3;
    // ctors, getters, etc.
}

List<Foo> foos = new ArrayList<Foo>();
for (int i = 0; i < 10; ++i) {
    foos.add(new Foo("abc", "def", 123);
}

If you want to insert into a database, write a DAO class:

public interface FooDao {
    void save(Foo foo);    
}

Implement as needed using JDBC.

Solution 2:[2]

A Google's Guava code would look like this:

import com.google.common.collect.Table;
import com.google.common.collect.HashBasedTable;

Table<String, String, Integer> table = HashBasedTable.create();

for (int i = 0; i < 10; i++) {
    table.put("abc", "def", i);
}

Code above will construct a HashMap inside a HashMap with a constructor that looks like this:

Table<String, String, Integer> table = Tables.newCustomTable(
        Maps.<String, Map<String, Integer>>newHashMap(),
        new Supplier<Map<String, Integer>>() {
    @Override
    public Map<String, Integer> get() {
        return Maps.newHashMap();
    }
});

In case you want to override the underlying structures you can easily change it.

Solution 3:[3]

Simply create a class

 class Data{
  String first;
  String second;
  int number;
 }

Solution 4:[4]

The answer depends on what the relationship between the values are.

1) you just want to store all three in the same order as they come: create a custom class that encompasses all three elements and add an instance of this class to a List<MyData>.

2) you want to associate the sirst string with the second-and-third data (and associate the second with the int): create a Map> and add the elements to it (you will have to create the inner map for each new first string)

3) you don't want to keep duplicates, but you don't want/need a map.: Create a custom type (a'la 1)) and put them in a Set<MyData>

3) mix-and match

Solution 5:[5]

For synchronized structure, you can use Tables.synchronizedTable from guava 22 or later.

Table<String, String, Integer> table = Tables.synchronizedTable(HashBasedTable.<String, String, Integer>create());

To add an element into the collection you can do:

table.put("abc", "def", 123);

To remove the element from the collection:

table.remove("abc", "def");

Solution 6:[6]

If you dont want to create a class:

Map<String, Map<String,Integer>> myData;

Solution 7:[7]

You can go with this code!

public class List3D {

    public static class MyList {
        String a = null;
        String b = null;
        String c = null;

        MyList(String a, String b, String c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }

    public static void main(String[] args) {

        List<MyList> myLists = new ArrayList<>();
        myLists.add(new MyList("anshul0", "is", "good"));
        myLists.add(new MyList("anshul1", "is", "good"));
        myLists.add(new MyList("anshul2", "is", "good"));
        myLists.add(new MyList("anshul3", "is", "good"));
        myLists.add(new MyList("anshul4", "is", "good"));
        myLists.add(new MyList("anshul5", "is", "good"));

        for (MyList myLista : myLists)
            System.out.println(myLista.a + myLista.b + myLista.c);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 UmNyobe
Solution 4 Attila
Solution 5 Lucian Bacila
Solution 6 Angel
Solution 7 Massimiliano Kraus