Hashed sometimes deceives me

I need to clarify whether my approach is right or wrong regarding any necessary changes.
Let me explain. I will have an Excel file that will indicate the name of the country of the country (mm / yyyy) as an additional 10 columns

countrycode country Name    12/2000 11/2000 10/2000 09/2000 08/2000 07/2000 06/2000 05/2000 04/2000 03/2000 02/2000 01/2000
     IND    India           10.1    10.2    10.3    10.4    10.5    10.6    10.7    10.8    10.9    11.1    11.2    11.3
     USA    Uinted States   8.1     8.2     8.3      8.4    8.5     8.6     8.7     8.8      8.9     9.1    9.2      9.3

In the line, if any price is repeated for a given year and country, I need to show the message as a Duplicate present in the Excel file.

For the above, I am implemented in this way. For VO, I override hashCode()using hashcode (coutrycode + year + price) and equalsalso when pasting into the database, I pass this VO to HashSet, and I delete the duplicate and compare the size of the original size of the list with the size HashSet.

But someday, if there is a unique price, I get the message as a duplicate.

Please suggest me that my approach be right or wrong or in another way that I can implement.

+4
source share
4 answers

Buddy, that you have taken the right idea and approach the solution of the problem, but there is simply not enough small edge (information) to solve the problem correctly.

I would like to give a little hint, which, I believe, can help and fix the problem and understand the basics really very well.

If you look at the documentation (or source code) hashCodefor Stringand variables Double, it states

LINE

- . - String  s [0] * 31 ^ (n-1) + s [1] * 31 ^ (n-2) +... + s [n-1] int , s [i] - i- , n - , ^ . (- .)
: - .

DOUBLE

- . , , doubleToLongBits(double), , . , - :
      (int)(v^(v>>>32))
v : long v = Double.doubleToLongBits(this.doubleValue());
: - .

, hashCode() , , int .

, .

, HashMap<Integer,List<String>>, Integer hashCode , , List<String> - , String coutrycode + + .

, List<String> hashCode() , List String.

+4

- hashcode() equals(), . , , , , , .

. VO, , , , , VO -.

hashcode() equals() , , ( eclipse, @EqualsAndHashcode Lombok, " Java" , .).

- .

:

public void doit(List<VO> vos) {
    Set<VOWrapper> dups = new HashSet<>();
    for (VO vo : vos) {
        if (dups.contains(new VOWrapper(vo))) {
            System.out.println("Found a duplicate");
        } else {
            dups.add(new VOWrapper(vo));
            // Process vo
        }
    }
}

VO

@Data // Lombok generates getters/setters/equals/hashcode (using all fields)
public class VO {
    private String countrycode;
    private String country;
    private int month;
    private int year;
    private double price;
}

public class VOWrapper {
private final VO vo;

public VOWrapper(VO vo) { this.vo = vo; }

// Equals method with only 3 fields used
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    VO other = ((VOWrapper) obj).vo;
    if (vo.getCountry() == null) {
        if (other.getCountry() != null)
            return false;
    } else if (!vo.getCountry().equals(other.getCountry()))
        return false;
    if (vo.getCountrycode() == null) {
        if (other.getCountrycode() != null)
            return false;
    } else if (!vo.getCountrycode().equals(other.getCountrycode()))
        return false;
    if (Double.doubleToLongBits(vo.getPrice()) != Double.doubleToLongBits(other.getPrice()))
        return false;
    return true;
}

//Hashcode method with only 3 fields used
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((vo.getCountry() == null) ? 0 : vo.getCountry().hashCode());
    result = prime * result + ((vo.getCountrycode() == null) ? 0 : vo.getCountrycode().hashCode());
    long temp;
    temp = Double.doubleToLongBits(vo.getPrice());
    result = prime * result + (int) (temp ^ (temp >>> 32));
    return result;
}
}
0

, :

List<CountryInstance> list = ...;
Set<CountryInstance> set = new HashSet<CountryInstance>(list);
if(set.size() < list.size()){
    /* There are duplicates */

. , -. , hashCode Java?

0

, .

. , , , . , "equals" , , . "hashCode" , , ( ).

If you are still experiencing problems, attach the implementation of "hashCode" and "equals", and this can help quickly answer the problem.

One more thing. I assume all countries in the sample are unique in the file? I mean that no countries are duplicated later in the file?

0
source

All Articles