A tutorial on sets in Java; in this tutorial I show you how to use HashSet, TreeSet and LinkedHashSet, plus how to use the Set interface methods and how to do stuff like finding the intersection of two sets.
After starting the video, click the maximise button to make it fullscreen so you can see the code!
Code For This Tutorial
App.java:
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
public class App {
public static void main(String[] args) {
// HashSet does not retain order.
// Set<String> set1 = new HashSet<String>();
// LinkedHashSet remembers the order you added items in
// Set<String> set1 = new LinkedHashSet<String>();
// TreeSet sorts in natural order
Set<String> set1 = new TreeSet<String>();
if (set1.isEmpty()) {
System.out.println("Set is empty at start");
}
set1.add("dog");
set1.add("cat");
set1.add("mouse");
set1.add("snake");
set1.add("bear");
if (set1.isEmpty()) {
System.out.println("Set is empty after adding (no!)");
}
// Adding duplicate items does nothing.
set1.add("mouse");
System.out.println(set1);
// ///////// Iteration ////////////////
for (String element : set1) {
System.out.println(element);
}
// ////////// Does set contains a given item? //////////
if (set1.contains("aardvark")) {
System.out.println("Contains aardvark");
}
if (set1.contains("cat")) {
System.out.println("Contains cat");
}
/// set2 contains some common elements with set1, and some new
Set<String> set2 = new TreeSet<String>();
set2.add("dog");
set2.add("cat");
set2.add("giraffe");
set2.add("monkey");
set2.add("ant");
////////////// Intersection ///////////////////
Set<String> intersection = new HashSet<String>(set1);
intersection.retainAll(set2);
System.out.println(intersection);
////////////// Difference /////////////////////////
Set<String> difference = new HashSet<String>(set2);
difference.removeAll(set1);
System.out.println(difference);
}
}
Set is empty at start [bear, cat, dog, mouse, snake] bear cat dog mouse snake Contains cat [cat, dog] [giraffe, ant, monkey]
Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.
All pages on this site are copyright © 2013 John W. Purcell
Can you please upload the Java collection videos for further topics….tutorial 6,7…..etc.
Thanks,
Regards
Joel Lobo
I haven’t made them yet, Joel. But don’t worry, they’re coming soon.
Thanks for your grateful Java Tutorial informations.
Awesome tutorials…short and sweet….making my life so easy….Love you dude for such a worthy contribution….God bless you!!!!