How to Make Hashmap Work with Arrays as Key

How to make HashMap work with Arrays as key?

You cannot do it this way. Both t and a will have different hashCode() values because the the java.lang.Array.hashCode() method is inherited from Object, which uses the reference to compute the hash-code (default implementation). Hence the hash code for arrays is reference-dependent, which means that you will get a different hash-code value for t and a. Furthermore, equals will not work for the two arrays because that is also based on the reference.

The only way you can do this is to create a custom class that keeps the boolean array as an internal member. Then you need to override equals and hashCode in such a way that ensures that instances that contain arrays with identical values are equal and also have the same hash-code.

An easier option might be to use List<Boolean> as the key. Per the documentation the hashCode() implementation for List is defined as:

int hashCode = 1;
Iterator<E> i = list.iterator();
while (i.hasNext()) {
E obj = i.next();
hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
}

As you can see, it depends on the values inside your list and not the reference, and so this should work for you.

Can an array be used as a HashMap key?

It will have to be the same object. A HashMap compares keys using equals() and two arrays in Java are equal only if they are the same object.

If you want value equality, then write your own container class that wraps a String[] and provides the appropriate semantics for equals() and hashCode(). In this case, it would be best to make the container immutable, as changing the hash code for an object plays havoc with the hash-based container classes.

EDIT

As others have pointed out, List<String> has the semantics you seem to want for a container object. So you could do something like this:

HashMap<List<String>, String> pathMap;

pathMap.put(
// unmodifiable so key cannot change hash code
Collections.unmodifiableList(Arrays.asList("korey", "docs")),
"/home/korey/docs"
);

// later:
String dir = pathMap.get(Arrays.asList("korey", "docs"));

Using array as key for hashmap java

Arrays are not suitable as keys in HashMaps, since arrays don't override Object's equals and hashCode methods (which means two different array instances containing the exact same elements will be considered as different keys by HashMap).

The alternatives are to use a List<String> instead of String[] as the key of the HashMap, or to use a TreeMap<String[]> with a custom Comparator<String[]> passed to the constructor.

Trying to make an array of hashmap keys from an array of hashmap values returning null

Your for-loop doesn't run because you've got a typo - int i=0; i>= n-1; but your i can never be larger than your n-1. Instead:

for (int i=0; i <= n-1; i++) {
for (Map.Entry<String, Integer> entry : itemCount.entrySet()) {

Get keys from hashmap and store in String array using java

You can do it like this.

HashMap<String, String> columnHeaders =
new HashMap<String, String>();
columnHeaders.put("Id", "101");
columnHeaders.put("First Name", "AAA");
columnHeaders.put("Last Name", "BBB");
columnHeaders.put("Country", "CCC");
columnHeaders.put("City", "DDD");
columnHeaders.put("State", "EEE");
columnHeaders.put("Province", "FFF");

String[] keys = columnHeaders.keySet().toArray(String[]::new);

for (String k : keys) {
System.out.println(k);
}

Prints

State
First Name
Country
Id
City
Last Name
Province


How to access an array´s element from hashmap´s values? Java

map.values() returns a Collections<int[]> on which we can stream and get the first element from each array:

int[] firstElements = map.values().stream().mapToInt(value -> value[0]).toArray();

System.out.println(Arrays.toString(firstElements)); //Prints: [1, 150]

Search Hashmap with Integer[] as a key

You have to provide a reference to the key.

If you create a new Integer[]{10, 23}, you will create a different one which has the same value but is not the key.

Do it like this:

public static void main(String[] args) {
Map<Integer[], String> mapka = new HashMap<>();
Integer[] key = new Integer[]{10, 23};
mapka.put(key, "Hello");
System.out.println(mapka.get(key));
}


Related Topics



Leave a reply



Submit