-bash-4.2$ cat Candidate.java
import java.util.ArrayList;

public class Candidate {
  private String name;
  private int votes;
  public Candidate(String name, int votes) {
    this.name = name;
    this.votes = votes;
  }
  public String toString() {
    return this.name + ":" + this.votes;
  }
  public static void main(String[] args) {
    Candidate a = new Candidate("Spongebob", 35);
    System.out.println( a );

    ArrayList<Candidate> candidates = new ArrayList<Candidate>();
    candidates.add(new Candidate("Laura" , 12));
    candidates.add(new Candidate("Leslie", 13));
    candidates.add(new Candidate("Larry" , 12));
    candidates.add(new Candidate("Alex"  , 11));
    // I want to sort the list alphabetically
    // I want to sort by votes descending, where same number of votes: alphabetical (descending?) 
    System.out.println( candidates );
  }
}
-bash-4.2$ javac Candidate.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.2$ java Candidate
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
Spongebob:35
[Laura:12, Leslie:13, Larry:12, Alex:11]
-bash-4.2$

--

Plan for today: 

  1. Sort candidates.
  2. Read from file, sort candidates (use HashMap). 
  3. Use a BST to sort candidates instead of HashMaps. 
  4. How was the generic sort designed? Can we design our own?

--

  5. What are Streams? 

--

At this point we should have Homework 7, Lab 7 and (coincidentally) the Final Exam code. 

--

-bash-4.2$ javac Candidate.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.2$ clear
-bash-4.2$ java Candidate
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
Spongebob:35
[Laura:12, Leslie:13, Larry:12, Alex:11]
[Leslie:13, Laura:12, Larry:12, Alex:11]
-bash-4.2$ cat Candidate.java
import java.util.ArrayList;
import java.util.Collections;
// https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

public class Candidate implements Comparable<Candidate> {

  public int compareTo(Candidate another) { // this is compared with another:
    // return -1 if this should come before another, 1 if this comes after another and 0 if they're equal
    if (this.votes > another.votes) return -1;
    if (this.votes < another.votes) return  1; // so we need to be swapped, this and another
    return 0;

  }

  private String name;
  private int votes;
  public Candidate(String name, int votes) {
    this.name = name;
    this.votes = votes;
  }
  public String toString() {
    return this.name + ":" + this.votes;
  }
  public static void main(String[] args) {
    Candidate a = new Candidate("Spongebob", 35);
    System.out.println( a );

    ArrayList<Candidate> candidates = new ArrayList<Candidate>();
    candidates.add(new Candidate("Laura" , 12));
    candidates.add(new Candidate("Leslie", 13));
    candidates.add(new Candidate("Larry" , 12));
    candidates.add(new Candidate("Alex"  , 11));
    // I want to sort the list alphabetically
    // I want to sort by votes descending, where same number of votes: alphabetical
    System.out.println( candidates );
    // prediction one: Laura Leslie Larry Alex

    Collections.sort(candidates); // that's how the request should be formulated
    // prediction two: Leslie Laura Larry Alex

    System.out.println( candidates );

  }
}
-bash-4.2$

--

-bash-4.2$ cat Candidate.java
import java.util.ArrayList;
import java.util.Collections;
// https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

public class Candidate implements Comparable<Candidate> {

  public int compareTo(Candidate another) { // this is compared with another:
    // return -1 if this should come before another, 1 if this comes after another and 0 if they're equal
    if (this.votes > another.votes) return -1;
    if (this.votes < another.votes) return  1; // so we need to be swapped, this and another
    // return 0;
    return this.name.compareTo(another.name); // this passes the responsibility to the names, result is ascending
    // what should I do if what I want is descending order lexicographically?
    // Answer: return - this.name.compareTo(another.name);
  }

  private String name;

  public String getName() {
    return this.name;
  }
  private int votes;
  public Candidate(String name, int votes) {
    this.name = name;
    this.votes = votes;
  }
  public String toString() {
    return this.name + ":" + this.votes;
  }
  public static void main(String[] args) {
    Candidate a = new Candidate("Spongebob", 35);
    System.out.println( a );

    ArrayList<Candidate> candidates = new ArrayList<Candidate>();
    candidates.add(new Candidate("Laura" , 12));
    candidates.add(new Candidate("Leslie", 13));
    candidates.add(new Candidate("Larry" , 12));
    candidates.add(new Candidate("Alex"  , 11));
    // I want to sort the list alphabetically
    // I want to sort by votes descending, where same number of votes: alphabetical (descending?)
    System.out.println( candidates );
    // prediction one: Laura Leslie Larry Alex

    Collections.sort(candidates); // that's how the request should be formulated
    // prediction two (before change to compareTo): Leslie Laura Larry Alex
    // prediction three: Leslie:13 Larry:12 Laura:12 Alex:11

    System.out.println( candidates );

    Collections.sort(candidates, new JudgeOne()); // that's how the request should be formulated

    System.out.println( candidates );
    // prediction last: Alex:11 Larry:12 Laura:12 Leslie:13
  }
}
-bash-4.2$ javac Candidate.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.2$ java Candidate
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
Spongebob:35
[Laura:12, Leslie:13, Larry:12, Alex:11]
[Leslie:13, Larry:12, Laura:12, Alex:11]
[Alex:11, Larry:12, Laura:12, Leslie:13]
-bash-4.2$

--

-bash-4.2$ cat Candidate.java
import java.util.ArrayList;
import java.util.Collections;
// https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

public class Candidate implements Comparable<Candidate> {

  public int compareTo(Candidate another) { // this is compared with another:
    // return -1 if this should come before another, 1 if this comes after another and 0 if they're equal
    if (this.votes > another.votes) return -1;
    if (this.votes < another.votes) return  1; // so we need to be swapped, this and another
    // return 0;
    return this.name.compareTo(another.name); // this passes the responsibility to the names, result is ascending
    // what should I do if what I want is descending order lexicographically?
    // Answer: return - this.name.compareTo(another.name);
  }

  private String name;

  public String getName() {
    return this.name;
  }
  private int votes;
  public Candidate(String name, int votes) {
    this.name = name;
    this.votes = votes;
  }
  public String toString() {
    return this.name + ":" + this.votes;
  }
  public static void main(String[] args) {
    Candidate a = new Candidate("Spongebob", 35);
    System.out.println( a );

    ArrayList<Candidate> candidates = new ArrayList<Candidate>();
    candidates.add(new Candidate("Laura" , 12));
    candidates.add(new Candidate("Leslie", 13));
    candidates.add(new Candidate("Larry" , 12));
    candidates.add(new Candidate("Alex"  , 11));
    // I want to sort the list alphabetically
    // I want to sort by votes descending, where same number of votes: alphabetical (descending?)
    System.out.println( candidates );
    // prediction one: Laura Leslie Larry Alex

    Collections.sort(candidates); // that's how the request should be formulated
    // prediction two (before change to compareTo): Leslie Laura Larry Alex
    // prediction three: Leslie:13 Larry:12 Laura:12 Alex:11

    System.out.println( candidates );

    Collections.sort(candidates, new JudgeOne()); // that's how the request should be formulated

    System.out.println( candidates );
    // prediction last: Alex:11 Larry:12 Laura:12 Leslie:13
  }
}
-bash-4.2$ javac Candidate.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.2$ java Candidate
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
Spongebob:35
[Laura:12, Leslie:13, Larry:12, Alex:11]
[Leslie:13, Larry:12, Laura:12, Alex:11]
[Alex:11, Larry:12, Laura:12, Leslie:13]
-bash-4.2$

--

For next time please look through chapter 11 and chapter 18. 

--