Studyon Minte9.com
Head First (Java)




List, Set, Map



	// ArrayList (most simplest)
	// Collections (sorting)
	// Comparable (sorting objects)
	// Comparator (sorting object by more than one element)
	// HashSet (no duplicates, but we loose sorting)
	// TreeSet (sorting & no duplicates)
	// HashMap

* ArrayList

	Display a list without sorting. ArrayList doesn't have a sort method.

	%java
		// 

		package testingapp;
		import java.util.*;

		public class TestingApp {
		    
		    ArrayList<String> songList = new ArrayList<String>();
		    
		    public static void main(String[] args) {
			new TestingApp().go();
		    }
		    
		    public void go() {
			songList.add("Pink Moon");
			songList.add("Listen");
			songList.add("Shiva Moon");
			songList.add("Listen");
			songList.add("Deep Channel");
			
			System.out.println(songList);
				// Output:
				// [Pink Moon, Listen, Shiva Moon, Listen, Deep Channel]
		    }
		}


* Collections (sorting)

	There is a sort() method in the Collections class. It takes a List as argument, and since ArrayList 
	implements List interface, ArrayList is a List.

	%java
		package testingapp;
		import java.util.*;

		public class TestingApp {
		    
		    ArrayList<String> songList = new ArrayList<String>();
		    
		    public static void main(String[] args) {
			new TestingApp().go();
		    }
		    
		    public void go() {
			songList.add("Pink Moon");
			songList.add("Listen");
			songList.add("Shiva Moon");
			songList.add("Listen");
			songList.add("Deep Channel");

			/*--- Look here ---*/
			
			Collections.sort(songList);
			System.out.println(songList);
			    // Output:
			    // [Deep Channel, Listen, Listen, Pink Moon, Shiva Moon]
			
		    }
		}


* Comparable (sorting objects)

	When you are sorting objects, the compiler doesn't know what to sort. The object by we sort needs to
	implements Comparable.

	%java
		package testingapp;
		import java.util.*;

		public class TestingApp {
		    
		    ArrayList<Song> songList = new ArrayList<Song>();
		    
		    public static void main(String[] args) {
			new TestingApp().go();
		    }
		    
		    public void go() {
			songList.add(new Song("Pink Moon", "Nick Drake"));
			songList.add(new Song("Listen", "Tahiti 80"));
			songList.add(new Song("Shiva Moon", "Prem Joshua"));
			songList.add(new Song("Listen", "Tahiti 80"));
			songList.add(new Song("Deep Channel", "Afro Celts"));
			
			Collections.sort(songList);
			System.out.println(songList); // use overrided method toString()
			    // Output:
			    // [Deep Channel, Listen, Listen, Pink Moon, Shiva Moon]
			
		    }
		    
		    /*--- Look here---*/
		    public class Song implements Comparable<Song> {
			String title;
			String artist;
			
			@Override
			public int compareTo(Song s) {
			    return title.compareTo(s.getTitle());
			}
			
			public Song(String t, String a) {
			    title = t;
			    artist = a;
			}
			
			public String getTitle() {
			    return title;
			}
			
			public String getArtist() {
			    return artist;
			}
			
			/*--- Look here---*/

			@Override
			public String toString() {
			    return title;
			}
		    }
		}


* Comparator (sorting object by more than one element)

	When you make a collection element comparable (by having it implement Comparable), you get only 
	one chance to implement the compareTo() method.

	An element in a list can compare itselfto another of its own type in only one way using its compareTo() 
	method.
	
	Comparator is external to the element type you're comparing, it's a separate class. So you can make 
	as many of these as you like!

	#java
		package testingapp;
		import java.util.*;

		public class TestingApp {
		    
		    ArrayList<Song> songList = new ArrayList<Song>();
		    
		    public static void main(String[] args) {
			new TestingApp().go();
		    }
		    
		    public void go() {
			songList.add(new Song("Pink Moon", "Nick Drake"));
			songList.add(new Song("Listen", "Tahiti 80"));
			songList.add(new Song("Shiva Moon", "Prem Joshua"));
			songList.add(new Song("Listen", "Tahiti 80"));
			songList.add(new Song("Deep Channel", "Afro Celts"));
			
			Collections.sort(songList);
			System.out.println(songList);
			    // Output:
			    // Afro Celts: Deep Channel, Tahiti 80: Listen, Tahiti 80: Listen,
			    // Nick Drake: Pink Moon, Prem Joshua: Shiva Moon
			
			/*--- Look here---*/

			ArtistComparator artistComparator = new ArtistComparator();
			Collections.sort(songList, artistComparator);
			System.out.println(songList);
			    // Output:
			    // Afro Celts: Deep Channel, Nick Drake: Pink Moon, Prem Joshua: Shiva Moon, 
			    // Tahiti 80: Listen, Tahiti 80: Listen     
		    }

		    /*--- Look here---*/
		    
		    // We still need the song to be Comparable, even if we use Comparator

		    public class Song implements Comparable<Song>{ 
			String title;
			String artist;

			@Override
			public int compareTo(Song s) {
			    return title.compareTo(s.getTitle());
			}
			
			public Song(String t, String a) {
			    title = t;
			    artist = a;
			}
			
			public String getTitle() {
			    return title;
			}
			
			public String getArtist() {
			    return artist;
			}
			
			@Override
			public String toString() {
			    return artist + ": " + title;
			}
		    }

		    /*--- Look here---*/
		    
		    public class ArtistComparator implements Comparator<Song> {
			
			@Override
			public int compare(Song one, Song two) {
			    return one.getArtist().compareTo(two.getArtist());
			}
		    }
		}

	
		
* HashSet (no duplicates, but we loose sorting)

	HashSet has a simple addAll() method that cane take another collection and use it to populate the 
	HashSet.

	%java
		package testingapp;
		import java.util.*;

		public class TestingApp {
		    
		    ArrayList<Song> songList = new ArrayList<Song>();
		    
		    public static void main(String[] args) {
			new TestingApp().go();
		    }
		    
		    public void go() {
			
			songList.add(new Song("Pink Moon", "Nick Drake"));
			songList.add(new Song("Listen", "Tahiti 80"));
			songList.add(new Song("Shiva Moon", "Prem Joshua"));
			songList.add(new Song("Listen", "Tahiti 80"));
			songList.add(new Song("Deep Channel", "Afro Celts"));

			HashSet<Song> songSet = new HashSet<Song>();
			songSet.addAll(songList);
			System.out.println(songSet);
			    // Output:
			    // [Listen, Shiva Moon, Deep Channel, Pink Moon]
		    }
		    
		    public class Song implements Comparable<Song>{
			String title;
			String artist;
			
			// Title is a String, and Strings have an overridden equals() method.
			@Override
			public boolean equals(Object aSong) {
			    Song s = (Song) aSong;
			    return title.equals(s.getTitle());
			}
			@Override
			public int hashCode() {
			    return title.hashCode();
			}
			
			@Override
			public int compareTo(Song s) {
			    return title.compareTo(s.getTitle());
			}
			
			public Song(String t, String a) {
			    title = t;
			    artist = a;
			}
			
			public String getTitle() {
			    return title;
			}
			
			public String getArtist() {
			    return artist;
			}
			
			@Override
			public String toString() {
			    return title;
			}
		    }
		}



* TreeSet (sorting & no duplicates)

	TreeSet is similar to HashSet in that it prevents duplicates. But it also keeps the list sorted. The 
	downside to TreeSet is that if you don't need sorting, you're still paying for it with a small performance 
	hit. But you'll probably find that the hit is almost impossible to notice for most apps.

	%java
		package testingapp;
		import java.util.*;

		public class TestingApp {
		    
		    ArrayList<Song> songList = new ArrayList<Song>();
		    
		    public static void main(String[] args) {
			new TestingApp().go();
		    }
		    
		    public void go() {
			
			songList.add(new Song("Pink Moon", "Nick Drake"));
			songList.add(new Song("Listen", "Tahiti 80"));
			songList.add(new Song("Shiva Moon", "Prem Joshua"));
			songList.add(new Song("Listen", "Tahiti 80"));
			songList.add(new Song("Deep Channel", "Afro Celts"));
			
			/*--- Look here ---*/

			TreeSet<Song> songSet = new TreeSet<Song>();
			songSet.addAll(songList);
			System.out.println(songSet);
			    // Output:
			    // [Deep Channel, Listen, Pink Moon, Shiva Moon]
		    }
		    
		    /*--- Look here ---*/

		    public class Song implements Comparable<Song>{
			String title;
			String artist;
			
			@Override
			public int compareTo(Song s) {
			    return title.compareTo(s.getTitle());
			}
			
			public Song(String t, String a) {
			    title = t;
			    artist = a;
			}
			
			public String getTitle() {
			    return title;
			}
			
			public String getArtist() {
			    return artist;
			}
			
			@Override
			public String toString() {
			    return title;
			}
		    }
		}


* HashMap

	Each element in a Map is actually TWO objects, a key and a value. You can have duplicate values, but
	NOT duplicate keys.

	%java
		package testingapp;
		import java.util.*;

		public class TestingApp {

		    public static void main(String[] args) {
			
			HashMap<String, Integer> scores = new HashMap<String, Integer>();
			scores.put("a", 1);
			scores.put("a", 2); // overrites preview "a" key
			scores.put("b", 2);
			scores.put("c", 2);
			System.out.println(scores.get("a"));
		    }
		}