0

So, I have been doing an online code problem on this website called Codewars and I'm having some problem with my code. When I was using the set interface, it gave me an error:

/workspace/java/src/FindOdd.java:16: error: no suitable constructor found for HashSet(List<int[]>)
    Set<Integer> keys = new HashSet<Integer>(Arrays.asList(a));
                        ^
    constructor HashSet.HashSet(Collection<? extends Integer>) is not applicable
      (argument mismatch; inferred type does not conform to upper bound(s)
          inferred: int[]
          upper bound(s): Integer,Object)
    constructor HashSet.HashSet(int) is not applicable
      (argument mismatch; no instance(s) of type variable(s) T exist so that List<T> conforms to int)
  where T is a type-variable:
    T extends Object declared in method <T>asList(T...)
Note: /workspace/java/src/FindOdd.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

(by the way, my code isn't finished, tell me if I have to finish it first or not)

this is my code:

import java.util.*;

public class FindOdd {
  public static int findIt(int[] arr) {
    LinkedHashMap apearanceCount = new LinkedHashMap();

    for(int i = 0; i < arr.length; i++){
      if(apearanceCount.containsKey(arr[i])){
        int amount = Integer.parseInt(apearanceCount.get(arr[i]).toString()) + 1;
        apearanceCount.put(arr[i], amount);
      } else {
        apearanceCount.put(arr[i], 1);
      }
    }

    Set<Integer> keys = new HashSet<Integer>(Arrays.asList(arr));
  }
}

Please help me!

(for people who think this is a duplicate, from what I know, arrays converting to sets isn't the same as converting arrays to lists. If I am wrong, answer in the comments. ;) )

3
  • 3
    Arrays.asList(arr) is returning an instance of List<int[]> - this is the problem with primitives :/ Commented Aug 25, 2018 at 3:14
  • Arrays.as List(arr) accepts a type array, which means all elements used need to be Object types rather than primitives. Use an Integer array instead of int (primitive type): Integer[] arr; it will work like charm public static boolean printer(Integer arr[], int n) { Set<Integer> set = new Hash Set<Integer>(Arrays.as List(arr)); Commented Sep 30, 2021 at 16:49
  • import java.util.*; public class FindOdd { public static int findIt(Integer[] arr) { LinkedHashMap apearanceCount = new LinkedHashMap(); for(int i = 0; i < arr.length; i++){ if(apearanceCount.containsKey(arr[i])){ int amount = Integer.parseInt(apearanceCount.get(arr[i]).toString()) + 1; apearanceCount.put(arr[i], amount); } else { apearanceCount.put(arr[i], 1); } } Set<Integer> keys = new HashSet<Integer>(Arrays.asList(arr)); } } Commented Sep 30, 2021 at 16:50

1 Answer 1

-2

The error is saying the HashSet class doesn't have a constructor that takes the interface List. It does, however, have a constructor for the Collection interface. So try new ArrayList<>(Arrays.asList(arr)).

Sign up to request clarification or add additional context in comments.

7 Comments

new ArrayList<>(Arrays.asList(arr)) just results in a ArrayList<int[]> list, which is the same as Arrays.asList :/ - You need to convert the int values to Integer, either through a second array or directly yo the map/list
Doesn't java auto box the int to and Integer?
@SamOrozco For single values, yes, but not for entire arrays.
@SamOrozco int[] isn't a primitive, it's a "special" type of object, so, no, auto boxing won't help here
Note that List extends Collection.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.