Reputation: 1579
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
my goal expectation result
how to reach my expectation? and any method elegantly to resolve this?
Upvotes: 0
Views: 587
Reputation: 4948
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
Upvotes: 0
Reputation: 424
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.
Upvotes: 1
Reputation: 140427
I see two options:
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.
Upvotes: 1