0

I am new to Java and am trying to take the below working code and convert my ArrayList to a Hashmap. My confusion comes in because they are so fundamentally different. Since the hashmap using key/value pairs I am not quite understanding I do this given the program I have working already. Sorry if it is a stupid question, I think I am confused about what I need to do.

This is the class where I am using the ArrayList:

Thank you for any help.

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

//new class ArrayMessage
public class ArrayMessage {

//new method shoutOutCannedMethod returning a String
public String shoutOutCannedMessage() {

    // create some variables
    int arraySize = 10;
    String displayUserMessage = "";
    String userMessage = "";
    String goAgain = "yes";

    // setup scanner to store input
    Scanner input = new Scanner(System.in);

    // create arrayList
    ArrayList<String> message = new ArrayList<String>();

    // start loop
    while (!goAgain.equals("no")) {
        // clear out arrayList
        message.clear();

        // ask the user for 10 messages as long as the counter is less than
        // the size of the array
        for (int counter = 0; counter < arraySize; counter++) {
            System.out.printf(counter + 1 + ": Please enter a message: ");
            // save user message to userMessage
            userMessage = input.nextLine();
            // add users message to arraylist
            message.add(userMessage);

        }

        // ask the user if they want to to load different messages into
        // arraylist
        System.out.print("Messages have been loaded? Would you like to reload? Type 'yes' or 'no': ");
        goAgain = input.nextLine(); // store input
    }
    // ask the user to choose which message they want displayed
    System.out.print("Please enter the number of the message you would like displayed: ");
    userMessage = input.nextLine();
    // store users message into variable to be used later
    displayUserMessage = message.get(Integer.parseInt(userMessage) - 1);

    input.close();
    // return userMessage
    return displayUserMessage;

}

}

This is my main class:

public class ShoutBox {

//main method
public static void main(String[] args) {

    //call ArrayMessage class 
    ArrayMessage myMessage = new ArrayMessage();
    //call shoutOutCannedMessage method 
    String userMessage = myMessage.shoutOutCannedMessage();
    // display message selected by user
    System.out.printf("Your selected value is " + userMessage + "\n");


}

}
11
  • 6
    Before you attempt to convert between two orthogonal data structures, you should answer the question why this is a thing you want to do. I'm not sure what you're attempting to accomplish by doing this. Commented Jun 29, 2016 at 23:00
  • Not sure why you would want to do this, but it seems to be the key, value that is worrying you... Take a look at HashSet. It is like a HashMap but without the key, just the value. Commented Jun 29, 2016 at 23:03
  • Are you asking us what you want to do? Commented Jun 29, 2016 at 23:05
  • I have to do this for school so I can learn HashMaps. Otherwise it is fine the way it is. That is why I am trying to understand. :/ Commented Jun 30, 2016 at 22:25
  • @shmosel, I'm asking how to do it. Commented Jun 30, 2016 at 22:57

2 Answers 2

2

I would assume you would use the index of the element in the ArrayList as the key, and the actual element in the array as the value in the HashMap.

Hope that helps!

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

Comments

0

An ArrayList is, as the name implies, a list of Elements (that internally uses an array, that gets resized sometimes).

A HashMap on the other hand, is a mapping between elements and keys. This means: you got no list, but a list of keys, that points each to one element (internal representation is a bit more difficult). So this is NOT a simple list and shall not be used for listing something, but creating links between objects.

For example, if you want to assign a Player object of a game to the chat name of the player, you can create a HashMap, that holds Strings as keys and Players as objects like this:

Ann -> Player instance of Ann
Bob -> Player instance of Bob
Clara -> Player instance of Clara
...

This is useful for example, when you create a chat command that targets a player:

/teleportto Clara

Now you can get the player object of Clara like this:

playerHashMap.get("Clara") where clara is the argument taken of the chat and this call will return the player instance of Clara.

So do not replace a list with a map, those are different data structures.

Comments

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.