0

I need my HashMap to print out alphabetically. E.g Im making restaurant menu, with names of dishes and price. I'm still learning these things but so far i realized that i will need HashMap due to fact that i will have certain item with both name and price. Can i use something else, if yes what, if not how can i print just once for instance from avocado to zest of Lemon e.g. regardless of price. I've seen some workarounds including integers but not with names.

Any help would be appreciated.

Thanks

1 Answers1

1

Just use tree map, it will sort items by natural order if their classes implement Comparable or if not you can specify Comparator, sample code:

package com.company;

import java.util.Map;

public class Main {

    public static void main(String[] args) {

        Map<String, Integer> map = new TreeMap<>();
        map.put("Pizza", 20);
        map.put("Spaghetti", 20);
        map.put("Ice Cream", 20);

        for (String s : map.keySet())
            System.out.println(s);
    }
}

Output:

Ice Cream

Pizza

Spaghetti

whatamidoingwithmylife
  • 1,119
  • 1
  • 16
  • 35
  • Thanks this did it, however since i need to print out food and price, i erased for loop and just added to println map(in my case food). and i got {avocado-212,.....,zest of lemon-1}. – Just a newbie Dec 04 '17 at 23:31
  • sure! :D 1 more thing: just how would you print only price in your case. if we change spagetti to 21, ice cream to 22. and that output is: Pizza Spaghetti Ice Cream (20,21,22) – Just a newbie Dec 04 '17 at 23:34
  • Simple, just do map.values() and it will return list of prices (all 20's in my example). I will post code sample in a moment. – whatamidoingwithmylife Dec 04 '17 at 23:36
  • You are awesome! :D However now i notice that im getting errors (not errors but wrong value) if the price is 0.20 or if second value is 01 for lets say my numbers in catalogue(this wasnt original question, this is if pizza is 20, and her catalogue num is 001.) if you dont know its totally ok you helped alot!:) – Just a newbie Dec 04 '17 at 23:48
  • Real simple once again :D Replace every "Integer" in your code with "Double" and change your prices from 20,21,22 to 20.0,21.0,22.0 OR 20d,21d,22d. – whatamidoingwithmylife Dec 04 '17 at 23:51
  • that would deal for doubles indeed. but how would you deal with (foodmap.put("avocado " , 051); or if you run in your case mapa.put("Pizza", 051); it throws a 41. wtf :D – Just a newbie Dec 04 '17 at 23:58
  • That's also simple, note that you can't put leading 0 in front of a number in Java as it will be treated as Octal number. But you can do that with simple change to the code, just tell me how many zeros you want ? (051 in octal is 41 in decimal that's why you're getting that result). – whatamidoingwithmylife Dec 05 '17 at 00:02
  • three zeros. and it goes to 9999, from 0001-9999, however smallest one is 0010-its ten. but count it as 3. – Just a newbie Dec 05 '17 at 00:04
  • Is this what you wanted: https://pastebin.com/LmS9BYdA ? Also don't forget to click the green arrow for poooooooooooints. – whatamidoingwithmylife Dec 05 '17 at 00:09
  • Fair play can you send me a message i would like to ask you something :) – Just a newbie Dec 05 '17 at 16:58
  • @Justanewbie you can't send messages on Stackoverflow. – whatamidoingwithmylife Dec 05 '17 at 17:03
  • no other way i can contact you? is it allowed even ? :D – Just a newbie Dec 05 '17 at 18:00