1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
5362 * @since 1.5
5363 */
5364 public static int frequency(Collection<?> c, Object o) {
5365 int result = 0;
5366 if (o == null) {
5367 for (Object e : c)
5368 if (e == null)
5369 result++;
5370 } else {
5371 for (Object e : c)
5372 if (o.equals(e))
5373 result++;
5374 }
5375 return result;
5376 }
5377
5378 /**
5379 * Returns {@code true} if the two specified collections have no
5380 * elements in common.
5381 *
5382 * <p>Care must be exercised if this method is used on collections that
5383 * do not comply with the general contract for {@code Collection}.
5384 * Implementations may elect to iterate over either collection and test
5385 * for containment in the other collection (or to perform any equivalent
5386 * computation). If either collection uses a nonstandard equality test
5387 * (as does a {@link SortedSet} whose ordering is not <em>compatible with
5388 * equals</em>, or the key set of an {@link IdentityHashMap}), both
5389 * collections must use the same nonstandard equality test, or the
5390 * result of this method is undefined.
5391 *
5392 * <p>Care must also be exercised when using collections that have
5393 * restrictions on the elements that they may contain. Collection
5394 * implementations are allowed to throw exceptions for any operation
5395 * involving elements they deem ineligible. For absolute safety the
5396 * specified collections should contain only elements which are
5397 * eligible elements for both collections.
5398 *
5399 * <p>Note that it is permissible to pass the same collection in both
5400 * parameters, in which case the method will return {@code true} if and
5401 * only if the collection is empty.
5402 *
5403 * @param c1 a collection
5404 * @param c2 a collection
5405 * @return {@code true} if the two specified collections have no
5406 * elements in common.
5407 * @throws NullPointerException if either collection is {@code null}.
5408 * @throws NullPointerException if one collection contains a {@code null}
5409 * element and {@code null} is not an eligible element for the other collection.
5410 * (<a href="Collection.html#optional-restrictions">optional</a>)
| 1 /*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
5362 * @since 1.5
5363 */
5364 public static int frequency(Collection<?> c, Object o) {
5365 int result = 0;
5366 if (o == null) {
5367 for (Object e : c)
5368 if (e == null)
5369 result++;
5370 } else {
5371 for (Object e : c)
5372 if (o.equals(e))
5373 result++;
5374 }
5375 return result;
5376 }
5377
5378 /**
5379 * Returns {@code true} if the two specified collections have no
5380 * elements in common.
5381 *
5382 * <p>Care must be exercised to ensure that both collections use the
5383 * same membership semantics. The implementation may elect to iterate
5384 * over either collection and test element containment in the other
5385 * collection, or perform any equivalent computation. If the collections
5386 * use different membership semantics, the result of this method is
5387 * undefined.
5388 *
5389 * <p>Care must also be exercised when using collections that have
5390 * restrictions on the elements that they may contain. Collection
5391 * implementations are allowed to throw exceptions for any operation
5392 * involving elements they deem ineligible. For absolute safety the
5393 * specified collections should contain only elements which are
5394 * eligible elements for both collections.
5395 *
5396 * <p>Note that it is permissible to pass the same collection in both
5397 * parameters, in which case the method will return {@code true} if and
5398 * only if the collection is empty.
5399 *
5400 * @param c1 a collection
5401 * @param c2 a collection
5402 * @return {@code true} if the two specified collections have no
5403 * elements in common.
5404 * @throws NullPointerException if either collection is {@code null}.
5405 * @throws NullPointerException if one collection contains a {@code null}
5406 * element and {@code null} is not an eligible element for the other collection.
5407 * (<a href="Collection.html#optional-restrictions">optional</a>)
|