如何对包含 Java 中的对象列表的对象列表进行排序?
How to sort the list of object what contains the list that contain also the list of object in Java?
我有“MainClass”的对象列表class,其中包含“问题”的对象列表class,其中包含选项的对象列表class.如何按“Id”对“MainClass”class 列表和按“Id”对“选项”class 进行排序?
public class MainClass {
private Long id;
private List<Question> questions;
}
public class Question {
private Set<Option> options;
}
public class Option{
private Long id;
}
假设,List<MainClass> result = data;
接下来我尝试了:
result.stream().sorted(Comparator.comparing(MainClass::getId)).map(qc->qc.getQuestions()).flatMap(List::stream).map(q->q.getOptions()).flatMap(Set::stream).sorted(Comparator.comparing(Option::getId)).collect(Collectors.toList());
因此,我应该按 MainClass“Id”和“Option”class 按其“Id”
排序 List<MainClass>
class
我写sample/prototype的代码源:
public class MainClass {
private Long id;
private List<Question> questions;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MainClass mainClass = (MainClass) o;
return Objects.equals(id, mainClass.id) && Objects.equals(questions, mainClass.questions);
}
@Override
public int hashCode() {
return Objects.hash(id, questions);
}
}
class Question {
private Set<Option> options;
public Set<Option> getOptions() {
return options;
}
public void setOptions(Set<Option> options) {
this.options = options;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Question question = (Question) o;
return Objects.equals(options, question.options);
}
@Override
public int hashCode() {
return Objects.hash(options);
}
}
class Option{
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Option option = (Option) o;
return Objects.equals(id, option.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
class Test{
public static void main(String[] args) {
Option option = new Option();
option.setId(3L);
Option option2 = new Option();
option2.setId(2L);
Option option3 = new Option();
option3.setId(1L);
Question question = new Question();
question.setOptions(Set.of(option));
Question question2 = new Question();
question2.setOptions(Set.of(option2, option3));
Question question3 = new Question();
question3.setOptions(Set.of(option3));
MainClass mc = new MainClass();
mc.setId(1L);
mc.setQuestions(List.of(question,question3));
MainClass mc2 = new MainClass();
mc2.setId(2L);
mc2.setQuestions(List.of(question,question2));
List<MainClass> list = new LinkedList<>();
list.add(mc);
list.add(mc2);
}
}
以下方法将根据要求进行排序:
public List<MainClass> sort(List<MainClass> list) {
List<MainClass> sorted = list.stream()
.map(t -> {
t.questions.stream().forEach(t1 -> {
t1.options = t1.options.stream().sorted(Comparator.comparing(o -> o.id)).collect(Collectors.toSet());
});
return t;
})
.sorted(Comparator.comparing(o -> o.id))
.collect(Collectors.toList());
return sorted;
}
我有“MainClass”的对象列表class,其中包含“问题”的对象列表class,其中包含选项的对象列表class.如何按“Id”对“MainClass”class 列表和按“Id”对“选项”class 进行排序?
public class MainClass {
private Long id;
private List<Question> questions;
}
public class Question {
private Set<Option> options;
}
public class Option{
private Long id;
}
假设,List<MainClass> result = data;
接下来我尝试了:
result.stream().sorted(Comparator.comparing(MainClass::getId)).map(qc->qc.getQuestions()).flatMap(List::stream).map(q->q.getOptions()).flatMap(Set::stream).sorted(Comparator.comparing(Option::getId)).collect(Collectors.toList());
因此,我应该按 MainClass“Id”和“Option”class 按其“Id”
排序List<MainClass>
class
我写sample/prototype的代码源:
public class MainClass {
private Long id;
private List<Question> questions;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MainClass mainClass = (MainClass) o;
return Objects.equals(id, mainClass.id) && Objects.equals(questions, mainClass.questions);
}
@Override
public int hashCode() {
return Objects.hash(id, questions);
}
}
class Question {
private Set<Option> options;
public Set<Option> getOptions() {
return options;
}
public void setOptions(Set<Option> options) {
this.options = options;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Question question = (Question) o;
return Objects.equals(options, question.options);
}
@Override
public int hashCode() {
return Objects.hash(options);
}
}
class Option{
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Option option = (Option) o;
return Objects.equals(id, option.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
class Test{
public static void main(String[] args) {
Option option = new Option();
option.setId(3L);
Option option2 = new Option();
option2.setId(2L);
Option option3 = new Option();
option3.setId(1L);
Question question = new Question();
question.setOptions(Set.of(option));
Question question2 = new Question();
question2.setOptions(Set.of(option2, option3));
Question question3 = new Question();
question3.setOptions(Set.of(option3));
MainClass mc = new MainClass();
mc.setId(1L);
mc.setQuestions(List.of(question,question3));
MainClass mc2 = new MainClass();
mc2.setId(2L);
mc2.setQuestions(List.of(question,question2));
List<MainClass> list = new LinkedList<>();
list.add(mc);
list.add(mc2);
}
}
以下方法将根据要求进行排序:
public List<MainClass> sort(List<MainClass> list) {
List<MainClass> sorted = list.stream()
.map(t -> {
t.questions.stream().forEach(t1 -> {
t1.options = t1.options.stream().sorted(Comparator.comparing(o -> o.id)).collect(Collectors.toSet());
});
return t;
})
.sorted(Comparator.comparing(o -> o.id))
.collect(Collectors.toList());
return sorted;
}