Created
December 4, 2013 11:33
-
-
Save kmb385/7786170 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Map.Entry; | |
public class Student { | |
private String name; | |
private String major; | |
private int number; | |
public Student(String name, String major, int number) { | |
super(); | |
this.name = name; | |
this.major = major; | |
this.number = number; | |
} | |
public static void main(String[] args) { | |
List<String> names = Arrays.asList("Joe", "Jack", "John","James"); | |
List<String> majors = Arrays.asList("English","Math","Geography"); | |
Map<String,Student> students = new HashMap<String,Student>(); | |
for(int i = 0; i < 100; i++){ | |
Collections.shuffle(names); | |
Collections.shuffle(majors); | |
students.put(names.get(0) + String.valueOf(i), new Student(names.get(0), majors.get(0), i)); | |
} | |
List<Student> mathMajors = getStudents(students, "Math"); | |
for(Student student:mathMajors){ | |
System.out.println(student.name); | |
System.out.println(student.major); | |
} | |
} | |
public static List<Student> getStudents(Map<String,Student> students, String searchToken){ | |
List<Student> results = new ArrayList<Student>(); | |
for(Entry<String,Student> entry:students.entrySet()){ | |
if(entry.getValue().getMajor().equalsIgnoreCase(searchToken)){ | |
results.add(entry.getValue()); | |
} | |
} | |
return results; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getMajor() { | |
return this.major; | |
} | |
public void setMajor(String major) { | |
this.major = major; | |
} | |
public int getNumber() { | |
return this.number; | |
} | |
public void setNumber(int number) { | |
this.number = number; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment