HashSet and HashMap
HashSet and HashMap
in Java
The HashMap and HashSet in Java are the most popular Collection classes. Both are used for the data
structure. The following table describes the difference between HashMap and HashSet:
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
string[] arr1 = {
"bus",
"truck",
"bus",
"car",
"truck"
};
Console.WriteLine(string.Join(",", arr1));
// HashSet
var h = new HashSet(arr1);
// eliminates duplicate words
string[] arr2 = h.ToArray();
Console.WriteLine(string.Join(",", arr2));
}
}
Output
bus,truck,bus,car,truck
bus,truck,car
To declare HashSet −
var h = new HashSet(arr1);
Source: