tags:

views:

64

answers:

3

Hi all,

i have one arraylist say "templist" it will have items of item say "Stool"

now list contains duplicates

1 abc wwww.com lob1
1 abc wwww.com lob2

now i want like

1 abc wwww.com (lob1,lob2)

may be a separated list of lob's alone. how we can do it ? please help

List tempList = new ArrayList();

I added items of type ServiceItem ( which has property like id,name,url,lob)).
Like said i will duplicates for id,name,url as these three can be mapped to different lobs.

I want first three property to be one entry and last property should be list of differnt lobs.

I made Changes to the code. now. Here is the code. Tell me how to achieve the result as 1 | ABC , BCA

public class ListTest {

public static void main(String args[]){

    List TestList = new ArrayList();

    MyBean myBean = new MyBean();
    MyBean myBean2 = new MyBean();
    myBean.setId("1");
    myBean.setLob("ABC");
    myBean2.setId("1");
    myBean2.setLob("BCA");

    TestList.add(myBean);
    TestList.add(myBean2);

}   

}

public class MyBean {

    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getLob() {
        return lob;
    }
    public void setLob(String lob) {
        this.lob = lob;
    }
    private String lob;

}

+1  A: 

Create a set from your list will remove the duplicate, ex:

Set set = new HashSet(templist)
nanda
Ok here is the explanation. My arraylist is having data like the below APS | http://a.net/aps/| ABCAPS|http://a.net/aps/|BCA i want a final list having one entry for the first three values and abc,bca for last (may be another list inside the list)
Rajesh
@Rajesh, I still can't understand it properly. Can you edit the question and format the explanation. A bit code will be very helpful.
nanda
A: 

I would do the following:

  1. Use Map instead of using List.
  2. Remove lob from ServiceItem.
  3. Override equals and hashCode methods of ServiceItem (to make sure dups won't be inserted).
  4. Insert new entry into map where key is this ServiceItem and value is a list (or better set if lobs are unique). Check if contains(ServiceItem) - then add new lob to the list (set) of the value.
Roman
A: 

Finally i solved the problem using apache collections multihashmap . but as of 3.2 it is deprecated so i used 3.1 version.

here is the final code.

public class ListTest {

public static void main(String args[]){

    List<MyBean> TestList = new ArrayList<MyBean>();

    MyBean myBean = new MyBean();
    MyBean myBean2 = new MyBean();
    myBean.setId("1");
    myBean.setLob("ABC");
    myBean2.setId("1");
    myBean2.setLob("BCA");

    TestList.add(myBean);
    TestList.add(myBean2);

    for(int i=0;i<TestList.size();i++){
        MyBean result = (MyBean)TestList.get(i);
        System.out.println("ID:"+result.getId());
        System.out.println("Lob:"+result.getLob());
    }

     MultiMap mhm = new MultiHashMap();
     for(int i=0;i<TestList.size();i++){
         MyBean result = (MyBean)TestList.get(i);
         mhm.put(result.getId(), result.getLob());
     }

     List list = (List) mhm.get("1");

     for(int i=0;i<list.size();i++){
         System.out.println(list.get(i));
     }

}   

}``

Rajesh
MultiMap from Google Guava seems better. http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multimap.html
nanda