-1

Using the new collections from Google's Guava, i was building a Map and adding values as

multimap.put("Date",somestring);
multimap.put("AccountNo",somestring);
multimap.put("Amount",somestring);
multimap.put("Status",somestring);

Now while iterating over the map i wanted to retrieve the values in same order as they were put in map like

12-01-2015 909123423133 2000 004

12-02-2015 909123423134 3000 005

12-03-2015 909123423135 4000 006

12-04-2015 909123423136 5000 007

The way i am iterating retrieves all dates first then all account no , then amounts and finally all statuses .

Iterator itr= ejLogMap.entries().iterator();
            //System.out.println("Map Size:"+ejLogMap.size());
            while(itr.hasNext()){
                Map.Entry pair= (Map.Entry)itr.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
                //itr.remove();
            }
Wasim Wani
  • 555
  • 2
  • 11
  • 24
  • 2
    Can you clarify the error/issue you are having? – Kalel Wade Apr 02 '15 at 17:17
  • I am getting output as Amount = 10,000.00 Amount = 1,000.00 Amount = 20,000.00 Date = 14/01/15 Date = 14/01/15 Date = 14/01/15 Account_Number = 09990701650014165 Account_Number = 09990801000014165 Account_Number = 09990987600005094 while as i want output like posted above – Wasim Wani Apr 02 '15 at 17:19
  • please clarify why you need a multimap. java comes with a LinkedHashMap that provides insertion order iterators. – muued Apr 02 '15 at 17:21
  • I am getting output as Amount = 10,000.00 Amount = 1,000.00 Amount = 20,000.00 Date = 14/01/15 Date = 14/01/15 Date = 14/01/15 Account_Number = 09990701650014165 Account_Number = 09990801000014165 Account_Number = 09990987600005094 while as i want output like posted above – Wasim Wani Apr 02 '15 at 17:23
  • Actually i wanted to keep track of transactions in a way they were executed .e.g In plain english , say an account number 09990701650014165 was debited by 400 on 12-10-13 with status code 004... i wanted to retrieve output in Table format with Date , Account No, Amount, Status as headers and then fill that Table with its corresponding Values – Wasim Wani Apr 02 '15 at 17:27
  • have you tried the proposed solutions that actually does output the data the way you asked? – T.Gounelle Apr 03 '15 at 08:55

2 Answers2

1

google guava comes with the LinkedListMultimap where the iteration order corresponds to the insertion order. I guess you could use that class or just reconsider whether a multimap is really what you want here.

For what you described in the comments I recommend writing a new class containing the information (date, accountNo, ...) and insert instances of this class into an ArrayList, LinkedList, ...

muued
  • 1,666
  • 13
  • 25
0

Assuming your multimap instance is a subclass of ListMultimap<String, String> to preserve insertion order and you inserted the (date, accountNo, ...) in sequence, then the following should give you the expected output.

Iterator<String> dates = ejLogMap.get("Date").iterator();
Iterator<String> accounts = ejLogMap.get("AccountNo").iterator();

while (dates.hasNext() && accounts.hasNext()) {
    System.out.println(dates.next() + " " + accounts.net());
}

EDIT triggered by (fully justified) @mueed comment - As-is, in your deisgn, the data is organised by columns ("Date", "AccountNo",...). You could reorganise it in rows, where each item is actually a tuple (date, accountNo, amount, status) that can be implemented in a specific POJO class, e.g.

public class Transaction {
   String accountNo;
   int amount; 
   Date date;
   int status; // or an enum
   // constructor
   // getters
}

Then you can easily process a List<Transaction> or even better if transactions are unique a Set<Transaction>, sort it on the property you want, filter it, etc. keeping the consistency of each transaction object. If you often need to look up Transaction objects on a specific property, you can build a Map<T, Transaction> where the key is - for instance - the accountNo. And if you need to preserve the insertion order use a LinkedHashMap<T, Transaction>.

T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
  • This would still mean misusing multimap. You rely on the insertion order to gather information. If one of the fields is missing at some point, all future information combinations are screwed up. – muued Apr 02 '15 at 17:47
  • Well, it answer OP question. I would certainly not have chosen this design. And the problem you mention is the same for your suggestion. – T.Gounelle Apr 02 '15 at 19:38