0
 public class myclass
{
 public static void main(String[] args){
     Scanner scan = new Scanner(System.in);
     int array1[] = new int[4];
     System.out.println("Enter some integers");
     for(int i = 0; i <array1.length; i++){
         array1[i] = scan.nextInt();
 }
 Arrays.sort(array1);
    HashMap<String, Integer> map = new HashMap<>();
    map.put("IdOne", array1[0]);
    map.put("IdOne", array1[1]);
    map.put("IdOne", array1[2]);
    }
}      

I want to be able to print the values that I entered into the array along with a key from the Hashmap. Example output:

Enter some integers

3

5

2

IdOne 2

IdOne 3

IdOne 5

1 Answers1

0

First, get the entries

someMap.entries();

then loop over them

for (Map.Entry entry : someMap.entries()) {
    // do something
}

then print out the key and the value

for (Map.Entry entry : someMap.entries()) {
    System.out.println("key: %s, value: %s\n", entry.getKey(), entry.getValue();
}
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Note that your map is being using incorrectly. Entries in a map have a "key" which is logically the "location" used to access or update a "value". By putting all the values into the same location (`IdOne`) you are assuring that prior values are being overwritten. – Edwin Buck Oct 03 '16 at 23:59
  • Do you have any suggestions for a way to assign one Key to a group of values? Is there anything other than a hashmap that could allow me to do this? – trevor01010010 Oct 04 '16 at 00:12
  • A "map" is a type of math function, defined such that one key only returns one value. What you want is one key that might return one, two, or more values. I would recommend a map that returns a collection, and then add the values to the collection "value" within the map. – Edwin Buck Oct 04 '16 at 00:21