Data structure in C# - Dictionary<TKey, TValue>
Hello guys, I have a quick question... I have a miss-conception, when we say the term "dictionary" ; is it just another term for a hash map/hash table? Or it just uses the idea of hash table behind the scenes? Because if I remember correctly, we also have the concept of Hash set where there (I think), it takes only one generic parameter. How does these 2 differs in terms of one is key-value and the other is just the value please, I'm a bit confused.
39 Replies
yes, the C# hash table is just called dictionary for reasons that i don't know
you look things up in it
hmm but in general they are the same thing ?
yes, different names for the same thing
ah ok
but hmm I don't understand 1 thing... in a dictionary, we use 2 generic parameters but in a hash set, we use only 1
in a hash set the key and value are the same object, basically
well a dictionary maps one key to another value
a set just contains unique values
and, yes, dictionaries use hashing under the hood
same reason why dictionary keys must be unique
It's called a "dictionary" probably because "hashtable" was taken by the non-generic collection
yep I see... but I'm still confuse for the hash set vs dictionary, why 2 parameters for dictionary while only 1 for the hash set... both do the same job, why dictionary requires 2 parameters, can't we just give the value we wanted to store?
Hashset != hashtable
they definitely don't do the same job
ah ?
Hashset is like a list, but stores unique values
Dictionary stores key-value pairs
Angius
REPL Result: Success
Result: HashSet<int>
Compile: 280.693ms | Execution: 25.953ms | React with ❌ to remove this embed.
oh ok
hmm
but we shouldn't get an error because HashSet aren't suppose to be able to contain duplicates, no ?
ah
the thing is we initialize the set but the set automatically removes the duplicate values?
you can think of hashset as a dictionary that only store the keys
yep I see
Sehra
REPL Result: Success
Result: ValueTuple<bool, bool, bool>
Compile: 306.594ms | Execution: 27.314ms | React with ❌ to remove this embed.
this part is very useful, it return true if the item was added, false if not
i'm not sure about that...
a
HashSet<T>
is a collection of T
s. a Dictionary<TKey, TValue>
is a collection of KeyValuePair<TKey, TValue>
s
the keys in a dictionary are hashed
but a dictionary doesn't use or act like a hashsetby the way dictionary also should have unique keys no ?
yes
Angius
REPL Result: Success
Result: Dictionary<int, int>
Compile: 211.767ms | Execution: 24.377ms | React with ❌ to remove this embed.
yep I see, thanks !
Sehra
REPL Result: Failure
Exception: ArgumentException
Compile: 270.283ms | Execution: 23.855ms | React with ❌ to remove this embed.
keep in mind they do different things, first use
[key] = val
to assign, while second calls .Add(key, val)
hmm I have a question, is there a difference in the syntax here to initialize the collection? Like I noticed you guys used
[]
and {}
because first code it didn't give an error but remove the duplicateLiterally the message above yours lol
ah
The
{}
syntax gets lowered to a bunch of .Add()
callslmao
While the
[]=
is a direct assignmentand HashSet.Add does not throw on duplicates, while Dictionary.Add does
yeah I see
hmm what does that mean on HashSet, it does not throw an exception ?
doesn't throw exception, it returns true/false instead. same with Dictionary.TryAdd
ahhh
I see
you really gotta pay more attention to the thread you yourself opened
here
yep sorry, understood how hash set and dictionary/hash table work now though and how they differ, thanks !
It's called a Dictionary because that's what it was called in VB6.