Eran Gabriel
Eran Gabriel

Reputation: 11

How to check if a list is a key in hashmap?

I have a map from List to String (keys are lists). the keys are converted from int arrays to lists. adding a pair example:

int[] arr = { 1, 2, 3, 4, 5 };
my_map.put(Arrays.asList(arr), "12345");

Now when i check if my_map contains some other list, i will always get null, example:

int[] test_arr = { 1, 2, 3, 4, 5 };

if (my_map.get(Arrays.asList(test_arr)) != null) { // always null!

// do something

}

I know what the problem is: it's comparing the addresses of the lists, and NOT the values!

How can i compare those lists values ?

Upvotes: 1

Views: 507

Answers (4)

erickson
erickson

Reputation: 269817

The List objects that you are creating only contain one element, an object of type int[]. int[] uses identity to test equality, not array content. Instead, convert the array content to a list with equivalent content:

static List<Integer> toList(int[] arr) {
    return IntStream.of(arr).boxed().collect(Collectors.toList());
}

Upvotes: 0

Traian GEICU
Traian GEICU

Reputation: 1786

My advice is to change a little bit the storage in order to have the same functionality. Means to check if a predefined list is within the stucture. You do not need a Map since it's not required to add values, just use the values within List. Further more use Objects and not primitives. Briefly a List of Lists will fit well in this case. Notice, the order of elements in List matters. If do not care order you could just sort both lists before compare.

public class TestListArr {

    public static void main(String[] args) {
    
        List<List<Integer>> list = new ArrayList<>();
        Integer[] arr = { 1, 2, 3, 4, 5 };
        //just check it
        //int[] arr1 = {1,2,3};
        //Arrays.asList(arr1).forEach(System.out::println);
        //Arrays.asList(arr).forEach(System.out::println);
        list.add(Arrays.asList(arr));
        Integer[] test_true = { 1, 2, 3, 4, 5 };
        System.out.println(check(list,test_true));
        Integer[] test_false = { 1, 2, 3, 4 };
        System.out.println(check(list,test_false));
        //direct test
        //System.out.println(Arrays.asList(arr).equals(Arrays.asList(test_true)));
    }
    
    public static boolean check(List<List<Integer>> list, Integer[] test)
    {
        for(List<Integer> lst:list)
        {
            if(lst.equals(Arrays.asList(test)))
                return true;
        }
        return false;
    }

}

Output

true
false

Upvotes: 0

klonq
klonq

Reputation: 3607

Some map implementations allow you to instantiate your map with a Comparator (TreeMap for example).

This way you can provide your own implementation to determine if the Keys are equal. Your comparator function returns an int with a value that is greater than zero, less than zero, or zero if the two values are equal.

Upvotes: 0

SgtOmer
SgtOmer

Reputation: 215

The problem you mentioned is correct, I’ll suggest replacing the key value to be string to list. But if you insist for your reasons you can envelope the list implementation with your own class and override the equal to function to be based on the values and not the address.

Upvotes: 1

Related Questions