Alexander Trauzzi
Alexander Trauzzi

Reputation: 7395

Keying a collection by type

I'm looking to have a collection of objects that implement a certain interface, but I'd like to only have one per concrete type within the collection.

collection of implementers of dog:
 - instance of dachshund
 - instance of beagle
 - instance of corgi

In .NET, there's a "KeyedByTypeCollection". Does something similar exist in Java in such a way that I could use it on Android?

Thanks!

Upvotes: 0

Views: 130

Answers (4)

Lalit Poptani
Lalit Poptani

Reputation: 67296

I think you need a custom HaspMap that will maintain multiple values with same key,

So, create an simple class that extends HashMap and put values into it.

public class MyHashMap extends LinkedHashMap<String, List<String>> {

    public void put(String key, String value) {
        List<String> current = get(key);
        if (current == null) {
            current = new ArrayList<String>();
            super.put(key, current);
        }
        current.add(value);
    }
}

Now, create the instance of MyHashMap and put values into it as below,

        MyHashMap hashMap = new MyHashMap();
        hashMap.put("dog", "dachshund");
        hashMap.put("dog", "beagle");
        hashMap.put("dog", "corgi");
        Log.d("output", String.valueOf(hashMap));

OUTPUT

{dog=[dachshund, beagle, corgi]}

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198321

If you're willing to use third-party libraries -- and if you don't care about maintaining order -- Guava's ClassToInstanceMap seems applicable here.

ClassToInstanceMap<Dog> map = MutableClassToInstanceMap.create();
map.putInstance(Corgi.class, new Corgi("Spot"));
map.putInstance(Beagle.class, new Beagle("Lady"));
Corgi corgi = map.getInstance(Corgi.class); // no cast required

(Disclosure: I contribute to Guava.)

Upvotes: 2

Kent
Kent

Reputation: 195209

this might be what you are looking for: see the comments in codes

// two Dog(interface) implementations
        // Beagle, Dachshund implements Interface Dog.
        final Dog d1 = new Beagle();
        final Dog d2 = new Dachshund();

        // here is your collection with type <Dog>
        final Set<Dog> set = new HashSet<Dog>();
        set.add(d1);
        set.add(d2);

        // see output here
        for (final Dog d : set) {
            System.out.println(d.getClass());
        }

        // you can fill them into a map
        final Map<Class, Dog> dogMap = new HashMap<Class, Dog>();
        for (final Dog d : set) {
            // dog instances with same class would be overwritten, so that only one instance per type(class)
            dogMap.put(d.getClass(), d); 
        }

the output of system.out.println line would be something like:

class test.Beagle
class test.Dachshund

Upvotes: 0

iozee
iozee

Reputation: 1349

You should look at generics. E.g.: List<Dogs> dogList = new ArrayList<Dogs>();

EDIT: to have only unique instances in your collection, you should use Set<Dogs> dogList = new HashSet<Dogs>();

Upvotes: 1

Related Questions