1

I already tried this post: Java, Simplified check if int array contains int

and tried this

int[] temp = {3,9,15,21,27,33,39};
HashSet<Integer> otherBy3 = new HashSet<Integer>(Arrays.asList(temp));

based on the post i saw on the above link instructing me to do this:

HashSet<Integer> set= new HashSet<Integer>(Arrays.asList(intArray));
set.contains(intValue)

but I keep getting this error

cannot find symbol
symbol  : constructor HashSet(java.util.List<int[]>)
location: class java.util.HashSet<java.lang.Integer> HashSet<Integer> otherBy3 = new HashSet<Integer>(Arrays.asList(temp));

Any help would be appreciated

Community
  • 1
  • 1
herteladrian
  • 381
  • 1
  • 6
  • 18
  • The first answer on that link points out what happens when `int[]` to `Arrays.asList` (and why it doesn't do what you want)... – Dennis Meng Jan 17 '14 at 07:51

5 Answers5

2

Change

int[] temp = ...

to

Integer[] temp = ...
Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
2
Integer[] temp = {3,9,15,21,27,33,39};
HashSet<Integer> set= new HashSet<Integer>(Arrays.asList(temp));
System.out.println(set.contains(3));
Vinayak Pingale
  • 1,315
  • 8
  • 19
1

Change int to Integer because you are declaring the List with generic as Integer

Integer[] temp = new Integer[] { 3, 9, 15, 21, 27, 33, 39 };
HashSet<Integer> otherBy3 = new HashSet<Integer>(Arrays.asList(temp));
otherBy3.contains(13);
Jagadesh Seeram
  • 2,630
  • 1
  • 16
  • 29
1

Asusming the real problem ou're asking about is "how to find an integer in an int[] array", why not just use Arrays.html#binarySearch. It's a one-liner, very quick O( log n) and is the common way of doing it. The only pre-req is that your list is sorted and yours appears to be (isn't it?).

int[] temp = {3,9,15,21,27,33,39};
int k = 28;    //Not present in temp
if (Arrays.binarySearch(temp, k) > 0) {
    //key found
}
else {
    //key not found - returns -5 (negative indicating not found)
}
wmorrison365
  • 5,995
  • 2
  • 27
  • 40
0

I find it somewhat disturbing that constructing several temporary objects is regarded "shorter" than a simple freaking loop:

 int[] temp = {3,9,15,21,27,33,39};
 boolean found = false;
 for (int i=0; i<temp.length && !found; ++i) {
     found = valueToFind == temp[i];         
 }

Its about the same amount of code as the high overhead approaches.

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • What about a [binary search](http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binarySearch) instead? Takes at most log n. You even get a free impl in `Arrays#binarySearch`. – wmorrison365 Jan 17 '14 at 11:13
  • That requires sorting. Sorting is O(N log N) (at best) vs a plain O(N) for the simple loop. The question is mainly about "short" (in amount of code lines, I assume), and I just put this answer to point out that "just doing it" is about as long as going through lengths to do it as a byproduct of something else. If the aim would be on performance, you'd need a lot more context to select the best method. – Durandal Jan 17 '14 at 11:18
  • OK. The list is sorted in the question but perhaps that's just incidental. – wmorrison365 Jan 17 '14 at 11:37