简体   繁体   中英

How to add using HashMap multiple values(different types, an int and a string) with the same key?

I have to store a key with multiple values but my values should be a string and an int. For this problem I have to use List,Stack, Queue or Map, I think that Map is the right choice.

I have a lot of pairs of shoes, and every shoe has a size, a colour and a price. The pairs of shoes must be ordered by their size.

I tought that the key could be the size of the shoes and values should be the colour and the price but I don't know how to implement this.

For example I have:

pair 1, size 36, colour black, price 30$
pair 2, size 36, colour white, price 35$
pair 3, size 37, colour black, price 40$
pair 4, size 38, colour black, price 45$

How could I store all of this using Map(I think) without creating a new class of shoes for example?

Update: I am also allowed to use SortedMap, SortedList, etc. In the second part of the problem I have to add and remove shoes of a specific size.

You could use 2 maps for this purposes. First map will contain name of shoes as a key, and map as value. Second map - properties. Something like:

Map<String, Map<String, Object>> shoes = new HashMap<>();
Map<String, Object> shoeProps = new HashMap<>();
shoeProps.put("color", "red");
shoeProps.put("size", 38);
shoeProps.put("price", 35);
shoes.put("nike", shoeProps);

Step One: understand the data.

You do not have one map of data, you have a hierarchy of maps for your data.

Top map: maps the shoe size to a map of "details"

Details map: maps color to price

If you use a map, each key has be unique. You cannot use the sizes as a key since there are not unique.

From your problem description:

I have a lot of pairs of shoes, and every shoe has a size, a colour and a price. The pairs of shoes must be ordered by their size.

a map doesn't seem to be a good choice since Maps are generally no ordered (except for SortedMap ). You want a data structure that could be ordered according to a specific criteria. A List seems like a really good choice. The most common implementation of list is ArrayList .

You first need a class to store your shoes object that should look like this:

public class Shoe{
    int size;
    Color color; // Color is an enum, but it can be a String if you want to be less restrictive
    int price;

    public Shoe(int size, Color color, int price) {
    ...
}

Then you can store your shoes in your list that way:

List<Shoe> shoes = new ArrayList<>();
shoes.add(new Shoe(36, Color.BLACK, 30));
shoes.add(new Shoe(36, Color.WHITE, 35));
shoes.add(new Shoe(37, Color.BLACK, 40));
shoes.add(new Shoe(38, Color.BLACK, 40));
...

And once your list is filled, you can order them by size:

shoes.sort(Comparator.comparing(Shoe::getSize));

Alternative : you can create a TreeMap<Integer,List<Shoe>> (a sorted map that maps keys to a list to resolve key collisions) using the same comparator, that way even if you insert new shoes, it stays ordered, but it looks outside of the level of your problem.

如果您要使用Map进行存储并且密钥可能相同,请尝试使用Google番石榴Multimap

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