i have a requirement in which i will retrieve all the records from the DB in a sorted form. These records will be sorted by “EnteredIds” field in DB.I am getting these records perfectly. I am placing these records in Map. So certainly I will have my map like :
<field1, EnteredId1>
<field4, EnteredId1>
<field3, EnteredId1>
<field2, EnteredId1>
<field6, EnteredId2>
<field1, EnteredId2>
<field2, EnteredId2>
where EnteredId is in sorted form (i.e 1,1,1,1,2,2,2,…).
Now I want another list which will have objects according to the EnteredIds Like
Object1 will contain 4 records having EnteredId = EnteredId1.
Object2 will contain 3 records having EnteredId = EnteredId2.
and so on..
I have tried it using Map but the logic seems to be very much time consuming and very less optimized.
Please help..
Thanks in advance..
Although I still don’t fully understand your question, I’ll try an attempt to answer it.
I assume you have a map like this:
I further assume you want to get it the other way round, i.e.
This could be achieved by using a
Map<Id,List<Field>>or better aMultiMapwhich Google Guava or Apache Commons Collections provide.