0
import java.util.HashMap;

public class Chapter1_Problem_1_1 {

    public static void main(String[] args) {

        String str = "bacdee";

        int j = 0;
        for(int i=0; i< str.length(); i++) {

            char ch = str.charAt(i);
            String s = new String(new char[] {ch});
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            if (map.containsKey(s)) {
                System.out.println("false");
            } else {
                map.put(s, j++);
                System.out.println("true:: " + s);
            }              
        }          
    }      
}

In the above code, I am getting containskey as true for the last char "e" not sure why, it should be false; any ideas why this is happening?

Andrew LaPrise
  • 3,373
  • 4
  • 32
  • 50
sbolla
  • 671
  • 3
  • 22
  • 39
  • 1
    Was it you desire to make a new map each time your loop iterates? It doesn't make sense your if would reach else ever, once it's always a new map you make. Just ran it, false for every char, so it prints true:: 6 times – Leonardo Lana Sep 15 '16 at 17:39
  • Ah!thanks that's it. thanks again! – sbolla Sep 15 '16 at 18:05

0 Answers0