Retrieve an item from a HashSet in C#
This post will discuss how to retrieve an item from a HashSet<T> in C#.
A HashSet<T> stores a set of distinct values in no particular order. Unlike a List<T>, a HashSet<T> doesn’t have any index which can be used to store and retrieve an element in constant time. This post provides an overview of some of the feasible options to accomplish this.
1. Using HashSet<T>.TryGetValue() Method
The .NET Framework 4.7.2 included TryGetValue() method in HashSet<T> class. It searches the set for a specified value and returns a boolean value indicating whether the search was successful. Note that it takes an out parameter for storing the matching value or a default value if the search yielded no match.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; using System.Collections.Generic; public class Example { public static void Main() { HashSet<int> numbers = new HashSet<int>() { 1, 2, 3, 4 }; int target = 2; int item; bool found = numbers.TryGetValue(target, out item); Console.WriteLine(found); // True } } |
2. Using Enumerable.Where() Method
Alternatively, you can use the LINQ’s Where() method to filter a sequence of values based on a predicate. The following code example demonstrates the usage of the Where() method to find the specified object in a set.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System; using System.Linq; using System.Collections.Generic; public class User { public string name; public int age; public User(string name, int age) { this.name = name; this.age = age; } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { HashSet<User> users = new HashSet<User>(); users.Add(new User("A", 12)); users.Add(new User("B", 15)); users.Add(new User("C", 18)); User user = users.Where(x => x.name.Equals("B")).FirstOrDefault(); Console.WriteLine(user); // [B, 15] } } |
Note that any changes in the returned object are reflected in the original Set and vice-versa. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using System; using System.Linq; using System.Collections.Generic; public class User { public string name; public int age; public User(string name, int age) { this.name = name; this.age = age; } public override string ToString() { return "[" + name + ", " + age + "]"; } } public class Example { public static void Main() { HashSet<User> users = new HashSet<User>(); users.Add(new User("A", 12)); users.Add(new User("B", 15)); users.Add(new User("C", 18)); User user = users.Where(x => x.name.Equals("B")).FirstOrDefault(); user.age = 16; Console.WriteLine(String.Join(", ", users)); // [A, 12], [B, 16], [C, 18] } } |
That’s all about retrieving an item from a HashSet<T> in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)