-1

Possible Duplicate:
Java: Detect duplicates in ArrayList?

I have a strange issue. Im sending out email alerts when remarks get added to a form. Basically each remarks has its own individual id, but multiple remarks can attached to certain form.

In this scenario I have 3 remarks that I added. Two were added to 1 form and the last to a different form. So I would like it just to send 2 forms. The email is sending out 3 forms. I understand that 3 beans will get populated, but 2 will have the same data because 2 remarks belong to the same form. I tried some algorithms I seen posted on here for dups in and what not, but I guess the code is not recognizing duplicate elements because the remarks each have their own id??

For iteration purposes the JSP requires I throw the list of hotParts into an additional ArrayList

This is the code..there is more to it, but this is the part that matters

Thanks

   try
                    {
                        List<Object> dataList = new ArrayList<Object>();
                        Set<Long> ids = new HashSet<Long>();

                        for (HotPartsRemarkBean remark : latestHotPartRemarks)
                        {
                            ids.add(remark.getHpId());  
                        }

                            for ( Long id : ids)
                            {
                                hotPart = hotPartsDAO.getHotPartById(id);
                                dataList.add( hotPart ); //List Of Hot Parts Beans
                            }


               THIS WORKS!!!!!!! :)
Community
  • 1
  • 1
Doc Holiday
  • 9,928
  • 32
  • 98
  • 151

5 Answers5

4
  • If you don't need random-access, use Set (HashSet)
  • If you don't need random-access, but need to preserve order, use LinkedHashSet
  • If you need it to be a List, but need uniqueness, use SetUniqueList from commons-collections.
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I would also suggest overriding Object.equals(Object other):boolean to compare field values and ignore the id attributes. It is also a good practice, then, to also override Object.hashCode():int to hash according to the same fields you compared in Object.equals(Object other):boolean. – Dylon Dec 22 '11 at 15:11
1

If you're looking for a data structure that implicitly forbids duplicates, use a Set.

mre
  • 43,520
  • 33
  • 120
  • 170
0

@gurung had a great point that I wanted to emphasize but he deleted his post (edit: looks like he undeleted it). You are adding the same list to dataList each time. Changes to hotParts will modify every entry of dataList because each entry points to the same object. So you will be left with a dataList that contains N identical lists (in fact, the same list).

If you want each element of dataList to be independent, you need to create a new hotParts each iteration of your loop:

for (...) {
    List<HotPartsBean> hotParts = new ArrayList<HotPartsBean>();
    ...
    dataList.add(hotParts);
} 

Or, depending on your expectations, you could copy the list when you put it into dataList:

dataList.add(new ArrayList<HotPartsBean>(hotParts));
Mark Peters
  • 80,126
  • 17
  • 159
  • 190
0

Implement 'Comparable' interface for HotPartsBean and override the 'compareTo' method. Then use the same algorithm that you used (probably the one where you convert from List to Set) or just use a different datastructure. This will avoid the duplicates.

Also, refactor your code and define List<Object> dataList = new ArrayList<Object>(); as below:

List<List<HotPartsBean>> dataList = new ArrayList<List<HotPartsBean>>();
bchetty
  • 2,231
  • 1
  • 19
  • 26
0
 try
                    {
                        List<Object> dataList = new ArrayList<Object>();
                        Set<Long> ids = new HashSet<Long>();

                        for (HotPartsRemarkBean remark : latestHotPartRemarks)
                        {
                            ids.add(remark.getHpId());  
                        }

                            for ( Long id : ids)
                            {
                                hotPart = hotPartsDAO.getHotPartById(id);
                                dataList.add( hotPart ); //List Of Hot Parts Beans
                            }
Doc Holiday
  • 9,928
  • 32
  • 98
  • 151