开发者

Removing Duplicate from Arraylist and Getting the duplicate of one property in a list

开发者 https://www.devze.com 2023-01-19 19:43 出处:网络
i have one arraylist say \"templist\" it will haveitems of item say \"Stool\" now list contains duplicates

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 pr开发者_高级运维operty 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;

}


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

Set set = new HashSet(templist)


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.


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));
     }

}   

}``

0

精彩评论

暂无评论...
验证码 换一张
取 消