简体   繁体   English

hashmap中每个键的值相同

[英]Same values for every key in hashmap

I'll start with my code 我将从我的代码开始

 public void simulateSale(List<IceCream> dailyIceCreamStock) {
    date = LocalDate.now().minusDays(6);
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd");

    for (int i = 0; i < timeInterval; i++) {
        for(IceCream iceCream: dailyIceCreamStock){
            iceCream.setSoldCount(rand.nextInt(IceCream.AMOUNT + 1));
            iceCream.setStockDate(date);
        }

        //Every day should have different ArrayList of values
        this.weeklyStats.put(date.toString(fmt), dailyIceCreamStock);
        date = date.plusDays(1);
    }

Problem is on this line: this.weeklyStats.put(date.toString(fmt), dailyIceCreamStock); 问题在于这一行: this.weeklyStats.put(date.toString(fmt), dailyIceCreamStock);

As you can see, I'm adding randomly generated values to the hashmap of type: 如您所见,我将随机生成的值添加到类型的hashmap中:

Map<String, List<IceCream>> weeklyStats

Problem is, that when I iterate this hashmap, every key has the same List of value. 问题是,当我迭代这个hashmap时,每个键都有相同的List List值。 Output looks like this: 输出如下:

[11, 3, 11, 12, 20]
[11, 3, 11, 12, 20]
[11, 3, 11, 12, 20]
[11, 3, 11, 12, 20]
[11, 3, 11, 12, 20]
[11, 3, 11, 12, 20]
[11, 3, 11, 12, 20]

Desired output is to have random values in every of this list. 期望的输出是在每个列表中具有随机值。 I guess, there's some problem with scope, which I don't understand 我想,范围存在一些问题,我不明白

You are adding the same List instance multiple times to the Map. 您正在多次向Map添加相同的List实例。 You should create copies of the List in order to have distinct values in your Map : 您应该创建List的副本,以便在Map中具有不同的值:

for (int i = 0; i < timeInterval; i++) {
    List<IceCream> copy = new ArrayList<>(dailyIceCreamStock); // create a copy
    for(IceCream iceCream: copy){ // modify the elements of the copy
        iceCream.setSoldCount(rand.nextInt(IceCream.AMOUNT + 1));
        iceCream.setStockDate(date);
    }

    //Every day should have different ArrayList of values
    this.weeklyStats.put(date.toString(fmt), copy); // put the copy in the Map
    date = date.plusDays(1);
}

EDIT: 编辑:

Actually, that's not enough, you should create copies of the IceCream instances too. 实际上,这还不够,你也应该创建IceCream实例的副本。 Otherwise all Lists will be different instances, but the would still contain the same IceCream objects. 否则所有列表将是不同的实例,但仍然包含相同的IceCream对象。

for (int i = 0; i < timeInterval; i++) {
    List<IceCream> copy = new ArrayList<>();
    for(IceCream iceCream: dailyIceCreamStock){ 
        IceCream newIC = new IceCream(); // not sure if you want to copy any
                                         // data from the original IceCream
        newIC.setSoldCount(rand.nextInt(IceCream.AMOUNT + 1));
        newIC.setStockDate(date);
        copy.add(newIC); // add a new IceCream instance to the new List
    }

    //Every day should have different ArrayList of values
    this.weeklyStats.put(date.toString(fmt), copy); 
    date = date.plusDays(1);
}

Every iteration, you modify the same List<IceCream> dailyIceCreamStock , so all keys in your Map point to the same list. 每次迭代,您都修改相同的List<IceCream> dailyIceCreamStock ,因此Map所有键都指向同一个列表。

You probably want to initialize and reference a new, deep-copy of your List at every iteration and put that in your Map instead, after the random mutations are performed. 您可能希望在每次迭代时初始化并引用List的新的深层副本,并在执行随机变换后将其放入Map中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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