散列表中每个键的值相同 [英] Same values for every key in hashmap

查看:109
本文介绍了散列表中每个键的值相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以我的代码开始

  public void simulateSale(List< IceCream> dailyIceCreamStock){
date = LocalDate.now()。minusDays(6);
DateTimeFormatter fmt = DateTimeFormat.forPattern(yyyy-MM-dd); (冰淇淋iceCream:dailyIceCreamStock){
iceCream.setSoldCount(rand.nextInt(IceCream.AMOUNT))的

(int i = 0; i< timeInterval; i ++) + 1));
iceCream.setStockDate(date);
}

//每天应该有不同的ArrayList值
this.weeklyStats.put(date.toString(fmt),dailyIceCreamStock);
date = date.plusDays(1);
}

问题出现在这行: this.weeklyStats。 put(date.toString(fmt),dailyIceCreamStock);



正如您所看到的,我将随机生成的值添加到hashmap键入:

 映射< String,List< IceCream>> weeklyStats 

问题是,当我迭代这个散列表时,每个键都有相同的List值。输出如下:

  [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]

期望的输出是在每个列表中都有随机值。
我想,有一些问题与范围,我不明白

解决方案

您添加相同的列表实例多次到Map。你应该创建List的副本,以便在Map中拥有不同的值:

  for(int i = 0; i< ; timeInterval; i ++){
List< IceCream> copy = new ArrayList<>(dailyIceCreamStock); //为(IceCream iceCream:copy)创建一个副本
{//修改副本元素
iceCream.setSoldCount(rand.nextInt(IceCream.AMOUNT + 1));
iceCream.setStockDate(date);
}

//每天应该有不同的ArrayList值
this.weeklyStats.put(date.toString(fmt),copy); //将副本放在地图
date = date.plusDays(1);

编辑:

实际上,这还不够,你还应该创建IceCream实例的副本。否则所有的列表将会是不同的实例,但仍然会包含相同的IceCream对象。

  for(int i = 0; i < timeInterval; i ++){
List< IceCream> copy = new ArrayList<>();
(IceCream iceCream:dailyIceCreamStock){
IceCream newIC = new IceCream(); //不确定是否要从原始IceCream
复制任何
//数据newIC.setSoldCount(rand.nextInt(IceCream.AMOUNT + 1));
newIC.setStockDate(date);
copy.add(newIC); //添加一个新的IceCream实例到新的列表
}

//每天应该有不同的值列表
this.weeklyStats.put(date.toString(fmt) ,复制);
date = date.plusDays(1);
}


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);

As you can see, I'm adding randomly generated values to the hashmap of type:

Map<String, List<IceCream>> weeklyStats

Problem is, that when I iterate this hashmap, every key has the same List of value. 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. You should create copies of the List in order to have distinct values in your 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. Otherwise all Lists will be different instances, but the would still contain the same IceCream objects.

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);
}

这篇关于散列表中每个键的值相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆