Returns the number of elements in the Collection that match the Object passed. If the Object is null, then the number of null elements is returned. @param c the Collection to search. @param o the Object to search for. @re
(Collection<?> c, Object o)
| 2454 | * @since 1.5 |
| 2455 | */ |
| 2456 | public static int frequency(Collection<?> c, Object o) { |
| 2457 | if (c == null) { |
| 2458 | throw new NullPointerException(); |
| 2459 | } |
| 2460 | if (c.isEmpty()) { |
| 2461 | return 0; |
| 2462 | } |
| 2463 | int result = 0; |
| 2464 | Iterator<?> itr = c.iterator(); |
| 2465 | while (itr.hasNext()) { |
| 2466 | Object e = itr.next(); |
| 2467 | if (o == null ? e == null : o.equals(e)) { |
| 2468 | result++; |
| 2469 | } |
| 2470 | } |
| 2471 | return result; |
| 2472 | } |
| 2473 | |
| 2474 | /** |
| 2475 | * Returns a type-safe empty, immutable {@link List}. |