Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Java Using Sets v1

This document discusses Java collections and sets. It covers the set interface, set operations like intersection and union, and provides examples of initializing and adding elements to sets. It also discusses using HashSet and TreeSet implementations of sets.

Uploaded by

Milos Mihajlovic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Java Using Sets v1

This document discusses Java collections and sets. It covers the set interface, set operations like intersection and union, and provides examples of initializing and adding elements to sets. It also discusses using HashSet and TreeSet implementations of sets.

Uploaded by

Milos Mihajlovic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Java Collections: Sets

USING THE SET INTERFACE

Dr. Sarah Holderness


PLURALSIGHT AUTHOR
@dr_holderness
Assumptions

General Java knowledge about


inheritance and interfaces
– The Collection and Set Interfaces
– Set operations
What this course
– Differences between Set and List
will focus on
– Practical example analyzing
Tweets
What is a Collection?
A Java Set inherits from a Collection, which is a general
container for any Java Objects
COLLECTION
The Collection Interface

A Collection is an Interface, like a Blueprint, that says you


MUST implement these methods to be a Collection:

COLLECTION
•add()
•remove()
•size()
•contains()
•clear()
•isEmpty()
•… and more!
Types of Collections

COLLECTION INTERFACE

LIST SET

An ordered group A group of unique values


of values that are without an index
indexed
numerically

List and Set are two subtypes of


Collection and there are many more!
Types of Collections

COLLECTION INTERFACE

LIST SET

An ordered group A group of unique values


of values that are without an index
indexed
numerically

In this course we’ll cover Set!


The Set Interface
A List

0 1 2 3 4 5 6 7 8

A Set

Unique, unordered elements


The Set Interface
COLLECTION

SETS

Collection methods Set inherits its


Set overrides: methods from the
• equals() Collection interface, but
• add() • remove() has additional rules on
• addAll() • removeAll() some methods. For
example add() must
• clear() • retainAll() add only unique values.
• contains() • size()
• containsAll() • toArray()
• equals() … And 4 more!
Can We Initialize a Set Yet?
'Set' is abstract;
Set<String> languages = new Set<String>(); cannot be instantiated

Since Set is ALSO an Interface, we need a non-abstract 



(non-interface) implementation of a Set to initialize.

•Build our own?


•HashSet
•TreeSet
•EnumSet
Can We Initialize a Set Yet?
'Set' is abstract;
Set<String> languages = new Set<String>(); cannot be instantiated

Since Set is ALSO an Interface, we need a non-abstract 



(non-interface) implementation of a Set to initialize.

•Build our own?


Let’s use a HashSet -
•HashSet one of the most common Sets used

•TreeSet
•EnumSet
Initializing a Set
Our Set will store the names of some programming languages as
Strings.

Set<String> languages = new HashSet<>();


Initializing a Set
Our Set will store the names of some programming languages as
Strings.

Set<String>
Set String languages = new HashSet<>();
HashSet<>

General Set What’s in 
 Specific Type
 Leave String out


Interface the Set of Set here, it’s inferred
Initializing a Set

Why not make the variable a HashSet on the left instead of Set?

HashSet<String> languages = new HashSet<>


HashSet HashSet<>();

This is not wrong…

… but using a general Set on the left lets


us switch out the type of Set we use on
the right without changing any other code!

Using a general Set


Set<String> languages = new TreeSet
Set TreeSet<>(); on the left is more
flexible
Set<String> languages = new MySet
Set MySet<>();
Sets Can Hold any Type of Object

Set of Integers
Set<Integer> numbers = new HashSet<>();

Set of class MyProduct objects


Set<MyProduct> products = new HashSet<>();

Set of any type


Set<Object> objects = new HashSet<>();
Adding Elements to a Set
Set<String> languages = new HashSet<>();
languages.add("HTML");
languages.add("CSS");
languages.add("JavaScript");

System.out.println(languages.size());

Console output
> 3
Adding Elements to a Set - NO Duplicates
Set<String> languages = new HashSet<>(Arrays.asList("HTML","CSS","JS"));

languages.add("HTML");
languages.add("CSS");

System.out.println(languages.size());

for (String language : languages)


System.out.println(language);

Console output
> 3
HTML
CSS
JS
A Review of General Set Operations
We have 2 sets: 1 for front end languages and 1 for back end.

Front End Back End

Java
HTML

JS JS Ruby

CSS
C#
Set Operations - Intersection
What languages are in both the front end set AND the back
end set?
Front End Back End
In Java, a Set's retainAll()
method returns the intersection
of two Sets Java
HTML

JS Ruby

CSS
C#
Set Operations - Intersection (retainAll)
Set<String> frontEnd = new HashSet<>(Arrays.asList("HTML","CSS","JS"));

Set<String> backEnd = new HashSet<>(Arrays.asList("Java","C#","Ruby","JS"));

Set<String> intersection = new HashSet<>(backend);


intersection.retainAll(frontEnd);
We initialize the Set with only the backend
values. Then we retain only the values that are
for (String language : intersection) in common with the frontend values.
System.out.println(language);

Console output
The only value that is
> JS "retained" because it exists
in both groups is JS
Set Operations - Union
What languages are in either the front end set AND the back
end set?
Front End Back End
In Java, a Set's addAll()
method returns the union of
two Sets
HTML Java

JS Ruby
CSS
C#
Set Operations - Union (addAll)
Set<String> frontEnd = new HashSet<>(Arrays.asList("HTML","CSS","JS"));
Set<String> backEnd = new HashSet<>(Arrays.asList("Java","C#","Ruby","JS"));

Set<String> union = new HashSet<>(backend);


union.addAll(frontEnd); Again we initialize the Set with
only the backend values. Then we
add all of the frontend values.
for (String language : union)
System.out.println(language);

Console output
> HTML
CSS Notice JS only appears once
JS even though it was added
again with the frontend values.
Java
Ruby
C#
Set Operations - Subtraction (remove)
Set<String> frontEnd = new HashSet<>(Arrays.asList("HTML","CSS","JS"));

frontEnd.remove("JS");

for (String language : intersection)


System.out.println(language);

Console output
> HTML
CSS
HTML
_ HTML

CSS
JS JS
= CSS

Frontend Frontend without JS


Set Operations - Subtraction (removeAll)
Set<String> frontEnd = new HashSet<>(Arrays.asList("HTML","CSS","JS"));
Set<String> backEnd = new HashSet<>(Arrays.asList("Java","C#","Ruby","JS"));

Set<String> intersection = new HashSet<>(backend);


intersection.addAll(frontEnd);
Union all values, then
intersection.removeAll(backEnd);
subtract all backend values.

for (String language : intersection)


System.out.println(language);

Console output
Java
> HTML
CSS
HTML Java
_ HTML

CSS JS Ruby
C#
JS

C#
Ruby
= CSS

Union Backend Frontend without JS


What if we want our
values sorted?
TreeSet Values are Sorted
Set<String> languages = new TreeSet<>();
languages.add("HTML");
languages.add("Ruby"); Notice these are not
languages.add("JavaScript"); added in sorted order
languages.add("Java");
languages.add("CSS");

for (String language : intersection)


System.out.println(language);

Console output
> CSS Elements are added in sorted
order to the TreeSet
HTML
Java
JavaScript
Ruby
Why can't we just use a
List?
An Unsorted List with Unique Values
vs a Set
String[] languages = {"Java", "C#", "Go"};
String[] moreLanguages = {"Java", "C#", "JavaScript", "Python"};

List<String> combined = new ArrayList<>(Arrays.asList(languages));


for (String language : moreLanguages)
A List needs a loop and
if (!combined.contains(language))
conditional to do the same
combined.add(language); thing as a Set and addAll().

Set<String> combinedSet = new HashSet<>(Arrays.asList(languages));


combinedSet.addAll(Arrays.asList(moreLanguages));

Right now, combinedSet looks [C#, Java, Python, Go, JavaScript]


like this (in no particular order):
Sorting a List vs Using a TreeSet
String[] languages = {"Java", "C#", "Go"};
String[] moreLanguages = {"Java", "C#", "JavaScript", "Python"};

List<String> combined = new ArrayList<>(Arrays.asList(languages));


for (String language : moreLanguages)
if (!combined.contains(language)) A List needs a loop, conditional, and a
combined.add(language); call to sort() to do the same thing as a
TreeSet and addAll().
combined.sort(String::compareTo);

Set<String> combinedSet = new TreeSet<>(Arrays.asList(languages));


combinedSet.addAll(Arrays.asList(moreLanguages));

Right now, combinedSet [C#, Go, Java, JavaScript, Python]


is sorted and looks like this:

You might also like