The HashSet collection has such good properties:

1) HashSet<T> is a very quick way to find elements.

2) Duplicate elements cannot be added in a HashSet<T> . Duplicate elements, if added are internally ignored without giving an error.

3) Indexed based element access is allowed in a HashSet<T> .

the Contains method returns a Boolean value, so how is it possible to get a value, say JAPAN from the code below, only without using foreach ?

 HashSet<string> countries = new HashSet<string> { "JAPAN", "USA", "AUSTRALIA", "CANADA", "CHINA", "RUSSIA", "FRANCE" }; 

    5 answers 5

    Your question is generally meaningless, since for a string the operation "get" value from a hashset is practically useless. Already have the string "JAPAN"? Do you want to get a special string "JAPAN" from the hashstat? I will disappoint you - it will be exactly the same as yours (and perhaps the same thanks to internment). Actually, what's the point of her "getting"?

    As you yourself noted, in this class there is a Contains method, it indicates whether the required value is contained in a hashset, this is quite enough. If necessary, you can use a loop, or a wrapper like linq, if you do not want to use the loop (but most likely the loop will be activated inside). For example, something like this:

     string japan = country.FirstOrDefault(x => x == "JAPAN"); 

    Meaning may have the receipt of values ​​for some other conditions other than equality. for example, all values ​​from 7 characters long:

     var greaterThanSeven = country.Where(x => x.Length > 7); 

    Alternatively, getting values ​​can make sense for some complex data types, where filtering can be performed on individual fields. For example:

      var country = new HashSet<Foo> { new Foo { id = 1, name = "JAPAN" }, new Foo { id = 2, name = "USA" }, new Foo { id = 3, name = "AUSTRALIA" }, new Foo { id = 4, name = "CANADA" }, new Foo { id = 5, name = "CHINA" }, new Foo { id = 6, name = "RUSSIA" }, new Foo { id = 7, name = "FRANCE" }, }; var foo = country.FirstOrDefault(x => x.name == "JAPAN"); 
    • Oh, right. You understood that you wanted a vehicle. - VladD
    • I can not find you in the chat. Vote for synonyms, please: ru.stackoverflow.com/tags/… - Nick Volynkin

    Good question. Only an unsuccessful example. Let's look at the class:

     class Class1 { private Int32 _value; public Int32 Value { get { return _value; } set { _value = value; } } public override int GetHashCode() { return _value.GetHashCode(); } public override bool Equals(object obj) { Class1 c1 = obj as Class1; if (c1 == null) return false; return c1._value == _value; } } 

    Create a couple of objects and add them to the HashSet:

     Class1 c1 = new Class1() { Value = 1 }; Class1 c2 = new Class1() { Value = 2 }; HashSet<Class1> set = new HashSet<Class1>{c1, c2}; 

    Next, we get the key by which we need to get an object from the HashSet:

     Class1 key1 = new Class1() { Value = 2 }; 

    The bottom line is that the values ​​of c1 and key1 same, but they are two different objects:

     Console.WriteLine(set.Contains(key1)); //true Console.WriteLine(Object.ReferenceEquals(c2, key1)); // false 

    And HashSet will show that it contains a similar object:

     Console.WriteLine(set.Contains(key1)); //true 

    The object is similar, but not the same.

    To get that object will have to do a search:

     Class1 result = set.FirstOrDefault(item => item.Equals(key1)); Console.WriteLine(Object.ReferenceEquals(c2, result)); // true 
    • MaLS: c2 and key1 are the same in value - Vladimir
    • @Vladimir This is how to look. Some by the value of a class type variable mean the value of an object reference. - mals

    It is impossible to get a specific value from HashSet.

    • Encrypted? o_O - VladD
    • HashSet does not work the way you wrote. - Pavel Mayorov
    • one
      HashSet stores both hashes and encrypted values. Those. If you add two objects with the same HashCode to it, then there will actually be two different objects in it. The mention of Hash in the class name only hints that hashing is used to speed up operations, but in no way indicates that the class is susceptible to hash collisions. - PashaPash
    • I agree, my explanation is not quite right. - Anatoly Nikolaev

    If I understand correctly, you want to get the same element that you put with HashSet<T> . Specifically, with HashSet<T> for O (1), this is impossible, but it is easy to model with Dictionary<T, T> , if you place the same element as a key and value.

     T GetItemEqualTo(T pattern) { T result; if (dict.TryGetValue(pattern, out result)) return result; else return null; } 

    You will need a generic constraint where T : class .

    (This will not compile for the value-type T , but in this case the concept of the instance itself is meaningless.)

    • a specific value, say JAPAN from the code above - Vladimir

    To get the value by key, you must use a Dictionary .

    Different ones are needed precisely for checking availability, and possibly sorting. You can get any of these objects only in a foreach loop and in similar ways.

    • why? in Dictionary I can get the value like this: myValue = myDictionary[key] - Vladimir
    • @Vladimir, that's it, in the dictionary you can, but in the set you can't. Signs that the class was wrong: you need a choice by key - change the set to the dictionary, noticed that all the values ​​in the dictionary are true - change it to the set. - Qwertiy
    • Why do we need this collection HashSet ???? - Vladimir
    • @Vladimir, in order to mean, was already such an object or not. For example, it can be used to exclude duplicates from the collection. - Qwertiy
    • @Vladimir, the question is incomprehensible. - Qwertiy