Skip to content

Instantly share code, notes, and snippets.

@btforsythe
Created June 1, 2017 18:51
Show Gist options
  • Save btforsythe/bda98fcc3fe469705aaf2cac296f8d63 to your computer and use it in GitHub Desktop.
Save btforsythe/bda98fcc3fe469705aaf2cac296f8d63 to your computer and use it in GitHub Desktop.
Student Records with and without Maps
package org.wecancodeit.objects.maps;
import java.util.ArrayList;
import java.util.List;
import org.wecancodeit.objects.students.Student;
public class StudentRecords {
public static void main(String[] args) {
List<Student> students = new ArrayList<>(); // same as
// ArrayList<Student>();
Student harvey = new Student("23A52", "Harvey Dent");
students.add(harvey);
students.add(new Student("68Z29", "Jessica Jones"));
Student jj = new Student("57W85", "J Jonah Jameson");
students.add(jj);
System.out.println(jj);
System.out.println(students);
String lookingFor = "Harvey Dent";
for(Student current: students) {
System.out.println("Current: " + current);
// same as if(current.name.equalsIgnoreCase(lookingFor)) {
if(lookingFor.equalsIgnoreCase(current.name)) {
System.out.println("Found my student. His id is " + current.id);
break;
}
}
}
}
package org.wecancodeit.objects.maps;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class StudentRecordsWithMap {
public static void main(String[] args) {
// Student harvey = new Student("23A52", "Harvey Dent");
// Student jessica = new Student("68Z29", "Jessica Jones");
// Student jj = new Student("57W85", "J Jonah Jameson");
Map<String, String> students = new HashMap<String, String>();
students.put("68Z29", "Jessica Jones");
System.out.println("hashcode for 68Z29 is " + "68Z29".hashCode());
System.out.println(students);
students.put("23A52", "Harvey Dent");
System.out.println("hashcode for 23A52 is " + "23A52".hashCode());
System.out.println(students);
String studentName = students.get("23A52");
System.out.println("I found student " + studentName);
// common map methods
// collection views
System.out.println("Student names (map values) are " + students.values());
System.out.println("Student ids (map keys) are " + students.keySet());
// use entrySet() when you need both key and value
System.out.println("Student entries (key/value pairs) are " + students.entrySet());
System.out.println("Iterating over student names:");
for(String currentName: students.values()) {
System.out.println(currentName);
}
System.out.println("Iterating over entries:");
for(Entry<String, String> entry: students.entrySet()) {
String key = entry.getKey(); // student id
String value = entry.getValue(); // student name
System.out.println("key is " + key + ", value is " + value);
}
// sorting a list (sidebar)
System.out.println("Sorting a list:");
List<String> listOfClasses = new ArrayList<String>();
listOfClasses.add("Philosophy");
listOfClasses.add("Creative Writing");
System.out.println(listOfClasses);
Collections.sort(listOfClasses);
System.out.println(listOfClasses);
// is this a valid student id?
if(students.containsKey("23A52")) { //Harvey Dent's id
System.out.println("Yes, 23A52 is a valid student id");
}
if(students.containsKey("1234")) { // bad one
System.out.println("Found the key"); // doesn't print
}
// is this a valid student name?
if(students.containsValue("Harvey Dent")) {
System.out.println("Found Harvey!");
}
// do we have any students?
if(!students.isEmpty()) {
System.out.println("We have students!");
}
// sorry, Harvey. You're DC
students.remove("23A52"); // his id is the key
System.out.println(students);
// how many students are there?
System.out.println("There are " + students.size() + " students.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment