An Integer Array as a Key for Dictionary

Dictionary with integer array as a key

It isn't predefined because it is expensive. If you know your list is short then just implement the obvious overrides. If not, you'll have to come up with some kind of heuristic for at least GetHashCode. Say, GetHashCode of only the first couple of elements xor-ed together with the Length.

How to map an array of integers as keys to dictionary values

You can map() each array element to the corresponding value in
the dictionary. If it is guaranteed that all array elements are
present as keys in the dictionary then you can do:

let mysteryDoors = [3, 1, 2]
let doorPrizes = [ 1:"Car", 2: "Vacation", 3: "Gift card"]

let prizes = mysteryDoors.map { doorPrizes[$0]! }
print(prizes) // ["Gift card", "Car", "Vacation"]

To avoid a runtime crash if a key is not present, use

let prizes = mysteryDoors.flatMap { doorPrizes[$0] }

to ignore unknown keys, or

let prizes = mysteryDoors.map { doorPrizes[$0] ?? "" }

to map unknown keys to a default string.

How to use int array as dictionary key C#

Arrays are reference types. Thus, two arrays having the same contents have two different object identities and are not "equal".

If all your arrays have a constant number of elements (for example, if you use the array to store 2D coordinates), consider using a value tuple instead:

public Dictionary<(int, int), car> listOfCells = new Dictionary<(int, int), car>();

Whether a Dictionary can have Array as Key?

By default only references of the arrays would be compared, so you either have to

  1. provide a custom IEqualityComparer<string[]> or
  2. use a Tuple<string, string> as key instead ( since you only have two strings)

Here's a similar question's answer which shows how to create a custom comparer for the Dictionary- constructor.

Int array as a key in a Dictionary VS a string

As ever, you should test performance rather than guessing about it or asking others to guess. However, two things to think about:

  • You've shown an IPv4 address... what about IPv6? That could definitely complicate things
  • The IPv4 address is really 4 bytes... so why not convert that into a single uint or IPAddress as the key? That way you don't need to write your own EqualityComparer<int[]>. It's a shame IPAddress doesn't implement IEquatable<IPAddress>, but it looks like it does override Equals/GetHashCode appropriately.

I'd consider the first point, write detailed benchmarks which work out how important this really is - if you're really only looking up about once per second, it's very unlikely to be important at all - and then try the various options. I would only move away from the simplest code (which would be a string if that's how you're already receiving the IP address) if you can show a proven, significant improvement.

Getting from a dictionary with int[] arrays as value

Create an IEqualityComparer and define your dictionary using that. you can find sample code in below SO question:

An integer array as a key for Dictionary



Related Topics



Leave a reply



Submit