Filtrer un TreeSet selon plusieurs critères (différents)
Bonjour !
Je me remets dans le monde du Java depuis quelques jours (après des années d’inactivité en programmation :D), et je suis visiblement rouillé.
J’ai un TreeSet peuplé d’éléments que j’aimerais filtrer selon plusieurs critères. Je sais filtrer via filter().collect(), mais pour un seul élément…
Je m’explique un peu mieux :
J’ai la classe Person
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class Person implements Comparable<Person> {
private String firstName;
private String lastName;
private LocalDate birthday;
private Smtg something;
public Person(String firstName, String lastName, LocalDate birthday, Smtg something) {
this.firstName = firstName;
this.lastName = lastName;
this.birthday = birthday;
this.something = something;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
public void setSomething(Smtg something) {
this.something = something;
}
public Smtg getSomething() {
return something;
}
public Category getCategory() {
var age = ChronoUnit.YEARS.between(birthday, LocalDate.now());
if (age >= 0 && age < 2) return Category.BABY;
else if (age >= 2 && age < 12) return Category.CHILD;
else if (age >= 12 && age < 18) return Category.TEEN;
else if (age >= 18 && age < 65) return Category.ADULT;
else if (age >= 65) return Category.SENIOR;
else return Category.UNKNOWN;
}
@Override
public int compareTo(Person o) {
if (this.firstName.equalsIgnoreCase(o.firstName)) return this.lastName.compareTo(o.lastName);
else return this.firstName.compareTo(o.firstName);
}
@Override
public String toString() {
return firstName + " " + lastName + " " + getCategory().toString() + " " + something.toString();
}
public enum Category {
BABY,
CHILD,
TEEN,
ADULT,
SENIOR,
UNKNOWN
}
public enum Smtg {
SOMETHING_1,
SOMETHING_2,
SOMETHING_3
}
} |
et une MainApp pour faire de la console:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
import java.time.LocalDate;
import java.util.SortedSet;
import java.util.TreeSet;
public class TestApp {
private TreeSet<Person> people = new TreeSet<>();
public static void main(String[] args) {
var app = new TestApp();
app.people.add(new Person("Lemon", "Ray", LocalDate.of(1981, 8, 12), Person.Smtg.SOMETHING_1));
app.people.add(new Person("Onyme", "Anne", LocalDate.of(1980, 7, 20), Person.Smtg.SOMETHING_1));
app.people.add(new Person("Lemoche", "Rocky", LocalDate.of(2007, 4, 1), Person.Smtg.SOMETHING_2));
app.people.add(new Person("Fonfec", "Sophie", LocalDate.of(2017, 4, 12), Person.Smtg.SOMETHING_3));
app.people.forEach(System.out::println);
}
private SortedSet<Person> filter(Object... tags) {
var data = new TreeSet<>(people);
if (tags == null) return data;
//TODO
}
}
|
J’aimerais pouvoir filtrer mes personnes soit sur le nom/prénom, soit les personnes nées en 2007, soit les personnes adultes,etc…
Soit j’aimerais récupérer une liste des personnes ADULT et SOMETHING_1 dont le nom contiendrait un R…
Dans l’idéal, j’aimerais associer ça à un Observable pour pouvoir faire des recherches "temps réel". En JavaFX, il existe une méthode, mais j’aimerais pouvoir appliquer ça en console…
Quelqu’un peut éclairer ma lanterne ?
Merci !