0

So i have an interface.

public interface ARecord {
    public BigInteger getAutoID();
    public String getACompId();
}

and

public class APPPRecord extends AbstratAPRecord implements ARecord{
    private BigInteger autoID;
    private String ACompId = null;
   //setter and getter}

In service,

    List<APPPRecord> PFRecord = null;
    while(scroll.next()){
      APPPRecord item = (APPPRecord) scroll.get(0);
      List<ARecord> recs = new ArrayList<ARecord>();
      recs.addAll(PFRecord);

My PFRecord list has results that are being duplicated. I have to use hash maps that can check for ACompId contains key. If the key already exists don't pass it to recs.addAll. How can I go about doing this? Any help appreciated

Update: I tried Set and still see duplicate results with HashCode() and equals() in my model class.

    for(ARecord records:recs){
        uniqueRecs.put(records.getACompId(), records);
        Set<String> keys = uniqueRecs.keySet();
        for(String key: keys){
            log.debug("keys " + key);
        }
        }

Also tried hashMaps.

    HashMap<String, ARecord > uniqueRecs = new HashMap<String, ARecord >();
    for(ARecord records:recs){
    if(!uniqueRecs.containsKey(records.getACompId())){
        uniqueRecs.put(records.getACompId(), records);
        for (String key : uniqueRecs.keySet()) {
                log.debug("unique record " + key);
        }

        }

    }

Both of them still produce duplicate results. Any ideas?

Jay
  • 471
  • 1
  • 4
  • 11
  • 1
    where exactly have u implemented the `Map`? – nafas Aug 04 '15 at 15:45
  • beside ur probably better off using `Set`. – nafas Aug 04 '15 at 15:46
  • needs to be implemented after/before the last line in service where it checks for if the record is already present in PFRecord, don't pass it to recs.addAll. – Jay Aug 04 '15 at 15:46
  • @nafas. I have tried set, hash map, equals. It doesn't work. All I want to do is to check for the record if it exists already, don't pass it. – Jay Aug 04 '15 at 15:47
  • u need to override `equals` and `hashCode` method inside ur `ARecord` Method. – nafas Aug 04 '15 at 15:49
  • here is a perfect example for that:http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java – nafas Aug 04 '15 at 15:50

2 Answers2

0

Replace List<ARecord> recs = new ArrayList<ARecord>();

with Set<ARecord> recs = new HashSet<ARecord>();

and make sure the implementations of the ARecord implements the hashcode/equals methods properly so that your set only contains distinct instances.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
0

Make use of HashMap<K,V> class, This document will provide you with the required API.

And according to your requirement you can make use of .containsKey(Object key) method to check for the existence of the key already.

Update: I suggested this because you asked for HashMap, if duplication is the only issue you have then you can make use of .contains(Object o) method on the List you already have in-place. the interface List<> provides this method which returns a boolean value based on the presence of the value.

  • hmm. This is what I need to implement but the use of custom array is confusing me. so far, Map> recsMap = new HashMap>(); recsMap.put(//need to pass in to APPPRecord. ACompId) then check for .containskey() – Jay Aug 04 '15 at 15:56
  • Yeah you can make use of the autoId or generate some other unique id for each of the record. – Vamshi Krishna Alladi Aug 04 '15 at 16:03