diff --git a/solutions/java/gotta-snatch-em-all/2/src/main/java/GottaSnatchEmAll.java b/solutions/java/gotta-snatch-em-all/2/src/main/java/GottaSnatchEmAll.java new file mode 100644 index 0000000..f844284 --- /dev/null +++ b/solutions/java/gotta-snatch-em-all/2/src/main/java/GottaSnatchEmAll.java @@ -0,0 +1,38 @@ +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +class GottaSnatchEmAll { + + static Set newCollection(List cards) { + return new HashSet<> (cards); + } + + static boolean addCard(String card, Set collection) { + return collection.add(card); + } + + static boolean canTrade(Set myCollection, Set theirCollection) { + boolean iHaveExtra = !myCollection.containsAll(theirCollection); + boolean theyHaveExtra = !theirCollection.containsAll(myCollection); + return iHaveExtra && theyHaveExtra; + + } + + static Set commonCards(List> collections) { + Set common = new HashSet<> (collections.get(0)); + + for(Set c : collections){ + common.retainAll(c); + } + return common; + } + + static Set allCards(List> collections) { + Set all = new HashSet<>(); + for(Set c : collections){ + all.addAll(c); + } + return all; + } +}