0

I have a list of type object as follows,

List<object> recordList

which will contain database record(integer,string) as follows,

[[1,AAAAAAAA],[2,BBBBBB]]

I need to split the data in the list and put into Hashmap<Integer,String> as follows,

I do knw how to split the data form the object list and poplualte the hashmap. How to populate the Hashmap with the data from list?

3
  • 1
    1,AAAAAAAA a single attribute or two different ? Commented Nov 20, 2012 at 7:18
  • Is each entry in the list of type String? Commented Nov 20, 2012 at 7:18
  • I found same question here : stackoverflow.com/questions/4100486/… Commented Nov 20, 2012 at 7:24

4 Answers 4

1
List<String> l = new ArrayList<String>();
Map<Integer,String> m = new HashMap<Integer, String>();
Iterator<String> ite = l.iterator();
while(ite.hasNext())
{
    String sTemp[] =ite.next().split(",");
    m.put(Integer.parseInt(sTemp[0]), sTemp[1]);
}
Sign up to request clarification or add additional context in comments.

Comments

0
   HashMap<Integer, String> map = new HashMap<Integer, String>();

   for (MyObject element : list) {
       if (element != null)
           map.put(element.getInt(), element.getString());
   }

1 Comment

You could explain your answer a bit, at least tell the assumptions you made (since question is rather vague).
0

if your List<object> containsObject[], you can do it like

HashMap<Integer,String> map  = new HashMap<Integer, String>();
for( Object obj : recordList) {
   Object[] objA = (String[]) obj ;
   map.put((Integer) objA[0],(String) objA[1]);
}

Comments

0
package listHashMap;
import java.util.*;
public class MyList {
    public static void main(String [] args)
    {
        List<String> aList=new ArrayList<String>();
        Map<Integer,String> aMap = new HashMap<Integer, String>();
        aList.add("AAAA");
        aList.add("BBBB");
        for(int i=0;i<aList.size();i++){
            aMap.put(i+1,aList.get(i));
        }
        System.out.println(aMap.toString());

    }

}

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.