











































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












































Lecture 24
keySet()
Set
values()
Collection
iterator()
compareTo()
compare()
public Play(String title,private int year;private String genre;private String title; String genre, int year)
{ this.year = year;this.genre = genre;this.title = title;
{ public String getTitle()} return this.title;
{ public String getGenre()} return this.genre;
}
{ public int getYear() return this.year;
{ public String toString()} return this.title + ", " + ')';this.genre + " (" + this.year +
}
}
{ public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),
};
}
}
Comparable
{ public Play(String title,private int year;private String genre;private String title; String genre, int year)
{ this.year = year;this.genre = genre;this.title = title;
{ public String getTitle()} return this.title;
{ public String getGenre()} return this.genre;
}
{ public int getYear() return this.year;
{ public String toString()} return this.title + ", " + ')';this.genre + " (" + this.year +
{ public int compareTo(Play p)} return this.title.compareTo( p.title);
}
}
Comparable
{ public Play(String title,private int year;private String genre;private String title; String genre, int year)
{ this.year = year;this.genre = genre;this.title = title;
{ public String getTitle()} return this.title;
{ public String getGenre()} return this.genre;
}
{ public int getYear() return this.year;
{ public String toString()} return this.title + ", " + ')';this.genre + " (" + this.year +
{ public int compareTo(Play p)} return this.title.compareTo( p.title);
}
}
{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),
for (Play p : plays)TreeSet
for (Play p : titleSet) System.out.println(p);
}
}
{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),
for (Play p : plays)TreeSet
for (Play p : titleSet) System.out.println(p);
}
}
{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),
for (Play p : plays)TreeSet
for (Play p : titleSet) System.out.println(p);
}
}
{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[] plays = new Play[] {// Set up the data new Play("The Merchant of Venice", "Comedy", 1597)new Play("Macbeth", "Tragedy", 1606),new Play("As You Like It", "Comedy", 1599),new Play("Henry VI", "History", 1592),new Play("A Midsummer Night's Dream", "Comedy", 1595),new Play("Othello", "Tragedy", 1603),
for (Play p : plays)TreeSet
for (Play p : titleSet) System.out.println(p);
}
}
genre = Tragedyname = Macbeth date = 1606
genre = Tragedyname = Othello date = 1603
date = 1592^ genre = Historyname = Henry VI
genre = Comedyname = As You Like It date = 1599
genre = Comedy
name = The Merchant of Venice
date = 1597
genre = Comedy
name = A Midsummer Night's Dream
date = 1595
TreeSet titleSet
{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[]// Set up the data
(^) plays
(^) = (^) new
(^) Play[]
(^) { (^) stuff
(^) };
for (Play p : plays)TreeSet
for (Play p : titleSet) System.out.println(p);
for (Play p : plays)TreeSet
for (Play p : dateSet) System.out.println(p);
}
}
{ public class TestPlays import java.util.TreeSet; { public static void main(String[] args) Play[]// Set up the data
(^) plays
(^) = (^) new
(^) Play[]
(^) { (^) stuff
(^) };
for (Play p : plays)TreeSet
for (Play p : titleSet) System.out.println(p);
for (Play p : plays)TreeSet
for (Play p : dateSet) System.out.println(p);
}
}