1

Hello I want to fill an hashmap with value => key :idissue value:arraylist (list of assignee) The problem that my function return the same liste of assignee for all my issues: My map{10700=[x,y,z], 10500=[x,y,z],10600=[x,y,z].......

Or the result should be like this: My map{10700=[x,y,z], 10500=[w,v,q,v,t,z],10600=[m,r,t,i]....... I treid to clear my list listassignee.clear(); after the put instruction but in this case is empty : My map{10700=[], 10500=[], 10601=[], 10400=[], 10401=[], 10301=[], 10300=[], 10600=[]}

Any suggestion please??

public HashMap<String ,ArrayList<String>> MAPAssigneeByissue() throws SQLException
{
    Issue iss =new Issue(); //a class return list of issue
    ArrayList<String> listassignee = new ArrayList<String>();

    HashMap<String ,ArrayList<String>> mapTicketassignee = new HashMap<String ,ArrayList<String>> ();


    Connectionx conn=new Connectionx();


    for (Iterator it = iss.getListissue().iterator();it.hasNext();)
    {
        Object o = (Object) it.next();

        String sql2;
        sql2=" my SQL:retrieve list assignee for each ticket (o.tostring) ";
        ResultSet rs2 = conn.st. executeQuery(sql2);

        while(rs2.next())
        {
            String str=  rs2.getString(1);
            listassignee.add(str);

        }
        mapTicketassignee.put(o.toString(),listassignee);
        //listassignee.clear();
    }

    System.out.println("My map"+mapTicketassignee.toString());
    return mapTicketassignee;
}

3 Answers 3

4

Instead of clearing the list you need to create a new instance of the assignee list for each map value.

Basically this line needs to be inside your loop instead of before:

ArrayList<String> listassignee = new ArrayList<String>();
Sign up to request clarification or add additional context in comments.

Comments

2

Every time you iterate trough
listassignee = new ArrayList<String>()

Comments

1

I have rewrtten your code to make it work (style issues notwithstanding)

public HashMap<String, ArrayList<String>> MAPAssigneeByissue() throws SQLException {  
    Issue iss = new Issue(); //a class return list of issue

    HashMap<String, ArrayList<String>> mapTicketassignee = new HashMap<String, ArrayList<String>> ();

    Connectionx conn=new Connectionx();

    for (Iterator it = iss.getListissue().iterator();it.hasNext();) {
        Object o = (Object) it.next();

        String sql2 = "my SQL:retrieve list assignee for each ticket (o.tostring) ";
        ResultSet rs2 = conn.st.executeQuery(sql2);

        ArrayList<String> listassignee = new ArrayList<String>();

        while(rs2.next()) {
            String str = rs2.getString(1);
            listassignee.add(str);
        }

        mapTicketassignee.put(o.toString(), listassignee);
    }

    System.out.println("My map"+mapTicketassignee.toString());
    return mapTicketassignee;
}

1 Comment

thank you for the help it worked I have followed Bohemian reply ,shame on me I haven't seen that ,really I was so tired :(

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.