AiiDee
AiiDee

Reputation: 169

Best approach to eliminate duplicate objects in array

I've a problem and need some help...

My object has a name, longitude and latitude. My problem is, that I've an array which has all the objects inside and now there are (nearly) duplicates.

That means long/lat are just nearly the same but definitely duplicates.

How can I filter them to get a list with unique objects? Here's what I've done so far...

public static Collection<Station> findDuplicates(Collection<Station> stations) {
    Collection<Station> uniqueList = new ArrayList<>();
    for (Station firstStation : stations) {
        Station tempStation = firstStation;
        for (Station secondStation : stations) {
            //Check if distance of the stations is less than 25m then we assume it's the same and we are going to merge the stations
            if ((distanceFrom(firstStation.getLatitude(), firstStation.getLongitude(), secondStation.getLatitude(), secondStation.getLongitude()) < 25)) {
                tempStation = mergeStation(firstStation, secondStation);
            }
        }
    }
    //How to find/add unique stations to uniqueList   
    return uniqueList;
}

Thanks in advance!

Upvotes: 2

Views: 94

Answers (3)

Andy Sug
Andy Sug

Reputation: 244

Modify your Station class as suggested by Zgurskyi. Then you can just do this:

public static Collection<Station> findDuplicates(Collection<Station> stations) {
    Set<Station> unique = new HashSet<>();
    stations.forEach(unique::add);
    return unique;
}

Upvotes: 0

Oleksii Zghurskyi
Oleksii Zghurskyi

Reputation: 4365

Just use Set instead of List as follows:

public static Collection<Station> findDuplicates(Collection<Station> stations) {
    Set<Station> uniqueList = new HashSet<>();
    // rest of your code
}

However, for this solution to work properly it's important to override equals and hashCode for Station. Something like this:

    public class Station {
        private long latitude;
        private long longitude;

        Station(long latitude, long longitude) {
            this.latitude = latitude;
            this.longitude = longitude;
        }

        long getLatitude() {
            return latitude;
        }

        long getLongitude() {
            return longitude;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            Station station = (Station) o;
            return latitude == station.latitude &&
                longitude == station.longitude;
        }

        @Override
        public int hashCode() {
            return Objects.hash(latitude, longitude);
        }
    }

Upvotes: 3

vhr
vhr

Reputation: 1664

public static Collection<Station> findDuplicates(Collection<Station> stations) {
    Collection<Station> uniqueList = new ArrayList<>();
    uniqueList.Add(stations[0]); 
    for (Station station : stations) {
        //check if uniqueList contains station, if not add it to the uniqueList         
    }    
    return uniqueList;
}

Use this feature of java to find collection elements: Java streams

Or iterate the uniqueList and use overriden Equals in your Station class @Override public boolean equals

Upvotes: 0

Related Questions