SORU
10 ŞUBAT 2011, PERŞEMBE


HashMap aynı anahtar altında birden fazla değer ile

Bize bir anahtar ve iki değere sahip bir HashMap uygulamak mümkün. HashMap gibi mi?

Lütfen benim için bunu söylüyorum, eğer bir yolu varsa hayır () tarafından da anahtar olarak bir ile üç değerleri depolama uygulamak için başka yol yok mu?

CEVAP
10 ŞUBAT 2011, PERŞEMBE


Olabilir:

  1. Değer olarak bir liste içeren bir harita kullanın. Map<KeyType, List<ValueType>>.
  2. Yeni bir sarıcı sınıf oluşturmak ve haritada yaparak örneklerini yerleştirin. Map<KeyType, WrapperType>.
  3. Dersi gibi bir kare ve ambalaj kağıtları sürü oluşturma kaydeder) kullanın. Map<KeyType, Tuple<Value1Type, Value2Type>>.
  4. Birden haritaları yan yana.

Örnekler

1. Bu değer listesini göster

// create our map
Map<string, List<Person>> peopleByForename = new HashMap<string, List<Person>>();    

// populate it
List<Person> people = new ArrayList<Person>();
people.Add(new Person("Bob Smith"));
people.Add(new Person("Bob Jones"));
peopleByForename.Add("Bob", people);

// read from it
List<Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs[0];
Person bob2 = bobs[1];

2. Sarıcı sınıfı kullanarak

// define our wrapper
class Wrapper {
    public Wrapper(Person person1, Person person2) {
       this.person1 = person1;
       this.person2 = person2;
    }

    public Person getPerson1 { return this.person1; }
    public Person getPerson2 { return this.person2; }

    private Person person1;
    private Person person2;
}

// create our map
Map<string, Wrapper> peopleByForename = new HashMap<string, Wrapper>();

// populate it
Wrapper people = new Wrapper()
peopleByForename.Add("Bob", new Wrapper(new Person("Bob Smith"),
                                        new Person("Bob Jones"));

// read from it
Wrapper bobs = peopleByForename["Bob"];
Person bob1 = bobs.Person1;
Person bob2 = bobs.Person2;

3. Bir demet kullanarak

// you'll have to write or download a Tuple class in Java, (.NET ships with one)

// create our map
Map<string, Tuple2<Person, Person> peopleByForename = new HashMap<string, Tuple2<Person, Person>>();

// populate it
peopleByForename.Add("Bob", new Tuple2(new Person("Bob Smith",
                                       new Person("Bob Jones"));

// read from it
Tuple<Person, Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs.Item1;
Person bob2 = bobs.Item2;

4. Çoklu haritalar

// create our maps
Map<string, Person> firstPersonByForename = new HashMap<string, Person>();
Map<string, Person> secondPersonByForename = new HashMap<string, Person>();

// populate them
firstPersonByForename.Add("Bob", new Person("Bob Smith"));
secondPersonByForename.Add("Bob", new Person("Bob Jones"));

// read from them
Person bob1 = firstPersonByForename["Bob"];
Person bob2 = secondPersonByForename["Bob"];

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • 0TACTICAL0HIPPY0

    0TACTICAL0HI

    30 EYLÜL 2012
  • Jonathan Flavell

    Jonathan Fla

    1 HAZİRAN 2006
  • LIVESTRONG.COM

    LIVESTRONG.C

    5 EKİM 2005