0

I have output from data in ArrayList, I have below two ArrayList, where the price of first ArrayList in second ArrayList. I want to use the price for particular items to calculate the bill with some formula. I want to store the value eg. Apple=200.

Formula: Bill = Apple + (10 * Cake)

I have used HashMap.I have never used HashMap before. Or is there any other way to store values?

    ArrayList<String> name = {Apple, Cake, ice-cream, Milk, Burger, Eggs}
        ArrayList<String> values = {200, 350, 75, 80, 200, 100}
        HashMap<List<String>, ArrayList<String>> nameWithvalues = new HashMap<>();
                nameWithvalues.put(name, values);

Output

nameWithvalues :{[Apple, Cake, ice-cream, Milk, Burger, Eggs]=[200, 350, 75, 80, 200, 100]}
2
  • Map<String, Integer> map = new HashMap<>(); since you want to put each item individually, right? Commented Sep 28, 2020 at 10:31
  • @SSC Yes I want individual values Commented Sep 28, 2020 at 10:39

1 Answer 1

2

You can start a loop and put the values from both the arrayList . Value from first arrayList will be the key and other will be the value

HashMap<String , Integer> nameWithvalues = new HashMap<>();

for(int i=0 ; i < name.length() ; i++ )
 {
  nameWithvalues.put(name.get(i), values.get(i));
 }
Sign up to request clarification or add additional context in comments.

3 Comments

Make sure to check for IndexOutOfBounds exception.
@Umesh Sanwal, Thanks I got values in {{Apple=200, Cake=350}, But how can I use in my Formula? like; int bill = Apple + (10 * Cake); ??
int bill = nameWithvalues.get("Apple") + (10 * nameWithvalues.get("Cake));

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.