Logo

Programming-Idioms

History of Idiom 118 > diff from v66 to v67

Edit summary for version 67 by programming-idioms.org:
[Java] Better demo link (shows code)

Version 66

2022-02-24, 18:09:17

Version 67

2022-02-24, 18:09:26

Idiom #118 List to set

Create set y from list x.
x may contain duplicates. y is unordered and has no repeated values.

Turning the list [a,b,c,b] into the set {c,a,b}

Idiom #118 List to set

Create set y from list x.
x may contain duplicates. y is unordered and has no repeated values.

Turning the list [a,b,c,b] into the set {c,a,b}
Variables
y,x
Variables
y,x
Imports
import java.util.Set;
import java.util.HashSet;
Imports
import java.util.Set;
import java.util.HashSet;
Code
Set<T> y = new HashSet<T>(x);
Code
Set<T> y = new HashSet<T>(x);
Comments bubble
x is a Collection<T>, e.g. a List<T>.
Elements of x and y have the same type T.
Comments bubble
x is a Collection<T>, e.g. a List<T>.
Elements of x and y have the same type T.
Doc URL
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/HashSet.html#%3Cinit%3E(java.util.Collection)
Doc URL
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/HashSet.html#%3Cinit%3E(java.util.Collection)
Origin
https://stackoverflow.com/a/1429869/871134
Origin
https://stackoverflow.com/a/1429869/871134
Demo URL
https://repl.it/@ProgIdioms/TalkativeClumsyEmulators
Demo URL
https://replit.com/@ProgIdioms/TalkativeClumsyEmulators#Main.java