2

I've a row in db returned by query below. Columns to select include rec, head, amount. I want to sort the rows by head column. I tried Map where string for head and list for other two columns.

I've hit the wall with my non-working code posted below. How would I append another list to list of repeated key. Documentation says it replaces the value for same key whereas I need it appended to the list value. I would be really greatful for any help.

Query q= session.createQuery("select tally_receipt_prefix, tally_receipt_no, tally_head, tally_amount from Tally_table where tally_system_date='"+fmtd_date+"' and tally_dbcr_indicator='DB' and tally_mode='Ca' order by tally_head,tally_receipt_prefix,tally_receipt_no"); System.out.println("query "+q);

List heads=new ArrayList();

for(Iterator it=q.iterate(); it.hasNext(); )
{
    Object[] row= (Object[]) it.next();

    payincash1=new LinkedHashMap<String, List>();

    heads.add((String)row[2]);

    List tails = null;
    tails=new ArrayList();
    tails.add((String)row[0]);
    tails.add((String)row[1]);
    tails.add((String)row[3]);

    System.out.println("heads in dao from iter 1: "+heads);  
    System.out.println("tails in dao from iter1 on: "+tails);

    if(heads.contains((String)row[2]))  // for head in temp list
    {
        System.out.println("in first if");
        if(payincash1.containsKey((String)row[2]))     
        {
            System.out.println("map if repeat: "+payincash1);
            payincash1.put((String)row[2],tails);
        }

    } 
    else
    {

        System.out.println("map if not repeat: "+payincash1);
        payincash1.put((String)row[2], tails);

    }

}
2
  • Can you please ask the question in a simpler way, such as "what is a sorted container which can have duplicates and has O(logN) search". BTW, is that the question? Commented Aug 29, 2012 at 2:40
  • Sorry if my question was not clear. I want to add a key (string in this case) from heads list and value (in this case a list) from tails list in my code. For each repeated head I want the value (here list) to be appended to old value ( old list ) and my efforts doing this were futile. Commented Aug 29, 2012 at 2:46

2 Answers 2

4

Sounds more like you want a list of lists

Something like Map<String, List<List>>

Then you'd end up with something like...

Map<String, List<List>> payincash1 = new LinkedHashMap<String, List<List>>();

heads.add((String) row[2]);

List tails = null;
tails = new ArrayList();
tails.add((String) row[0]);
tails.add((String) row[1]);
tails.add((String) row[3]);

System.out.println("heads in dao from iter 1: " + heads);
System.out.println("tails in dao from iter1 on: " + tails);

List master = payincash1.get((String)row[2]);
if (master == null) {

    master = new List();
    payincash1.put((String)row[2], master);

}

master.add(tails);

Now, personally, I'd be creating a "data" object that would contain all this information.

public class MyData {
    private String rec, head, amount, ??; // Apparently you have another variable I don't know about
    public MyData(String rec, String head, String amount, String ??) {
        // Initalise...
    }
    // Setters and getters not inclueded
}

Then you could do something like this...

Map<String, List<MyData>> payincash1 = new LinkedHashMap<String, List<MyData>>();

MyData data = new MyData(row[0], row[1], row[2], row[3]);

List master = payincash1.get((String)row[2]);
if (master == null) {

    master = new List<MyData>();
    payincash1.put((String)row[2], master);

}

master.add(data);

Which is a little cleaner (IMHO)

Sign up to request clarification or add additional context in comments.

Comments

4

From what I understand you need Multimap of guava library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.