0

I have a program that adds the name of the donors as key in the HashMap with their Cities(ArrayList of city) as value. 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);

    }

}
5
  • 3
    Can you explain what's the problem? What's not working? Commented Jan 13, 2020 at 12:33
  • You might want to use a set instead of a list for the cities - at least that's what I'd make of "if he donor is donating from a different city then I need to update the cities arraylist in the map", i.e. you don't seem to want duplicate cities on the list/collection. Commented Jan 13, 2020 at 12:38
  • 1
    Side notes: you should try to use interfaces where possible, so Map<String, List<String>> instread of HashMap<String, ArrayList<String>> etc. Also note that since get() will already return a value type as defined by the generics (in your case ArrayList<String>) those casts like in (ArrayList<String>)donorMap.get(name) aren't necessary. Commented Jan 13, 2020 at 12:40
  • And here's another guess at what might be wrong: cu.nameKeyChecker(donorName,donorCity) - here you're reusing the same list for every donor so in the end they'll all share the same list of cities. Instead either replace donorCity with new ArrayList<>() in that call - or better create a new list in nameKeyChecker(...) when you need to put one and the list that's passed in is null. Commented Jan 13, 2020 at 12:43
  • @Thomas thank you so much. It was really helpful. I understood and I will try again Commented Jan 13, 2020 at 18:55

2 Answers 2

2

You've made this way more complicated than it needs to be.

Given: 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It was helpful. I will try again, with this logic
1

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


  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.

1 Comment

This was easiest to understand as per my knowledge. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.