Monday, July 14, 2014

VB.Net: Hashset(Of T) vs List(Of T), C#.Net: HashSet<T> vs List<T>

Here are the differences.

1. List(Of T) can contain duplicates.
Hashset(Of T) contains unique values, No duplicates. Example below:

         
        Dim ls As New List(Of Integer)
        ls.Add(3)
        ls.Add(2)
        ls.Add(1)
        ls.Add(1)
        'Result: 3, 2, 1, 1 '

        Dim hs As New HashSet(Of Integer)
        hs.Add(3)
        hs.Add(2)
        hs.Add(1)
        hs.Add(1)
        'Result: 3, 2, 1 '


2. In List(Of T), Order can be predictable
But In Hashset(Of T), Order can not be predictable

        ls.Remove(2)
        ls.Add(4)
        'Result: 3, 1, 1, 4 '

        hs.Remove(2)
        hs.Add(4)
        'Result: 3, 4, 1 '


3. List(Of T) doesn't have properties like IntersectWith, UnionWith,
IsProperSubsetOf, IsProperSupersetOf, ExceptWith, SymmetricExceptWith
Hashset(Of T) does have all these properties (IntersectWith, UnionWith,
IsProperSubsetOf, IsProperSupersetOf, ExceptWith , SymmetricExceptWith)


Hashset(Of T)


The HashSet(Of T) class provides high-performance set operations. The capacity of a HashSet(Of T) object is the number of elements that the object can hold. A HashSet(Of T) object's capacity automatically increases as elements are added to the object.

List(Of T)


The List(Of T) class is the generic equivalent of the ArrayList class. It implements the IList(Of T) generic interface by using an array whose size is dynamically increased as required.You can add items to a List(Of T) by using the Add or AddRange methods

No comments:

Post a Comment

React-select is very slow on larger list - Found solution - using react-window

 I had more than 4000 items in searchable dropdownlist. I have used react-select but it was very slow. finally I found complete solution to ...