HashMap: containsKey () Isn't it true when it should be?

I am currently dropping an obfuscation program for school homework. I am trying to get a program to read a file, and then create a new file that replaces each letter in the file with some corresponding value that I am extracting from HashMap. I created a whole bunch of keys and values, but later in the process of writing a new file, I tried to check if the card contains a key before adding a new one String. The characters to be checked are actually in the file that I am reading for verification, and the file is being read correctly. However, this fails with the encryptionDict.containsKey()(my hashmap) method .

I hope some Java specialist helps me figure this out! I'm pretty clueless, I'm more of a C and D guy. The only thought that struck me was that it would be something like Strings, where "foo" != "foo". But characters are not objects.

The code is in the pastebine below, the key parts to look at are the class constructor, the encryption method and the method initDictionary, can someone tell me why it HashMap<char, String>is invalid because I have to use the object?

Code: http://pastebin.com/NcHTHPfw

+5
source share
4 answers
 private HashMap<char [], String> initDictionary() {
                HashMap<char [], String> d = new HashMap<char [], String>();

                d.put("a".toCharArray(), "!\"#¤");
                d.put("b".toCharArray(), "¤#\"!");
                d.put("c".toCharArray(), "\"#¤%");
                d.put("d".toCharArray(), "%¤#\"");
                d.put("e".toCharArray(), "#¤%&");
                d.put("f".toCharArray(), "&%¤#");
                d.put("g".toCharArray(), "¤%&/");
                d.put("h".toCharArray(), "/&%¤");
                d.put("i".toCharArray(), "%&/(");
                d.put("j".toCharArray(), "(/&%");
                d.put("k".toCharArray(), "&/()");
                d.put("l".toCharArray(), ")(/&");
                d.put("m".toCharArray(), "/()=");
                d.put("n".toCharArray(), "=)(/");
                d.put("o".toCharArray(), "()=?");
                d.put("p".toCharArray(), "?=)(");
                d.put("q".toCharArray(), ")=?!");
                d.put("r".toCharArray(), "!?=)");
                d.put("s".toCharArray(), "=?!\"");
                d.put("t".toCharArray(), "\"!?=");
                d.put("u".toCharArray(), "?!\"#");
                d.put("v".toCharArray(), "#\"!?");
                d.put("w".toCharArray(), ";:*^");
                d.put("x".toCharArray(), "^*:;");
                d.put("y".toCharArray(), ":*^>");
                d.put("z".toCharArray(), ">^*:");
// etc.

. - Java, Array equals() hashCode().

- , , , equals() . HashMap, , , . , , , char, - - char.

- :

public class Key {
    private final char[] array;

    public Key(final String string) {
        this(string.toCharArray());
    }
    public Key(final char[] array) {
        this.array = array;
    }

    public char[] getArray() {
        return array;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Key key = (Key) o;

        if (!Arrays.equals(array, key.array)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return array != null ? Arrays.hashCode(array) : 0;
    }
}

Map<Key, String> bould a Key Object String, char[]

+6

, char[] , . , String. ​​, char[]

private Map<Character, String> initDictionary() {
Map<Character, String> d = new HashMap<Character, String>();

d.put('a', "!\"#¤");
// etc
+2

Do not use arrays as a key, they are not comparable, therefore, when it compares the value, it does not match

0
source

You are using HashMap<char [], String>. This does not work, since .equalsit is .hashCodenot defined on arrays. This simple test program verifiesthis:

public static void main(String[] args) {
  char[] a = String.valueOf('a').toCharArray();
  char[] b = String.valueOf('a').toCharArray();

  System.out.println(a.hashCode() == b.hashCode());
}

You can use Map<Character, String>for your own purposes. Another solution would be to use the String [MAX_INDEX] array, which is indexed filecontent.charAt(index)- it could be even easier if MAX_INDEX is small (like "z") in your case.

0
source

All Articles