如果我有 ArrayList,如何比较两个 HashMap?

我不能继续我的HashMap作业了。你能帮助我吗?


我有 2 HashMap:mtutors和mstudents.


mtutors关键:他们的名字。

mtutors价值:一个主题。

mstudents关键:他们的名字。

mstudents值:主题(ArrayList)


我必须找到匹配的导师-学生。例如: mtutors 键之一:Peter Parker。mtutors 的价值之一:数学。学生关键之一:比尔·盖茨。学生的价值之一:数学,IT。配对:彼得帕克和比尔盖茨 - 数学。


HashMap<String,String> mtutors = new HashMap<>();

HashMap<String,ArrayList> 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).students);


扬帆大鱼
浏览 181回答 3
3回答

牧羊人nacy

如果您使用的是 Java8 或更高版本,请尝试以下操作:mtutors.forEach((tutorName, subject) -> {&nbsp; &nbsp; &nbsp; &nbsp; mstudents.forEach((studentName, subjects) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (subjects.contains(subject)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(tutorName + ":" + studentName + ":" + subject);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });我假设您将科目(不是学生)放到学生地图上。仅获得第一次拟合的数据流示例。mtutors.forEach((tutorName, subject) -> {&nbsp; &nbsp; &nbsp; &nbsp; mstudents.entrySet().stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(entry -> entry.getValue().contains(subject))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ifPresent(entry -> System.out.println(tutorName + ":" + entry.getKey() + ":" + subject));&nbsp; &nbsp; });

隔江千里

我的回答类似于@ŁukaszKucik 的回答,只是使用 Java 流。您可能不应该将我的解决方案用于您的作业,因为我想这不是您应该解决的方法。不过,只是为了多样化:List<String>&nbsp;matches&nbsp;=&nbsp;mtutors.entrySet() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(tutor&nbsp;->&nbsp;mstudents.entrySet() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter(student&nbsp;->&nbsp;student.getValue() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.contains(tutor.getValue())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(student&nbsp;->&nbsp;tutor.getKey()&nbsp;+&nbsp;"&nbsp;and&nbsp;"&nbsp;+&nbsp;student.getKey()&nbsp;+&nbsp;"&nbsp;-&nbsp;"&nbsp;+&nbsp;tutor.getValue())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList());如果缺少的部分是list.contains():https ://www.tutorialspoint.com/java/util/arraylist_contains.htm如果您缺少的部分是如何解决这种组合,那么仅仅在这里寻求解决方案并不是学习和家庭作业的工作方式。

富国沪深

两个解决方案示例:forEach Lambda - Java 8 或更高版本经典for循环对未来的建议:一个。不要直接使用类变量——而是根据 Java Bean 规范创建 getter/setter 对。然后使用它们:例如,改为tutors.get(i).lastName使用tutors.get(i).getLastName()湾。for即使只有一行,在编写循环时也要使用大括号 - 它可以防止循环体出现意外的拼写错误。例如for (int i = 0; i < students.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; mstudents.put(students.get(i).getFirstName() + " " + students.get(i).getLastName(), students.get(i).getSubjects());&nbsp; &nbsp; }代替for (int i = 0; i < students.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; 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 {&nbsp; public static void main(String[] args) {&nbsp; &nbsp; // Just for test - inbound data&nbsp; &nbsp; Tutor mathTutor = new Tutor("Peter", "Parker", "MATH");&nbsp; &nbsp; Tutor physicsTutor = new Tutor("Tonny", "Stark", "PHYSICS");&nbsp; &nbsp; Tutor computersTutor = new Tutor("Steve", "Rogers", "COMPUTERS");&nbsp; &nbsp; Student freshmen = new Student("Bill", "Gates", Arrays.asList("MATH", "COMPUTERS"));&nbsp; &nbsp; Student sophomore = new Student("Peter", "Quill", Arrays.asList("MATH", "COMPUTERS", "PHYSICS"));&nbsp; &nbsp; Student senior = new Student("Peggy", "Carter", Arrays.asList("MATH", "PHYSICS"));&nbsp; &nbsp; List<Tutor> tutors = Arrays.asList(mathTutor, physicsTutor, computersTutor);&nbsp; &nbsp; List<Student> students = Arrays.asList(freshmen, sophomore, senior);&nbsp; &nbsp; // Untouched question code (almost ...)&nbsp; &nbsp; HashMap<String, String> mtutors = new HashMap<>();&nbsp; &nbsp; HashMap<String, List<String>> mstudents = new HashMap<>();&nbsp; &nbsp; for (int i = 0; i < tutors.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; mtutors.put(tutors.get(i).firstName + " " + tutors.get(i).lastName, tutors.get(i).subject);&nbsp; &nbsp; for (int i = 0; i < students.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; mstudents.put(students.get(i).firstName + " " + students.get(i).lastName, students.get(i).subjects);&nbsp; &nbsp; // variation 1. using forEach Lambda (> Java 8)&nbsp; &nbsp; List<MatchingPair> matchingPairs1 = new LinkedList<>();&nbsp; &nbsp; mtutors.entrySet().forEach(tutorEntry -> {&nbsp; &nbsp; &nbsp; &nbsp; mstudents.entrySet().forEach(studentEntry -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (studentEntry.getValue().contains(tutorEntry.getValue()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matchingPairs1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(new MatchingPair(tutorEntry.getKey(), studentEntry.getKey(), tutorEntry.getValue()));&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; System.out.println("result variation1: ");&nbsp; &nbsp; System.out.println("------------------ ");&nbsp; &nbsp; matchingPairs1.forEach(onePair -> System.out.println("Tutor: " + onePair.getTutorName() + " -> Student: "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + onePair.getStudentName() + " -> Subject: " + onePair.getSubject()));&nbsp; &nbsp; // variation 2. using classical for loops (< Java 7)&nbsp; &nbsp; final List<MatchingPair> matchingPairs2 = new LinkedList<>();&nbsp; &nbsp; for (Entry<String, String> oneTutor : mtutors.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; for (Entry<String, List<String>> oneStudent : mstudents.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (oneStudent.getValue().contains(oneTutor.getValue())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; matchingPairs2.add(new MatchingPair(oneTutor.getKey(), oneStudent.getKey(), oneTutor.getValue()));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("------------------ ");&nbsp; &nbsp; System.out.println();&nbsp; &nbsp; System.out.println("result variation2: ");&nbsp; &nbsp; System.out.println("------------------ ");&nbsp; &nbsp; for (MatchingPair onePair : matchingPairs2) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Tutor: " + onePair.getTutorName() + " -> Student: " + onePair.getStudentName() + " -> Subject: "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + onePair.getSubject());&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println("------------------ ");}&nbsp; &nbsp; // supporting classes for testprivate static class MatchingPair {&nbsp; &nbsp; private String tutorName;&nbsp; &nbsp; private String studentName;&nbsp; &nbsp; private String subject;&nbsp; &nbsp; MatchingPair(String tutorName, String studentName, String subject) {&nbsp; &nbsp; &nbsp; &nbsp; this.tutorName = tutorName;&nbsp; &nbsp; &nbsp; &nbsp; this.studentName = studentName;&nbsp; &nbsp; &nbsp; &nbsp; this.subject = subject;&nbsp; &nbsp; }&nbsp; &nbsp; public String getTutorName() {&nbsp; &nbsp; &nbsp; &nbsp; return tutorName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setTutorName(String tutorName) {&nbsp; &nbsp; &nbsp; &nbsp; this.tutorName = tutorName;&nbsp; &nbsp; }}private static class Tutor {&nbsp; &nbsp; String firstName;&nbsp; &nbsp; String lastName;&nbsp; &nbsp; String subject;&nbsp; &nbsp; Tutor(String firstName, String lastName, String subject) {&nbsp; &nbsp; &nbsp; &nbsp; this.firstName = firstName;&nbsp; &nbsp; &nbsp; &nbsp; this.lastName = lastName;&nbsp; &nbsp; &nbsp; &nbsp; this.subject = subject;&nbsp; &nbsp; }&nbsp; &nbsp; public String getFirstName() {&nbsp; &nbsp; &nbsp; &nbsp; return firstName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setFirstName(String firstName) {&nbsp; &nbsp; &nbsp; &nbsp; this.firstName = firstName;&nbsp; &nbsp; }&nbsp; &nbsp; public String getLastName() {&nbsp; &nbsp; &nbsp; &nbsp; return lastName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setLastName(String lastName) {&nbsp; &nbsp; &nbsp; &nbsp; this.lastName = lastName;&nbsp; &nbsp; }&nbsp; &nbsp; public String getSubject() {&nbsp; &nbsp; &nbsp; &nbsp; return subject;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSubject(String subject) {&nbsp; &nbsp; &nbsp; &nbsp; this.subject = subject;&nbsp; &nbsp; }}private static class Student {&nbsp; &nbsp; String firstName;&nbsp; &nbsp; String lastName;&nbsp; &nbsp; List<String> subjects;&nbsp; &nbsp; Student(String firstName, String lastName, List<String> subjects) {&nbsp; &nbsp; &nbsp; &nbsp; this.firstName = firstName;&nbsp; &nbsp; &nbsp; &nbsp; this.lastName = lastName;&nbsp; &nbsp; &nbsp; &nbsp; this.subjects = subjects;&nbsp; &nbsp; }&nbsp; &nbsp; public String getFirstName() {&nbsp; &nbsp; &nbsp; &nbsp; return firstName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setFirstName(String firstName) {&nbsp; &nbsp; &nbsp; &nbsp; this.firstName = firstName;&nbsp; &nbsp; }&nbsp; &nbsp; public String getLastName() {&nbsp; &nbsp; &nbsp; &nbsp; return lastName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setLastName(String lastName) {&nbsp; &nbsp; &nbsp; &nbsp; this.lastName = lastName;&nbsp; &nbsp; }&nbsp; &nbsp; public List<String> getSubjects() {&nbsp; &nbsp; &nbsp; &nbsp; return subjects;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSubjects(List<String> subjects) {&nbsp; &nbsp; &nbsp; &nbsp; this.subjects = subjects;&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java