简体   繁体   English

从 HashMap 返回 ArrayList

[英]Returning ArrayList from HashMap

I have a program that adds the name of the donors as key in the HashMap with their Cities(ArrayList of city) as value.我有一个程序,将捐赠者的姓名添加为 HashMap 中的键,并将他们的 Cities(ArrayList of city) 作为值。 So, I have to take the name of the donor and first check whether this name is available in the map or not , if the name is available and the donor is donating from a different city then I need to update the cities arraylist in the map and if it is a new donor then I need to just add the donor in the map.所以,我必须取捐助者的名字,首先检查这个名字在地图上是否可用,如果这个名字可用并且捐助者是从不同的城市捐赠的,那么我需要更新地图中的城市数组列表如果它是一个新的捐赠者,那么我只需要在地图中添加捐赠者。

if anyone can help me with this problem, I have been facing great crisis because of this.如果有人可以帮助我解决这个问题,我因此一直面临着巨大的危机。 Totally stucked.完全卡住了。

I have attached my code.我附上了我的代码。

import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashMap;

public class MultCityMain {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CollectionUtil cu = new CollectionUtil();
        String donorName,city;
        Scanner sc = new Scanner(System.in);
        ArrayList<String> donorCity = new ArrayList<String>();
        boolean choiceFlag = true;
        while(choiceFlag){
            System.out.println("Enter name");
            donorName = sc.nextLine();
            System.out.println("Enter city name");
            city = sc.nextLine();
            ArrayList<String> newCity = cu.nameKeyChecker(donorName,donorCity);
            newCity.add(city);
            cu.addDonor(donorName, newCity);
            System.out.println("donate again? (1/0)");
            int choice = sc.nextInt(); sc.nextLine();
            if(choice == 1)
                choiceFlag =  true;
            else
                choiceFlag = false;
        }
        System.out.println(CollectionUtil.donorMap);


    }
}
import java.util.ArrayList;
import java.util.HashMap;


public class CollectionUtil {

    static HashMap<String, ArrayList<String>> donorMap = new HashMap<>();

    public ArrayList<String> nameKeyChecker(String name, ArrayList<String> city){
        if(donorMap.containsKey(name))
        return (ArrayList<String>)donorMap.get(name);
        else
            donorMap.put(name, city);
        return (ArrayList<String>)donorMap.get(name);

    }

    public void addDonor(String name, ArrayList<String> city){

        donorMap.put(name, city);

    }

}

You've made this way more complicated than it needs to be.你让这种方式变得比它需要的更复杂。

Given: Map<String, List<String>> donors;鉴于: Map<String, List<String>> donors; representing a donor's name with a list of cities donated to, all you need to add a new city is:用捐赠的城市列表来代表捐赠者的名字,您只需添加一个新城市:

donors.computeIfAbsent(donorName, d -> new ArrayList<String>()).add(newCity);

This code will fetch the existing list, but, if there is no list, it 'computes' a value first (by making a new list).此代码将获取现有列表,但是,如果没有列表,它首先“计算”一个值(通过创建一个新列表)。 Then, that list is returned (be it the newly computed list, or the existing one);然后,返回该列表(无论是新计算的列表,还是现有的列表); so you just add the new city to it.所以你只需将新城市添加到它。

Voila.瞧。 you can get rid of half of your code now;你现在可以去掉一半的代码; you don't need the util class (in general, if you name a class 'util', rethink things), nameKeyChecker is not needed, you don't need the donorCity variable here either.您不需要 util 类(通常,如果您将一个类命名为“util”,请重新考虑一下),不需要 nameKeyChecker,您也不需要这里的donorCity变量。

Definitely the code is much more complicated than it has to be.代码肯定比它必须复杂得多。 Perhaps, you need little more practice on programming.也许,您需要更多的编程练习。

However, if you want to fix you code all you need is addDonor method that looks something like following但是,如果你想修复你的代码,你需要的只是addDonor方法,它看起来像下面这样


  public void addDonor(String name, String city) {
    if (donorMap.containsKey(name)) {
      donorMap.get(name).add(city);
    } else {
      ArrayList<String> newCity = new ArrayList<>();
      newCity.add(city);
      donorMap.put(name, newCity);
    }


  }

You can get rid of nameKeyChecker.您可以摆脱 nameKeyChecker。

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

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