0

i have array list object, but i want to filter remove data if any duplicate id in one day.

this is my code

public static void main(String[] args) {

        ArrayList<UniqueCompletedChat> al = new ArrayList<UniqueCompletedChat>();
        al.add(new UniqueCompletedChat("2015-11-01", "D01"));
        al.add(new UniqueCompletedChat("2015-11-01", "D01"));
        al.add(new UniqueCompletedChat("2015-11-01", "D02"));
        al.add(new UniqueCompletedChat("2015-11-01", "D01"));
        al.add(new UniqueCompletedChat("2015-11-02", "D01"));
        al.add(new UniqueCompletedChat("2015-11-02", "D02"));
        al.add(new UniqueCompletedChat("2015-11-02", "D03"));
        al.add(new UniqueCompletedChat("2015-11-02", "D02"));
        al.add(new UniqueCompletedChat("2015-11-02", "D02"));
        al.add(new UniqueCompletedChat("2015-11-03", "D01"));

        List<UniqueCompletedChat> result = new ArrayList<UniqueCompletedChat>();
        Set<String> titles = new HashSet<String>();

        for (UniqueCompletedChat u : al) {
            if (titles.add(u.getIdDoctor()) || titles.add(u.getDate())) {
                result.add(u);
            }
        }

        for(UniqueCompletedChat u : result){
            System.out.print(u.getDate() + " || ");
            System.out.print(u.getIdDoctor());
            System.out.println("");
        }
}

class UniqueCompletedChat {
    private String date;
    private String idDoctor;

    public UniqueCompletedChat(String date, String idDoctor) {
        this.date = date;
        this.idDoctor = idDoctor;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getIdDoctor() {
        return idDoctor;
    }

    public void setIdDoctor(String idDoctor) {
        this.idDoctor = idDoctor;
    }
}

this is output from my code

enter image description here

my goal expectation result

enter image description here

how to reach my expectation? and any method elegantly to resolve this?

3 Answers 3

1

I think the bug come from this LOC:

if (titles.add(u.getIdDoctor()) || titles.add(u.getDate())) {
    result.add(u);
}

Try to combine and check it like this:

if (titles.add(u.getIdDoctor() + u.getDate())) {
    result.add(u);
}

Of course, you can use StringBuilder for better performance.

Sign up to request clarification or add additional context in comments.

Comments

1

I see two options:

  1. Doing it by hand (looping your array and figuring duplicates)
  2. Using a Set, and adding equals(), hashcode() methods to your class UniqueCompletedChat that simply makes two chats that have the same date and doctor ... the same chat.

Basically you have to understand your "model" that guides your design. And, if in your model such two "chats" are "the same" when they happen on the same day, with the same doctor, then your implementation should reflect that.

And, talking about good design: don't use low-level abstractions. A date, is ... a date, not a String. Probably Java has to many different ways to deal with dates, calendars, time. But well: they are there. Pick one of those. Instead of moving around Strings. Same for "DoctorID": create a class that represents that thing.

Thing is: pushing around Strings might look convenient and easy to do; but it is the opposite of a good OO design. You know, if you don't care about using Javas type system; you could as well step back and do everything in a dynamic language instead of Java.

1 Comment

Java has only two ways to deal with date-time handling: (a) the troublesome, confusing, and flawed old way (java.util.Date, java.util.Calendar, java.text.SimpleDateFormat, etc.), and (b) the modern clean powerful way in the java.time classes. Avoid any date-time class not in the java.time package.
0

If you are on java 8

Collection<UniqueCompletedChat> result = al.stream()
                .<Map<String, UniqueCompletedChat>>collect(HashMap::new, (k, uc) -> k.put(uc.getKey(), uc), Map::putAll)

Just adding one method on your Chat class as :

public String getKey() {
    return this.idDoctor + this.date;
}

prints :

2015-11-02 || D02
2015-11-01 || D02
2015-11-02 || D03
2015-11-03 || D01
2015-11-02 || D01
2015-11-01 || D01

1 Comment

if using java 7 how to implement it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.