Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a equivalent of HashSet Java in Swift? [duplicate]

Tags:

java

swift

I have specific logic in Java that uses HashSet<String>. Set collection contains only unique items.

For example:

Set<String> mySets = new HashSet<String>();
mySets.add("a");
mySets.add("a");
mySets.add("b");
mySets.add("a");

I get: ["a","b"].

What is equivalent collection in Swift?

Thanks,

like image 234
snaggs Avatar asked Aug 11 '14 15:08

snaggs


People also ask

Can HashSet contain duplicates Java?

Duplicates: HashSet doesn't allow duplicate values. HashMap stores key, value pairs and it does not allow duplicate keys. If the key is duplicate then the old key is replaced with the new value.

Can HashSet contain duplicates?

The first thing you need to know is that HashSet acts like a Set , which means you add your object directly to the HashSet and it cannot contain duplicates. You just add your value directly in HashSet . However, HashMap is a Map type.

Is HashMap same as HashSet?

No. Hashmap is the implementation of Map interface. Hashset on other hand is the implementation of set interface. Hashmap internally do not implements hashset or any set for its implementation.

How does HashSet check for duplicates Java?

If the hashcode of two objects are equal then hashset uses equal() to see if the hashcode matched objects are really equal. And if they are equal the hashset knows that the new object is duplicate of something exist in the HashSet. And the add does not happen. The add() of hashcode returns false.

What is the use of HashSet in Java?

HashSet in Java. The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the set which means that the class does not guarantee the constant order of elements over time. This class permits the null element.

What is the difference between HashSet and HashMap?

The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the set which means that the class does not guarantee the constant order of elements over time. This class permits the null element.

What is LinkedHashSet in Java?

HashSet extends Abstract Set<E> class and implements Set<E>, Cloneable and Serializable interfaces where E is the type of elements maintained by this set. The directly known subclass of HashSet is LinkedHashSet.

How to create an empty instance of HashSet in Java?

HashSet (): It is used to create an instance of the HashSet class that is empty and uses the default equality comparer for the set type. Step 1: Include System.Collections.Generic namespace in your program with the help of using keyword: Step 2: Create a HashSet using the HashSet class as shown below:


1 Answers

The Swift to Java's HashSet is Set.

Example:

var s = Set<Character>()
s.insert("a")
s.insert("a")
s.insert("b")
s.insert("a")
print("s: \(s)")

output:

s: ["b", "a"]

Official docs

like image 198
zaph Avatar answered Nov 15 '22 14:11

zaph