What is the equivalent of Java LinkedHashSet in C#

1 Answer

0 votes
// HashSet - not 100% equivalent
// check: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1?view=net-8.0

using System;
using System.Collections.Generic;

public class Program
{
	public static void Main(string[] args)
	{
		String[] crew = {"c#", "c", "c++", "java", "python", "php"};
        HashSet<String> hashSet = new HashSet<String>(crew);

        foreach (String value in hashSet){
            Console.Write(value + " "); 
        }

        hashSet.Remove("c++");
        Console.WriteLine();
        foreach (String value in hashSet){
            Console.Write(value + " "); 
        }

        hashSet.Add("c++");
        hashSet.Add("go");
        Console.WriteLine();
        foreach (String value in hashSet){
            Console.Write(value + " "); 
        }

        hashSet.Remove("c++");
        hashSet.TrimExcess();
        hashSet.Add("c++");
        Console.WriteLine();
        foreach (String value in hashSet){
            Console.Write(value + " "); 
        }
	}
}



/*
run:
 
c# c c++ java python php 
c# c java python php 
c# c c++ java python php go 
c# c java python php go c++ 
   
*/

 



answered Feb 22, 2024 by avibootz

Related questions

1 answer 327 views
327 views asked Feb 9, 2019 by avibootz
1 answer 97 views
1 answer 170 views
1 answer 142 views
142 views asked Aug 9, 2023 by avibootz
1 answer 145 views
1 answer 271 views
1 answer 147 views
...