Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary with two keys?

Tags:

c#

dictionary

I am keeping track of values in a console. Two people "duel" against each other and I was using a dictionary to keep the names recorded along with damage done.

var duels = new Dictionary<string, string>();
duels.Add("User1", "50");
duels.Add("User2","34");

I'm trying to store both users in the same dictionary row, so it could be verified as User1 is dueling against User2. This way if another duel started, it would not interfere with User1 or User2.

duels.Add("KeyUser1","KeyUser2","50","34",.../*Other attributes of the duel*/);

I need two keys so I can check where the user's damage will go. The damage will always go to the other key--vice versa. What can I do to make this work?

Thank you.

like image 328
Kyle Avatar asked Feb 27 '12 03:02

Kyle


People also ask

Can a dictionary have two keys?

Answer. No, each key in a dictionary should be unique. You can't have two keys with the same value.

Can we have 2 Keys in dictionary Python?

Dictionaries in Python First, a given key can appear in a dictionary only once. Duplicate keys are not allowed. A dictionary maps each key to a corresponding value, so it doesn't make sense to map a particular key more than once.

Can a dictionary have 2 keys in C#?

It's a dictionary of dictionaries, so you have 2 keys to access each object, the key for the main dictionary to get you the required sub dictionary, and then the second key for the sub dictionary to get you the required item.

Can a dictionary have two same keys with different values?

You can't. Keys have to be unique.


2 Answers

public class Duel
{
  public string User1 {get; protected set;}
  public string User2 {get; protected set;}
  public Duel(string user1, string user2)
  {
    User1 = user1;
    User2 = user2;
  }

  public HashSet<string> GetUserSet()
  {
    HashSet<string> result = new HashSet<string>();
    result.Add(this.User1);
    result.Add(this.User2);
    return result;
  }

  //TODO ... more impl
}

Let's make some duels. CreateSetComparer allows the dictionary to use the values of the set for equality testing.

List<Duel> duelSource = GetDuels();
Dictionary<HashSet<string>, Duel> duels =
  new Dictionary<HashSet<string>, Duel>(HashSet<string>.CreateSetComparer());

foreach(Duel d in duelSource)
{
  duels.Add(d.GetUserSet(), d);
}

And finding a duel:

HashSet<string> key = new HashSet<string>();
key.Add("User1");
key.Add("User2");
Duel myDuel = duels[key];
like image 94
Amy B Avatar answered Sep 27 '22 20:09

Amy B


You could try making a custom data type for the key:

class DualKey<T> : IEquatable<DualKey<T>> where T : IEquatable<T>
{
    public T Key0 { get; set; }
    public T Key1 { get; set; }

    public DualKey(T key0, T key1)
    {
        Key0 = key0;
        Key1 = key1;
    }

    public override int GetHashCode()
    {
        return Key0.GetHashCode() ^ Key1.GetHashCode();
    }

    public bool Equals(DualKey<T> obj)
    {
        return (this.Key0.Equals(obj.Key0) && this.Key1.Equals(obj.Key1))
            || (this.Key0.Equals(obj.Key1) && this.Key0.Equals(obj.Key0));
    }
}

Then use a Dictionary<DualKey<string>, string>;

like image 35
Daryl Avatar answered Sep 27 '22 21:09

Daryl