about | help | code help+videos | done | prefs |
tmhscs@gmail.com sets
___ _ / __| ___| |_ \__ \/ -_) _| |___/\___|\__| _ _ _ ___ _ | || |__ _ __| |_ / __| ___| |_ | __ / _` (_-< ' \\__ \/ -_) _| |_||_\__,_/__/_||_|___/\___|\__| _ _ _ _ _ _ _ ___ _ | | (_)_ _ | |_____ __| | || |__ _ __| |_ / __| ___| |_ | |__| | ' \| / / -_) _` | __ / _` (_-< ' \\__ \/ -_) _| |____|_|_||_|_\_\___\__,_|_||_\__,_/__/_||_|___/\___|\__| _____ ___ _ |_ _| _ ___ ___/ __| ___| |_ | || '_/ -_) -_)__ \/ -_) _| |_||_| \___\___|___/\___|\__|A set is a collection that contains no duplicate elements. They can be useful if you know you are working with unique values, such as student IDs, school codes, creating categories for things, building data bases with unique users, and so much more. Note: Sets do not keep indexes for their elements. They are only interested on if a value is inside of the set or not.
In Java, Set is an interface that is implemented by the classes HashSet, LinkedHashSet, and TreeSet.
HashSet is the fastest but does not maintain any ordering.
LinkedHashSet maintains the insertion order of the elements.
TreeSet maintains natural ascending order of the elements.
(natural order means integers least-to-greatest, strings alphabetical, etc.)
Here are some examples of creating sets:
Set<Integer> uniqueNumbers = new HashSet<Integer>();
Set<Double> dataValuesInOrder = new LinkedHashSet<Double>();
Set<String> namesAlphabetically = new TreeSet<String>();
Here are the methods you will need to know about sets:
set.add(VALUE) --- will add some value to the set. Returns true if it wasn't already present.
set.addAll(COLLECTION) --- adds all values from a collection to the set. Returns true if the set changed.
set.contains(VALUE) --- returns true if some value is already in the set.
set.containsAll(COLLECTION) --- returns true if the set already has all values in the collection.
set.remove(VALUE) --- removes the value from the set if it exists. Returns true if found and removed it.
set.removeAll(COLLECTION) --- removes all values in the collection from the set. Returns true if the set changed.
set.retainAll(COLLECTION) --- makes set only retain values found in the collection. Returns true if the set changed.
set.isEmpty() --- returns true if the set is empty.
set.size() --- returns the number of elements in the set.
NOTE: CODINGBAT DOES NOT FULLY SUPPORT SETS
Use the following as necessary to convert between data structures:
ARRAY TO SET: Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
SET TO ARRAY: Integer[] arr = set.toArray(new Integer[set.size()]);
LIST TO SET: Set<Integer> set = new HashSet<Integer>(list);
SET TO LIST: List<Integer> list = new ArrayList<Integer>(set);
HashSet Problems:
countDuplicates
allCharsSame
countAlpha
LinkedHashSet Problems:
removeDuplicateLetters
removeDuplicateWords
firstUniqueLetter
TreeSet Problems:
sortAndRemoveDuplicates
common H
atLeastOnce H
symmetricSetDifference H
firstLetters
removeDuplicateWords
repeatedLetters
setMismatch
Authoring docs
Copyright Nick Parlante 2017 - privacy