0

In this program when I run map.get("b"), I get the whole list but I want only value pair which is related to key "b".

    package test1;
    import java.util.*;
    public class Test1 {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);//scanner object
            Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();//map
            List<Integer> times = new ArrayList<Integer>();//arraylist
            
            int exit = 0;
            int stime;
            int etime;
            String [] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
                "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
            for(int x = 0; exit == 0 ; x++){
                    System.out.println("Enter 1 -> ");
                    stime = in.nextInt();
                    System.out.println("Enter 2 --> ");
                    etime = in.nextInt();
                    if(stime == -1 || etime == -1){
                        exit = -1;
                    }
                    times.add(stime);
                    times.add(etime);
                    map.put(letters[x],times);
           }

            System.out.println(map.get("b"));
            
        }
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Sankalpa Wijewickrama
  • 985
  • 3
  • 19
  • 30
  • 2
    You probably want to instantiate your `List times` inside the for loop. Now you're putting all the `stime` and `etime` values into the same list instead of a separate one. – Kayaman Jan 20 '17 at 06:17
  • `times` is a list , inside your `map` ,`Map>` – Pavneet_Singh Jan 20 '17 at 06:17

1 Answers1

1

The issue is that the Arraylist times is initialized outside the for loop. Below should work.

   for(int x = 0; exit == 0 ; x++){
            System.out.println("Enter 1 -> ");
            stime = in.nextInt();
            System.out.println("Enter 2 --> ");
            etime = in.nextInt();
            if(stime == -1 || etime == -1){
                exit = -1;
            }
            List<Integer> times = new ArrayList<Integer>();//arraylist
            times.add(stime);
            times.add(etime);
            map.put(letters[x],times);
   }

Below uses Pair data structure from Apache Commons Lang library:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);//scanner object
    Map<String, Pair> map = new HashMap<>();//map


    int exit = 0;
    int stime;
    int etime;
    String [] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    for(int x = 0; exit == 0 ; x++){
            System.out.println("Enter 1 -> ");
            stime = in.nextInt();
            System.out.println("Enter 2 --> ");
            etime = in.nextInt();
            if(stime == -1 || etime == -1){
                exit = -1;
            }
            map.put(letters[x],Pair.of(stime, etime));
   }

    System.out.println(map.get("b"));
}
Pavan Kumar
  • 4,182
  • 1
  • 30
  • 45
  • Yes it works in that way ! Is there any way to assign the pair of values that i get from map.get("b") to two variables ? – Sankalpa Wijewickrama Jan 20 '17 at 06:37
  • Apache Lang API has an object called [Pair](https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/tuple/Pair.html) which is meant for such purposes. Can you take a look? – Pavan Kumar Jan 20 '17 at 07:12
  • Actually I had updated the answer using the Pair... if you did not take a look yet... And if you find the solution helpful, do remember to accept the answer. That can help others know that the solution is already available. – Pavan Kumar Jan 20 '17 at 10:49
  • In fact how can i import Apache Commons Lang Library ? – Sankalpa Wijewickrama Jan 20 '17 at 11:26
  • If using Maven, you can include the dependency from https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.5 or otherwise, download the jar and add to classpath. – Pavan Kumar Jan 20 '17 at 11:49