пример двух решений:
- forEach Lambda - Java 8 или выше
- classic
for
loops
Рекомендации на будущее:
а.не используйте переменные класса напрямую - вместо этого создайте пары getts / setters в соответствии со спецификацией Java Bean.Затем используйте их: например, вместо tutors.get(i).lastName
используйте tutors.get(i).getLastName()
b.при записи в цикл for
используйте скрученные скобки, даже если есть только одна строка - это предотвращает случайные опечатки в теле цикла.например,
for (int i = 0; i < students.size(); i++) {
mstudents.put(students.get(i).getFirstName() + " " + students.get(i).getLastName(), students.get(i).getSubjects());
}
вместо
for (int i = 0; i < students.size(); i++)
mstudents.put(students.get(i).firstName + " " + students.get(i).lastName, students.get(i).subjects);
=====================
package org.tempuri.tests;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
public class SimpleTest {
public static void main(String[] args) {
// Just for test - inbound data
Tutor mathTutor = new Tutor("Peter", "Parker", "MATH");
Tutor physicsTutor = new Tutor("Tonny", "Stark", "PHYSICS");
Tutor computersTutor = new Tutor("Steve", "Rogers", "COMPUTERS");
Student freshmen = new Student("Bill", "Gates", Arrays.asList("MATH", "COMPUTERS"));
Student sophomore = new Student("Peter", "Quill", Arrays.asList("MATH", "COMPUTERS", "PHYSICS"));
Student senior = new Student("Peggy", "Carter", Arrays.asList("MATH", "PHYSICS"));
List<Tutor> tutors = Arrays.asList(mathTutor, physicsTutor, computersTutor);
List<Student> students = Arrays.asList(freshmen, sophomore, senior);
// Untouched question code (almost ...)
HashMap<String, String> mtutors = new HashMap<>();
HashMap<String, List<String>> mstudents = new HashMap<>();
for (int i = 0; i < tutors.size(); i++)
mtutors.put(tutors.get(i).firstName + " " + tutors.get(i).lastName, tutors.get(i).subject);
for (int i = 0; i < students.size(); i++)
mstudents.put(students.get(i).firstName + " " + students.get(i).lastName, students.get(i).subjects);
// variation 1. using forEach Lambda (> Java 8)
List<MatchingPair> matchingPairs1 = new LinkedList<>();
mtutors.entrySet().forEach(tutorEntry -> {
mstudents.entrySet().forEach(studentEntry -> {
if (studentEntry.getValue().contains(tutorEntry.getValue()))
matchingPairs1
.add(new MatchingPair(tutorEntry.getKey(), studentEntry.getKey(), tutorEntry.getValue()));
});
});
System.out.println("result variation1: ");
System.out.println("------------------ ");
matchingPairs1.forEach(onePair -> System.out.println("Tutor: " + onePair.getTutorName() + " -> Student: "
+ onePair.getStudentName() + " -> Subject: " + onePair.getSubject()));
// variation 2. using classical for loops (< Java 7)
final List<MatchingPair> matchingPairs2 = new LinkedList<>();
for (Entry<String, String> oneTutor : mtutors.entrySet()) {
for (Entry<String, List<String>> oneStudent : mstudents.entrySet()) {
if (oneStudent.getValue().contains(oneTutor.getValue())) {
matchingPairs2.add(new MatchingPair(oneTutor.getKey(), oneStudent.getKey(), oneTutor.getValue()));
}
}
}
System.out.println("------------------ ");
System.out.println();
System.out.println("result variation2: ");
System.out.println("------------------ ");
for (MatchingPair onePair : matchingPairs2) {
System.out.println("Tutor: " + onePair.getTutorName() + " -> Student: " + onePair.getStudentName() + " -> Subject: "
+ onePair.getSubject());
}
System.out.println("------------------ ");
}
// supporting classes for test
private static class MatchingPair {
private String tutorName;
private String studentName;
private String subject;
MatchingPair(String tutorName, String studentName, String subject) {
this.tutorName = tutorName;
this.studentName = studentName;
this.subject = subject;
}
public String getTutorName() {
return tutorName;
}
public void setTutorName(String tutorName) {
this.tutorName = tutorName;
}
}
private static class Tutor {
String firstName;
String lastName;
String subject;
Tutor(String firstName, String lastName, String subject) {
this.firstName = firstName;
this.lastName = lastName;
this.subject = subject;
}
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 String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
private static class Student {
String firstName;
String lastName;
List<String> subjects;
Student(String firstName, String lastName, List<String> subjects) {
this.firstName = firstName;
this.lastName = lastName;
this.subjects = subjects;
}
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 List<String> getSubjects() {
return subjects;
}
public void setSubjects(List<String> subjects) {
this.subjects = subjects;
}
}
}