0

I'm new to java and I'm working on a question where I need to output the elements from a HashMap in the order of the input. I understood that HashMap cannot do it and LinkedHashMap was specifically designed to maintain this order. I've implemented the LinkedHashMap in my problem but the output was not following the input sequence. Kindly help me on that. Please find my question and the code below.

The user enters the number of names n and the names of the persons (the names are added into a map with name being the key and the team number he/she belongs to as value). I should print the respective values for the names the user enters.

Input: 4

Aurora

Dumbo

Ariel

Bambi

Output: It should be [1,3,1,2] - the output from the list

But the output I get from the below program is [1,1,2,3]

I've tried to figure out what was my mistake but in vain. Kindly help on this. Thanks.

import java.io.*;
import java.util.*;

public class DonaldPostman {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in); 
        PrintWriter out = new PrintWriter(System.out);
                
        Map<String,Integer> m = new LinkedHashMap<String,Integer>();
        List<Integer> l = new ArrayList<Integer>();
        m.put("Aurora", 1);
        m.put("Ariel", 1);
        m.put("Alice", 1);
        m.put("Phil", 1);
        m.put("Peter", 1);
        m.put("Olaf", 1);
        m.put("Phoebus", 1);
        m.put("Ralph", 1);
        m.put("Robin", 1);
        m.put("Bambi", 2);
        m.put("Belle", 2);
        m.put("Bolt", 2);
        m.put("Mulan", 2);
        m.put("Mowgli", 2);
        m.put("Mickey", 2);
        m.put("Silver", 2);
        m.put("Simba", 2);
        m.put("Stitch", 2);
        m.put("Dumbo", 3);
        m.put("Genie", 3);
        m.put("Jiminy", 3);
        m.put("Kuzko", 3);
        m.put("Kida", 3);
        m.put("Kenai", 3);
        m.put("Tarzan", 3);
        m.put("Tiana", 3);
        m.put("Winnie", 3);

        int n = in.nextInt();
        String[] arr = new String[n];
        for(int i=0;i<n;i++) {
            arr[i] = in.next();
        }
    
        Set ms = (Set) m.entrySet();
        Iterator it = ms.iterator();
        while (it.hasNext()) {
            Map.Entry mapEntry = (Map.Entry) it.next();
            String key =  (String) mapEntry.getKey();
            Integer value = (Integer) mapEntry.getValue();
            for(int i=0;i<arr.length;i++) {
                if(arr[i].equals(key)) {
                    l.add(value);
                }
            }
        }
        System.out.println(l);
    }

}
devReddit
  • 2,696
  • 1
  • 5
  • 20
sdgd
  • 723
  • 1
  • 17
  • 38
  • Why do you think it should be that output? – Oliver Charlesworth Jul 25 '15 at 06:08
  • You add them in the order of 1's follows by 2's followed by 3's. Why would you expect the order to be `1,3,1,2` ? – Peter Lawrey Jul 25 '15 at 06:08
  • the names the user enters can be anything. i want to print the value of the particular name the user enters – sdgd Jul 25 '15 at 06:10
  • Basically you don't want the structure to sort the keys and display you in the order they were inserted? – Rachit Ahuja Jul 25 '15 at 06:14
  • if the user enters the name Aurora , the respective value this name has should be added to the list and similarly it should go on. the values should not be sorted when printing the output – sdgd Jul 25 '15 at 06:17

1 Answers1

0

It appears you expect the numbers to appear in the order you lookup them up. This of course has nothing to do with the order they are stored. e.g.

For example if you ask for Aurora Dumbo Alice and Bolt you are scanning through the collection in order that you added the words to the collection looking for matches. While this is inefficient, it can work if this is what you expected.

However it appears you expected them to be in the order you entered the words. Actually this is much simpler and much more efficient as you can use Map.get(String)

You can replace both your loops with this

// add the lookup value in the order the words are entered
// not the order they appear in the original map.
for(int i = 0; i < n; i++) {
    String name = in.next();
    l.add(map.get(name));
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks. This is what i was actually expecting. i just learned new thing about `Maps` too. `Map.get(String)`. Thanks again! – sdgd Jul 25 '15 at 06:23
  • @Dev Note this is an O(1) operation instead of scanning all the entries which is O(N). i.e. it can be 1000000x faster if you have millions of elements. – Peter Lawrey Jul 25 '15 at 06:27
  • it should be O(n) only right.. because if the value of the n is high.. the time complexity increases right.. please correct me if Im wrong. – sdgd Jul 25 '15 at 06:36
  • @Dev You want it to be `O(N)` where N is the number of words to lookup. This is the fundamental complexity of the problem. What you *don't* want is `O(N * M)` where `M` is the number of elements in the data set. – Peter Lawrey Jul 25 '15 at 06:42
  • Im getting it.. Can you please tell me any website which explains these concepts clearly please. That will be really helpful for me to build my knowledge on these too. – sdgd Jul 25 '15 at 06:57