Count occurrences of a given number in a list I have written a solution to find the occurrence of a common number in a n lists. I am just concerned if this is the best solution? Please suggest the best way to do it.

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

public class CommonNumberFinder
{

    final static List&lt;Number&gt; list1 = Arrays.asList(new Number[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
    final static List&lt;Number&gt; list2 = Arrays
            .asList(new Number[] { 3, 5, 1, 6, 2, 6, 3, 3, 6, 1, 5, 7, 2, 6, 2, 5, 2, 6, 8, 2 });

    public static void main(String[] args)
    {
        findCountOfNumber(Arrays.asList(new List[] { list1, list2 }), 6);
    }

    private static void findCountOfNumber(final List&lt;List&lt;Number&gt;&gt; lists, final Number num)
    {
        int count = 0;
        for (List&lt;Number&gt; list : lists)
        {
            if (!list.contains(num))
            {
                System.err.printf("Number %d is not common", num);
                return;
            }
        }
        for (List&lt;Number&gt; list : lists)
        {
            for (Number number : list)
            {
                if (number == num)
                    count++;
            }
            System.out.println("List have-" + count);
            count = 0;
        }

    }
}
There are a decent number of reservations I take with this code.    Most IDEs would pick this up, but there are some nuances which you may not have fully grasped.

First, your import of java.util.Collections is superfluous and isn't used anywhere.

Next, your bound on List&lt;Number&gt; seems a bit...overly generic.  Since these lists support Number, they could support BigDecimal and BigInteger, or more conventionally, Float and Double - which poses problems later on.

There's no reason to declare this:

Arrays.asList(new List[]{list1, list2});


Arrays.asList takes a variadic argument, so you can simply list the arguments after it without the need for an explicit array.

Arrays.asList(list1, list2);


You should look to do this for your other lists as well.

The biggest issue as far as a broken implementation that I can see is this:

if (number == num)


This is wrong for a few reasons:


You're using == on objects.
The objects you're performing this == on are not guaranteed to unbox.
Strict equivalence in the face of floating-point values is not entirely guaranteed.


If you wanted to fix this, you'd want to fix the bound on what your method accepts to incorporate Comparable...

private static &lt;T extends Number &amp; Comparable&lt;T&gt;&gt; void findCountOfNumber(final List&lt;List&lt;T&gt;&gt; lists, final T num)


...and simply compare the values together.

for (T number : list) {
    if (number.compareTo(num) == 0) {
        count++;
    }
}


You'll also need to ensure that the types you use for your array and the types that you use for num are homogeneous, since it really wouldn't make sense to try to find a floating-point value in a list of integers.  This is easily accomplished with the new type bounds by simply changing your lists to List&lt;Integer&gt;.

Lastly, let's talk about the output.  This is incredibly cryptic:

List have-1
List have-5


What you're saying is, the first list has one occurrence of the number 6, and the second list has five occurrences of the number 6.  But, what you're searching for and which list you're talking about aren't clear.

You should look to express that concisely.  Perhaps instead of using an enhanced-for you should look to use a conventional for, which states clearly what list has what occurrences of what number.