Conversion of List of CustomObject to Guava Table collection with less complexity

Viewed 1297

I have

class CustomObject {
Integer day;
List<InnerObject> innerObjects;
///getter setter

}

class InnerObject {
String id;
List<String> someVal;
//getter setter

}

I have a

List<CustomObject>

and I want

Table<String, Integer, List<String>>

I want table to represent id (from InnerObject) -> (day (from Custom object), List of someVal (from InnerObject)

Just to make it clean I tweaked names a bit but structure is same as what is expected.

Now how I am doing is

final List<CustomObject> objects = ???
final Map<Integer, List<InnerObject>> dayVsInnerObjects = objects.stream()
.collect(toMap(CustomObject::getDay, CustomObject::getInnerObjects));


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

 dayVsInnerObjects.forEach((day, innerObjects) -> 
                            innerObjects.forEach(i -> {
                             table.put(i.getId(), day, i.getSomeVal());
            })
);

My questions:

  1. Is there a better way of doing this? may be a better guava/Collection API that can make it a bit cleaner.
  2. Right now table is being populated and it is mutable. can we have a way to make it immutable while creating it.
  3. Time complexity if can be reduced here.
2 Answers
Related