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
)).
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:
- Use Map instead of using List.
- Remove lob from ServiceItem.
- Override
equals
andhashCode
methods of ServiceItem (to make sure dups won't be inserted). - 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));
}
}
}``
精彩评论