-2

I have two types of user, LibraUsers and GenesysUser. Each have their own properties, but some are similar.

I need to find the differences between two lists of these types of users. Made a small program to illustrate my problem - My issue is that I have duplicate entries in my difference list, the compare method on the list does not seem to do its job. I've read that I should override the equals method, and also the hashcode (in this case of the LibraUser). I tried that using an apache function, but it did not work. The question therefore is, what are the alternatives for solving this problem?

import java.util.ArrayList;
import java.util.List;

// one class needs to have a main() method
public class HelloWorld {
    public static List<LibraUser> libraUsers = new ArrayList<LibraUser>();
    public static List<GenesysUser> genesysUsers = new ArrayList<GenesysUser>();
    public static List<LibraUser> difference = new ArrayList<LibraUser>();

    public static void initialiseGenesysUsers() {
        // GenesysUser(String alias, String firstname, String lastname, String email)
        genesysUsers.add(new GenesysUser("Donna", "Donna", "Paulsen", "donna@gmail.com", true));
        genesysUsers.add(new GenesysUser("TheHarv", "Harvey", "Specter", "harvey@gmail.com", true));
        genesysUsers.add(new GenesysUser("Rache", "Rachel", "Zane", "rachel@gmail.com", true));
        genesysUsers.add(new GenesysUser("Mike", "Mike", "Ross", "mike@gmail.com", true));
    }

    public static void initialiseLibraUsers() {
        // LibraUser(String name, String email, String telephone) {
        libraUsers.add(new LibraUser("Louis", "louis@gmail.com", "0447521082"));
        libraUsers.add(new LibraUser("Jessica", "jessica@gmail.com", "0447521044"));
        libraUsers.add(new LibraUser("Mike", "mike@gmail.com", ""));
    }

    public static void getDifference() {
        for (LibraUser librauser : libraUsers) {
            for (GenesysUser genesysuser : genesysUsers) {
                String genusername = genesysuser.getFirstname();
                LibraUser dummy = new LibraUser(genusername, genesysuser.getEmail(), "");
                if (!difference.contains(dummy)) {
                    // do the actual comparison using the relevant keys and insert into a new list
                    if (!librauser.getUsername().equals(genusername)) {
                        difference.add(dummy);
                    }
                }
            } // inner for loop
        }// outer for loop
    }

    public static void printDifference() {
        for (LibraUser usr : difference) {
            System.out.println(usr.getUsername());
        }
    }

    // arguments are passed using the text field below this editor
    public static void main(String[] args) {
        initialiseGenesysUsers();
        initialiseLibraUsers();

        getDifference();
        printDifference();
    }
}

public class GenesysUser {
    private String Alias;
    private String Firstname;
    private String Lastname;
    private String Email;
    private boolean Active;

    public GenesysUser(String alias, String firstname, String lastname, String email, boolean active) {
        Alias = alias;
        Firstname = firstname;
        Lastname = lastname;
        Email = email;
        Active = active;
    }

    public String getAlias() {
        return Alias;
    }

    public String getFirstname() {
        return Firstname;
    }

    public String getLastname() {
        return Lastname;
    }

    public String getEmail() {
        return Email;
    }

    public void setAlias(String alias) {
        Alias = alias;
    }

    public void setFirstname(String firstname) {
        Firstname = firstname;
    }

    public void setLastname(String lastname) {
        Lastname = lastname;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public void setActive(boolean active) {
        Active = active;
    }

    public boolean getActive() {
        return Active;
    }
}

public class LibraUser {
    private String username;
    private String email;
    private String telephone;

    public LibraUser(String username, String email, String telephone) {
        this.username = username;
        this.email = email;
        this.telephone = telephone;
    }

    public String getUsername() {
        return this.username;
    }

    public String getEmail() {
        return this.email;
    }

    public String getTelephone() {
        return this.telephone;
    }

    public void setName(String username) {
        this.username = username;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this)
            return true;
        if (!(o instanceof LibraUser)) {
            return false;
        }

        LibraUser user = (LibraUser) o;

        return new EqualsBuilder()
            .append(username, user.getUsername())
            .append(email, user.getEmail())
            .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
            .append(username)
            .append(email)
            .toHashCode();
    }
}
Tom
  • 14,120
  • 16
  • 41
  • 47
Harriet
  • 1,303
  • 5
  • 19
  • 31
  • http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java .... http://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java – Tom May 08 '17 at 08:43
  • admittedly that was a mistake, the bigger question still stands... – Harriet May 08 '17 at 08:50
  • I copy and pasted from my actual code to my dummy code to provide more information. User should be LibraUser. – Harriet May 08 '17 at 08:58
  • I just tried your way - it works except when I add the same user to both lists (genesys and libra) then it does not pick up a duplicate.Example when I add this line: libraUsers.add(new LibraUser("Mike", "mike@gmail.com", "")); – Harriet May 08 '17 at 09:36
  • Ok, now we're talking. Notice that I took the liberty to add this line to your code, since it is quite important. Your issue is the `getDifference` method the both `for` loops. You first take the first entry from `libraUsers` and compare it with every entry in `genesysUsers`. No what happens? Is Donna that first entry (in libraUsers I mean)? No -> add to difference. Is Harvey that first entry? No -> add. Or Rachel or Mike? No for both. You check the entry "Mike" from "libraUsers" as the third iteration of the outer loop, but you've added "Mike" from "genesysUsers" already to differences. – Tom May 08 '17 at 11:08
  • So I've pointed you to the issue in that algo, now take same time and try to find a better solution, you'll learn a lot from that experience. As a hint: try to use only one `for` (and only that loop, not an additional "while" loop). – Tom May 08 '17 at 11:10
  • 1
    hey Tom, long story short - I made a mockup to ask the question here, and I got the mockup working. However when I do the same in my working project code, it does not work. I then decided to take out the place where I call "contains" on the list, and write my own code that basically iterates through both lists. This code then works as I expect. There may be some weird relationship between classes causing the equals method not to the called in my working project case. Thanks for your effort though. – Harriet May 08 '17 at 13:03

1 Answers1

0

You need to use .equals instead of == to test the equality

if(librauser.getUsername() != genusername)

should be replaced with

if(!librauser.getUsername().equqls(genusername))
murthy
  • 162
  • 4
  • 11
  • hi @murthy, thanks for your answer - i have replaced the '==' with equals, and put a not sign at the next condition. I still get duplicates in my differences list though... – Harriet May 08 '17 at 08:47
  • Can you post that code here of the condition you have made – murthy May 08 '17 at 08:50
  • sure - please check the original question, i've added the two override methods. – Harriet May 08 '17 at 08:52