0

Ok, So I know this topic has been asked lots of times. All usually referring to using a set of some kind.

Now here is the problem.

I have a RateSheet (ArrayList) which has many rates (object) in it. In this rate object we have the variables:

desName prefix cost

Now to summarise, I want to take this RateSheet and remove all the duplicate rates based on desName.

I have used a set and collection todo this. Which works, but then I can't go in and access individual objects and then their variables etc as I get cast errors. So I am trying todo this without using sets.

chostwales
  • 89
  • 2
  • 9
  • 4
    Please share some code. What do you have so far, and what errors exactly are you getting? – Mureinik Dec 16 '14 at 22:45
  • 2
    make a new array list, and start inserting elements into that new array. if an element already exists in that array, then don't insert it a second time – Sam I am says Reinstate Monica Dec 16 '14 at 22:47
  • This will probably be closed under "unclear what you are asking" if you do not provide more context and code showing what you are trying to accomplish. – mkobit Dec 16 '14 at 22:53

4 Answers4

1

I assume you are creating a set from the rate sheet and inject in that set the desName of each rate sheet.

Unfortunately, the Set api is based upon the equals method and the hashCode methods, so once you imnplemented the equals and hashCode based upon the desName attribute, your sets will contain unique rate sheet objects regarding the desName, if you want them to be unique regarding the prefix, you have to change the equals method and hashCode method.

I've done something like this in the past by plugging in an adapter inside the rate sheet that either links to the desName or to the prefix, hence the hashCode redirects to the "hashCode" provided by the current adapter.

GerritCap
  • 1,606
  • 10
  • 9
1
  Set<String> s = new HashSet();
  for(int i=0;i<RateSheet.size;i++){
     Rate rate = (Rate)RateSheet.get(i);
     s.add(rate.desName);
    }

Set<Rate> rs = new HashSet();

  for(int i=0;i<RateSheet.size;i++){
     Rate rate = (Rate) RateSheet.get(i);
    Iterator it = s.iterator();
     while(it.hasNext()){
      if(rate.desName.equals(it.next()){
            rs.add(rate);
       }
    }
 }
PeerNet
  • 855
  • 1
  • 16
  • 31
0

Here's an example code on how to do it with a Set. You need to override hashCode() and equals() because they are used under the hood to find out what's a duplicate and what's not:

import java.util.HashSet;

public class RateSheet
{
    private String desName;

    public RateSheet(String name)
    {
        desName = name;
    }

    @Override
    public boolean equals(Object other)
    {
        if (other == null) return false;
        if (other == this) return true;
        if (!(other instanceof RateSheet))return false;
        RateSheet otherAsRateSheet = (RateSheet)other;

        return desName.equals(otherAsRateSheet.desName);
    }

    @Override
    public int hashCode()
    {
        return desName.hashCode();
    }

    public static void main(String[] args)
    {
        HashSet<RateSheet> sheets= new HashSet<RateSheet>();

        RateSheet a = new RateSheet("a");
        RateSheet b = new RateSheet("a");
        RateSheet c = new RateSheet("b");

        System.out.println(sheets.add(a));
        System.out.println(sheets.add(b));
        System.out.println(sheets.add(c));
    }
}

This is based on this other answer found here: Java Set collection - override equals method and also this one: Why is equals() not called while adding to HashSet and hashCode matches? and what @GerritCap posted. (sorry, I am a little slow)

Community
  • 1
  • 1
null
  • 5,207
  • 1
  • 19
  • 35
0
List<RateSheets> dedupSheets(List<RateSheet> sheets) {
    Map<String,RateSheet> map = new HashMap<>();
    for(Rate r: sheets) map.put(r.getName(), r);
    return new ArrayList<>(rateSheets.values());
}
Dima
  • 39,570
  • 6
  • 44
  • 70