简体   繁体   中英

create map with string key and object reference value

I am having trouble with this one part of my assignment: Create a Map where the key is a type of animal and the value is a list of CartoonCharacter objects that are that type of animal, stored in alphabetical order by name. Print each key from the map and the list of objects that key references. I understand how to create a map and what the keys and values represent but I am having trouble understanding how i would maybe iterate through the objects and extract the types to use as keys. maybe using the put method? here is code so far

public class CartoonDriver {

public static void main(String[] args) {

    //Construct objects of cartoon characters
    CartoonCharacter BugsBunny = new CartoonCharacter("Bugs Bunny","rabbit",1990);
    CartoonCharacter RogerRabbit = new CartoonCharacter("Roger Rabbit", "rabbit",1858);
    CartoonCharacter MickeyMouse = new CartoonCharacter("Mickey Mouse", "mouse",1928);
    CartoonCharacter MinnieMouse = new CartoonCharacter("Minnie mouse", "mouse",1930);
    CartoonCharacter RoadRunner = new CartoonCharacter("Road Runner", "roadrunner",1986);
    CartoonCharacter DaffyDuck = new CartoonCharacter("Daffy Duck", "duck",1999);
    CartoonCharacter DonaldDuck = new CartoonCharacter("Donald Duck", "duck",1958);
    CartoonCharacter ScoobyDoo = new CartoonCharacter("Scooby Doo", "dog",1975);
    CartoonCharacter WinnieThePooh = new CartoonCharacter("Winnie The Pooh", "bear",1963);
    CartoonCharacter Snoopy = new CartoonCharacter("Snoopy", "dog",1959);


    //Create toons array list to add characters to
    List<CartoonCharacter> toons = new ArrayList<CartoonCharacter>();

    //Add each characther to the array list
    toons.add(BugsBunny);
    toons.add(RogerRabbit);
    toons.add(MickeyMouse);
    toons.add(MinnieMouse);
    toons.add(RoadRunner);
    toons.add(DaffyDuck);
    toons.add(DonaldDuck);
    toons.add(ScoobyDoo);
    toons.add(WinnieThePooh);
    toons.add(Snoopy);

    //print each object
    for(CartoonCharacter toon : toons){
        System.out.println(toon);
    }

    //Create Map to hold type of toon and toon object as value
    Map<String, CartoonCharacter> toonsMap = new HashMap<>();






}

}

i have tired a couple things but they dont make since like using the put method as in toons.put(rabbit, BugsBunny); but that does not seem right to me to have to do that for every object

Try this.

Map<String, List<CartoonCharacter>> map = toons.stream()
    .collect(Collectors.groupingBy(CartoonCharacter::getType, Collectors.toList()));

I assumed that the class CartoonCharacter has String getType() method.

The question states exactly what you should store in your map: key is type of animal (a string ie "rabbit") and the value is a list of cartoon characters of that type of animal. A simple way is to create a list for every type of animal, and then put the respective cartoon character into the list it belongs in. Then you can create your map, with these lists as the values in your map.

We don't know the code behind the CartoonCharacter class, but we can assume that there would hopefully be a method called getType, or getAnimalType, or something similar. This is how you can get the animal type for each character.

The best way to probably do this would be to loop through all your cartoon characters, and get their animal type using the method we assume exists. Then check if your map contains a key for that animal type. If it doesn't, it means it's the first occurrence of that animal so far, so you would create a key and for the value you would create a List with one element: the current cartoon character. If, however, the key exists, that means all you need to do is get the list for that animal type, and add the current cartoon character to the existing list.

This is not a complete answer, but I think you want to use a SortedMap rather than a List. Your description of the problem says you want them in alphabetical order, presumably by species (the second parameter to the CartoonCharacter constructor).

So you want to use a class that implements SortedMap, eg, TreeMap.

so... TreeMap myMap = new TreeMap;

then

Let's add standard getter methods ton your CartoonCharacter class, and also a method to get the sort key. You should also provide a toString method or some other method that gets a nice printable representation of the CartoonCharacter.

Class CartoonCharacter
{
...
public getName() { return name; }
public getSpecies { return species; }
public getBirthDate { return birthDate; }
public getSortKey { String s = getSpecies(); return s.concat(getName()); }
...
}

myMap.put(BugsBunny.getSortKey(), BugsBunny); ...

At the end, you can use the keySet() method to get an iterator over the keys, then iterate through them.

Iterator iter = myMap.keySet().iterator();

in older versions of java, you'd use hasNext() and next() to iterate through the keys. In modern java, you can use the for-each loop:

for (CartoonCharacter ch: iter)
{
    System.out.print(ch.toString();
}

I think something like this will get you what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM