I'm not quite sure what your needs are (or a specific use case), but I will try to guess. Other answers suggest using Linked * Multimap or Immutable, but to get the desired result (shown in the question) using Multimap
you will need to create some kind of fancy map (I will talk about this later) or, for example, create three temporary meetings, the second and the third value for each key (they will be in the input order if you use one of the proposed Multimap
implementations). Preferably this will be one of ListMultimaps
, as you can ListMultimaps
over multimap.keySet()
to get lists with values ββavailable by index:
final ListMultimap<String,String> multimap = LinkedListMultimap.create(); // put values from question here final List<Object> firstValues = Lists.newArrayList(); for (final String key: multimap.keySet()) { firstValues.add(multimap.get(key).get(0)); } System.out.println(firstValues); // prints [value1, value11, value111] // similar for multimap.get(key).get(1) and so on
but the disadvantage is that you will need to create three lists for example, which makes this solution rather inflexible. So maybe it's better to post the {First, Second, Third} collection of values ββin Map> which brings me to the point:
Perhaps you should use Table ?
A table is created as a collection that associates an ordered key pair, called a row key and a column key, with a single value and, more importantly here, has representations of rows and columns. I will use ArrayTable
here:
final ArrayTable<String, Integer, Object> table = ArrayTable.create( ImmutableList.of("1", "2", "3"), ImmutableList.of(0, 1, 2)); table.put("1", 0, "value1"); table.put("1", 1, "value2"); table.put("1", 2, "value3"); table.put("2", 0, "value11"); table.put("2", 1, "value22"); table.put("2", 2, "value33"); table.put("3", 0, "value111"); table.put("3", 1, "value222"); table.put("3", 2, "value333"); for (final Integer columnKey : table.columnKeyList()) { System.out.println(table.column(columnKey).values()); }
I intentionally used String for the string keys, which are [1, 2, 3, ...]. Integers (for example, what you did in the question) and integers for column columns starting at 0 ([0, 1, 2, ...]) to show similarities to the previous example using List get(int)
in the value collection multimaps.
Hope this will be useful, mainly when deciding what you want;)
PS I use ArrayTable
here because it has an easier way to create a fixed set (universe) of string / key values ββthan ImmutableTable
, but if mutability is not required you should use it instead of a single change - ImmutableTable
(and any other table implementation) does not have columnKeyList()
method, but only columnKeySet()
, which does the same thing, but slower for ArrayTable
. And of course, ImmutableTable.Builder
or ImmutableTable.copyOf(Table)
.