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
    `Arrays.asList(arr)` is returning an instance of `List` - this is the problem with primitives :/ – MadProgrammer Aug 25 '18 at 03: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 set = new Hash Set(Arrays.as List(arr)); – Rohit Chaurasiya Sep 30 '21 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 keys = new HashSet(Arrays.asList(arr)); } } – Rohit Chaurasiya Sep 30 '21 at 16:50

1 Answers1

-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)).

Sam Orozco
  • 1,258
  • 1
  • 13
  • 27
  • 1
    `new ArrayList<>(Arrays.asList(arr))` just results in a `ArrayList` 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 – MadProgrammer Aug 25 '18 at 03:19
  • Doesn't java auto box the int to and Integer? – Sam Orozco Aug 25 '18 at 03:20
  • 1
    @SamOrozco For single values, yes, but not for entire arrays. – Andreas Aug 25 '18 at 03:21
  • 1
    @SamOrozco `int[]` isn't a primitive, it's a "special" type of object, so, no, auto boxing won't help here – MadProgrammer Aug 25 '18 at 03:22
  • Note that `List extends Collection`. – Code-Apprentice Aug 25 '18 at 03:29
  • Sry guys, I'm just a ten year old learning coding and I don't exactly understand the things your are talking about. well, I guess I understand some, int[], I guess, Arraylists, kinda. It's just that, can you guys explain a bit more clearly? – ChickenSoup439 Aug 25 '18 at 05:20
  • Basically @ChickenSoup439 I mislead you and didn't test my code example before I posted it as the answer. You will find the answer of your question here: https://stackoverflow.com/questions/1073919/how-to-convert-int-into-listinteger-in-java and https://stackoverflow.com/questions/17520964/how-to-create-arraylist-arraylistinteger-from-array-int-in-java – Sam Orozco Aug 25 '18 at 05:53