3697 * of <tt>o</tt> 3698 * @param o the object whose frequency is to be determined 3699 * @throws NullPointerException if <tt>c</tt> is null 3700 * @since 1.5 3701 */ 3702 public static int frequency(Collection<?> c, Object o) { 3703 int result = 0; 3704 if (o == null) { 3705 for (Object e : c) 3706 if (e == null) 3707 result++; 3708 } else { 3709 for (Object e : c) 3710 if (o.equals(e)) 3711 result++; 3712 } 3713 return result; 3714 } 3715 3716 /** 3717 * Returns <tt>true</tt> if the two specified collections have no 3718 * elements in common. 3719 * 3720 * <p>Care must be exercised if this method is used on collections that 3721 * do not comply with the general contract for <tt>Collection</tt>. 3722 * Implementations may elect to iterate over either collection and test 3723 * for containment in the other collection (or to perform any equivalent 3724 * computation). If either collection uses a nonstandard equality test 3725 * (as does a {@link SortedSet} whose ordering is not <i>compatible with 3726 * equals</i>, or the key set of an {@link IdentityHashMap}), both 3727 * collections must use the same nonstandard equality test, or the 3728 * result of this method is undefined. 3729 * 3730 * <p>Note that it is permissible to pass the same collection in both 3731 * parameters, in which case the method will return true if and only if 3732 * the collection is empty. 3733 * 3734 * @param c1 a collection 3735 * @param c2 a collection 3736 * @throws NullPointerException if either collection is null 3737 * @since 1.5 3738 */ 3739 public static boolean disjoint(Collection<?> c1, Collection<?> c2) { 3740 /* 3741 * We're going to iterate through c1 and test for inclusion in c2. 3742 * If c1 is a Set and c2 isn't, swap the collections. Otherwise, 3743 * place the shorter collection in c1. Hopefully this heuristic 3744 * will minimize the cost of the operation. 3745 */ 3746 if ((c1 instanceof Set) && !(c2 instanceof Set) || 3747 (c1.size() > c2.size())) { 3748 Collection<?> tmp = c1; 3749 c1 = c2; 3750 c2 = tmp; 3751 } 3752 3753 for (Object e : c1) 3754 if (c2.contains(e)) 3755 return false; 3756 return true; 3757 } 3758 3759 /** 3760 * Adds all of the specified elements to the specified collection. 3761 * Elements to be added may be specified individually or as an array. 3762 * The behavior of this convenience method is identical to that of 3763 * <tt>c.addAll(Arrays.asList(elements))</tt>, but this method is likely 3764 * to run significantly faster under most implementations. 3765 * 3766 * <p>When elements are specified individually, this method provides a 3767 * convenient way to add a few elements to an existing collection: 3768 * <pre> 3769 * Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon"); 3770 * </pre> 3771 * 3772 * @param c the collection into which <tt>elements</tt> are to be inserted 3773 * @param elements the elements to insert into <tt>c</tt> 3774 * @return <tt>true</tt> if the collection changed as a result of the call 3775 * @throws UnsupportedOperationException if <tt>c</tt> does not support | 3697 * of <tt>o</tt> 3698 * @param o the object whose frequency is to be determined 3699 * @throws NullPointerException if <tt>c</tt> is null 3700 * @since 1.5 3701 */ 3702 public static int frequency(Collection<?> c, Object o) { 3703 int result = 0; 3704 if (o == null) { 3705 for (Object e : c) 3706 if (e == null) 3707 result++; 3708 } else { 3709 for (Object e : c) 3710 if (o.equals(e)) 3711 result++; 3712 } 3713 return result; 3714 } 3715 3716 /** 3717 * Returns {@code true} if the two specified collections have no 3718 * elements in common. 3719 * 3720 * <p>Care must be exercised if this method is used on collections that 3721 * do not comply with the general contract for {@code Collection}. 3722 * Implementations may elect to iterate over either collection and test 3723 * for containment in the other collection (or to perform any equivalent 3724 * computation). If either collection uses a nonstandard equality test 3725 * (as does a {@link SortedSet} whose ordering is not <em>compatible with 3726 * equals</em>, or the key set of an {@link IdentityHashMap}), both 3727 * collections must use the same nonstandard equality test, or the 3728 * result of this method is undefined. 3729 * 3730 * <p>Note that it is permissible to pass the same collection in both 3731 * parameters, in which case the method will return {@code true} if and 3732 * only if the collection is empty. 3733 * 3734 * @param c1 a collection 3735 * @param c2 a collection 3736 * @return {@code true} if the two specified collections have no 3737 * elements in common. 3738 * @throws NullPointerException if either collection is {@code null} 3739 * @since 1.5 3740 */ 3741 public static boolean disjoint(Collection<?> c1, Collection<?> c2) { 3742 final boolean c1isSet = c1 instanceof Set; 3743 final Collection<?> contains; 3744 final Collection<?> iterate; 3745 3746 if(c1isSet ^ (c2 instanceof Set)) { 3747 // one is a Set, one isn't. 3748 3749 if(c1.isEmpty() || c2.isEmpty()) { 3750 // One of the collections is empty. Nothing will match. 3751 return true; 3752 } 3753 3754 // Iterate on the one that isn't the Set. 3755 if(c1isSet) { 3756 // c1 has fast contains() 3757 contains = c1; 3758 iterate = c2; 3759 } else { 3760 // c2 has fast contains() 3761 contains = c2; 3762 iterate = c1; 3763 } 3764 } else { 3765 // Neither or both are Sets 3766 int c1Size = c1.size(); 3767 int c2Size = c2.size(); 3768 3769 if((0 == c1Size) || (0 == c2Size)) { 3770 // One of the collections is empty. Nothing will match. 3771 return true; 3772 } 3773 3774 // iterate over smaller collection. 3775 // If c1 contains 3 elements and c2 contains 50 elements and 3776 // assuming contains() requires ceiling(n/2) comparisons then 3777 // checking for all c1 elements in c2 would require 75 comparisons 3778 // vs. all c2 elements in c1 would require 100. 3779 if(c1Size < c2Size) { 3780 contains = c2; 3781 iterate = c1; 3782 } else { 3783 contains = c1; 3784 iterate = c2; 3785 } 3786 } 3787 3788 for (Object e : iterate) { 3789 if (contains.contains(e)) { 3790 // found a common element. Collections are not disjoint. 3791 return false; 3792 } 3793 } 3794 3795 // no common elements were found. 3796 return true; 3797 } 3798 3799 /** 3800 * Adds all of the specified elements to the specified collection. 3801 * Elements to be added may be specified individually or as an array. 3802 * The behavior of this convenience method is identical to that of 3803 * <tt>c.addAll(Arrays.asList(elements))</tt>, but this method is likely 3804 * to run significantly faster under most implementations. 3805 * 3806 * <p>When elements are specified individually, this method provides a 3807 * convenient way to add a few elements to an existing collection: 3808 * <pre> 3809 * Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon"); 3810 * </pre> 3811 * 3812 * @param c the collection into which <tt>elements</tt> are to be inserted 3813 * @param elements the elements to insert into <tt>c</tt> 3814 * @return <tt>true</tt> if the collection changed as a result of the call 3815 * @throws UnsupportedOperationException if <tt>c</tt> does not support |