Results 1 to 3 of 3

Thread: how to join two hashsets to one hashset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2024
    Posts
    72

    how to join two hashsets to one hashset

    Hello,

    I have two hashsets of integer as below:
    Code:
    Private _ELSPropNames As New HashSet(Of Integer)
    Private _ELSPropNames1 As New HashSet(Of Integer)
    the first hashset includes 1 and 2 and the second hashset includes 1 and 3.
    I want to mix these two hashsets to one hashset but i need 1 to be included twice in my new hashset. do you know how to do it?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: how to join two hashsets to one hashset

    https://learn.microsoft.com/en-us/do...h?view=net-8.0
    https://www.techiedelight.com/merge-...ets-in-csharp/

    The only problem is that you can't have the same value twice... that defeats the purpose of a hashset...the value is used to calculate the hash, which is then used as the index. So you can only have 1 in there once... because 1 will produce the same hash every time. That second link gives two examples of combining two hashsets, and that one of the values, 4, isn' duplicated despite being in both sets.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,099

    Re: how to join two hashsets to one hashset

    Quote Originally Posted by mred View Post
    i need 1 to be included twice in my new hashset
    That's impossible. If you want duplicates then you should not be using a Hashset, at least for the combined data. You can create an array or generic List like so:
    Code:
    Dim combined = list1.Concat(list2).ToArray() 'Or .ToList()
    That will work with any two IEnumerable(Of T).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width