简体   繁体   中英

add multiple values to key in hashtable

I need help with adding description to roomType in code below. Now I can add only one word to roomType , but i want to be able to add multiple words such as "double suite room" .

    Scanner input = new Scanner(System.in);
    Map<String, String> rooms = new HashMap<String, String>();
    String roomNumber = "0";
    String roomType = null;
    System.out.println( "Enter room numbers and type : [ Enter  'stop' to quit ]" );

    while(!roomNumber.equals("stop")){
        input.useDelimiter(", *");
        roomNumber = input.next();

        if(roomNumber.equals("stop")){
            break;
        }
        roomType = input.next();

        rooms.put(roomNumber, roomType);
    }

I also tried this approach:

    Map<String, List<String>> rooms = new HashMap<String, List<String>>();
    List<String> roomTypeList= new ArrayList<String>();
    List<String> description= new ArrayList<String>();

    String roomNumber = "0";
    String roomType = null;
    System.out.println( "Enter room numbers and type : [ Enter  'stop' to quit ]" );

    while(!roomNumber.equals("stop")){
        roomNumber = input.next();

        if(roomNumber.equals("stop")){
            break;
        }
        roomType = input.next();
        description.add(roomType);
        rooms.put(roomNumber, description);
    }

But scanner is not working properly this way. Thanks PS also any other remarks are welcome

It looks like you want to change the scanners default delimiter from a space to something like comma space.

// Set delimiters to space and comma.
//  ", *" tells Scanner to match a comma and zero or more spaces as
// delimiters.
input.useDelimiter(", *");

Right now, when you enter "double suite room" next should pick up double, then when called again will pick up suite etc.. by altering the delimiter to "," you can enter as many spaces as you like until you hit a comma, then it will stop accepting input.

Make sense?

I'm not sure what kind of description you want to use but i'd wrap the type and the description in some object/enum.

Perhaps something like

public enum RoomType {
    SOME_ROOM_TYPE("description of first type"),
    SOME_OTHER_TYPE("description of other type");

    String description;

    public RoomType(String description) {
        this.description = description;
    } 

    public static RoomType convert(String original) {
         // some code for converting the string from the scanner, 
         // maybe Enum.valueOf(...)
    }
}

And then in your scanner loop:

rooms.put(roomNumber, RoomType.convert(description));

I recommend using the MultiValueMap class from the Apache Commons Collections library .

// create multimap to store key and values
MultiMap multiMap = new MultiValueMap();

// put multiple values for "key" 
multiMap.put("key", "Apple");
multiMap.put("key", "Aeroplane");
multiMap.put("key", "Hello World");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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