6

I have used Collections.frequency in the past and its worked fine but I'm having problems now that I'm using an int[].

Basically Collections.frequency requires a array but my data is in the form of a int[] so I convert my list but am not getting an results. I think my mistake is in the converting of the list but not sure how to do it.

Here's an example of my problem:

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class stackexample {
    public static void main(String[] args) {
        int[] data = new int[] { 5,0, 0, 1};
        int occurrences = Collections.frequency(Arrays.asList(data), 0);
        System.out.println("occurrences of zero is " + occurrences); //shows 0 but answer should be 2
    }
}

I don't get an error just zero but I get weird data when I try to list the items in Arrays.asList(data), if I just add data directly, it wants to convert my list into collections<?>

Any suggestions?

Lostsoul
  • 25,013
  • 48
  • 144
  • 239

3 Answers3

12

This works:

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class stackexample {
    public static void main(String[] args) {
        List<Integer> values = Arrays.asList( 5, 0, 0, 2 );
        int occurrences = Collections.frequency(values, 0);
        System.out.println("occurrences of zero is " + occurrences); //shows 0 but answer should be 2
    }
}

It's because Arrays.asList isn't giving you what you think it is:

http://mlangc.wordpress.com/2010/05/01/be-carefull-when-converting-java-arrays-to-lists/

You're getting back a List of int [], not int.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thanks so much Duffy, I thought something like this was the problem because I couldn't do a for loop on the converted list. But is it possible to convert my list from the existing format to this? my core data is in int[] and it'll be a bit hard to change the input so I was trying to convert it into a new list instead to do this test. – Lostsoul Apr 13 '12 at 00:28
  • The conversion question has been asked [before](http://stackoverflow.com/q/880581/422353) and there is no one-line solution. I feel the easiest way out of this is rolling your own frequency function that works with `int[]`. – madth3 Apr 13 '12 at 00:39
  • There isn't a one-line solution with the JDK alone, but Guava's [`Ints.asList`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/primitives/Ints.html#asList(int...)) would do the job in one line. – Louis Wasserman Apr 13 '12 at 02:12
4

You problem is from this instruction Arrays.asList(data).

the return of this method is List<int[]> not List<Integer>.

here a correct implementation

    int[] data = new int[] { 5,0, 0, 1};
    List<Integer> intList = new ArrayList<Integer>();
    for (int index = 0; index < data.length; index++)
    {
        intList.add(data[index]);
    }

    int occurrences = Collections.frequency(intList, 0);
    System.out.println("occurrences of zero is " + occurrences);
1

The API expects an Object, and primitive types are not objects. Try this:

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class stackexample {
    public static void main(String[] args) {
        Integer[] data = new Integer[] { 5,0, 0, 1};
        int occurrences = Collections.frequency(Arrays.asList(data), Integer.valueOf(5));
        System.out.println("occurrences of five is " + occurrences); 
    }
}
Amir Afghani
  • 37,814
  • 16
  • 84
  • 124