< prev index next >

src/java.base/share/classes/java/util/Collections.java

Print this page
8193031: Collections.addAll is likely to perform worse than Collection.addAll
Reviewed-by: smarks, forax


5381                 iterate = c2;
5382                 contains = c1;
5383             }
5384         }
5385 
5386         for (Object e : iterate) {
5387             if (contains.contains(e)) {
5388                // Found a common element. Collections are not disjoint.
5389                 return false;
5390             }
5391         }
5392 
5393         // No common elements were found.
5394         return true;
5395     }
5396 
5397     /**
5398      * Adds all of the specified elements to the specified collection.
5399      * Elements to be added may be specified individually or as an array.
5400      * The behavior of this convenience method is identical to that of
5401      * {@code c.addAll(Arrays.asList(elements))}, but this method is likely
5402      * to run significantly faster under most implementations.
5403      *
5404      * <p>When elements are specified individually, this method provides a
5405      * convenient way to add a few elements to an existing collection:
5406      * <pre>
5407      *     Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
5408      * </pre>
5409      *
5410      * @param  <T> the class of the elements to add and of the collection
5411      * @param c the collection into which {@code elements} are to be inserted
5412      * @param elements the elements to insert into {@code c}
5413      * @return {@code true} if the collection changed as a result of the call
5414      * @throws UnsupportedOperationException if {@code c} does not support
5415      *         the {@code add} operation
5416      * @throws NullPointerException if {@code elements} contains one or more
5417      *         null values and {@code c} does not permit null elements, or
5418      *         if {@code c} or {@code elements} are {@code null}
5419      * @throws IllegalArgumentException if some property of a value in
5420      *         {@code elements} prevents it from being added to {@code c}
5421      * @see Collection#addAll(Collection)
5422      * @since 1.5
5423      */
5424     @SafeVarargs

5425     public static <T> boolean addAll(Collection<? super T> c, T... elements) {
5426         boolean result = false;
5427         for (T element : elements)
5428             result |= c.add(element);
5429         return result;
5430     }
5431 
5432     /**
5433      * Returns a set backed by the specified map.  The resulting set displays
5434      * the same ordering, concurrency, and performance characteristics as the
5435      * backing map.  In essence, this factory method provides a {@link Set}
5436      * implementation corresponding to any {@link Map} implementation.  There
5437      * is no need to use this method on a {@link Map} implementation that
5438      * already has a corresponding {@link Set} implementation (such as {@link
5439      * HashMap} or {@link TreeMap}).
5440      *
5441      * <p>Each method invocation on the set returned by this method results in
5442      * exactly one method invocation on the backing map or its {@code keySet}
5443      * view, with one exception.  The {@code addAll} method is implemented
5444      * as a sequence of {@code put} invocations on the backing map.
5445      *
5446      * <p>The specified map must be empty at the time this method is invoked,
5447      * and should not be accessed directly after this method returns.  These
5448      * conditions are ensured if the map is created empty, passed directly
5449      * to this method, and no reference to the map is retained, as illustrated




5381                 iterate = c2;
5382                 contains = c1;
5383             }
5384         }
5385 
5386         for (Object e : iterate) {
5387             if (contains.contains(e)) {
5388                // Found a common element. Collections are not disjoint.
5389                 return false;
5390             }
5391         }
5392 
5393         // No common elements were found.
5394         return true;
5395     }
5396 
5397     /**
5398      * Adds all of the specified elements to the specified collection.
5399      * Elements to be added may be specified individually or as an array.
5400      * The behavior of this convenience method is identical to that of
5401      * {@code c.addAll(Arrays.asList(elements))}.

5402      *
5403      * <p>When elements are specified individually, this method provides a
5404      * convenient way to add a few elements to an existing collection:
5405      * <pre>
5406      *     Collections.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
5407      * </pre>
5408      *
5409      * @param  <T> the class of the elements to add and of the collection
5410      * @param c the collection into which {@code elements} are to be inserted
5411      * @param elements the elements to insert into {@code c}
5412      * @return {@code true} if the collection changed as a result of the call
5413      * @throws UnsupportedOperationException if {@code c} does not support
5414      *         the {@code add} operation
5415      * @throws NullPointerException if {@code elements} contains one or more
5416      *         null values and {@code c} does not permit null elements, or
5417      *         if {@code c} or {@code elements} are {@code null}
5418      * @throws IllegalArgumentException if some property of a value in
5419      *         {@code elements} prevents it from being added to {@code c}
5420      * @see Collection#addAll(Collection)
5421      * @since 1.5
5422      */
5423     @SafeVarargs
5424     @SuppressWarnings("varargs")
5425     public static <T> boolean addAll(Collection<? super T> c, T... elements) {
5426         return c.addAll(Arrays.asList(elements));



5427     }
5428 
5429     /**
5430      * Returns a set backed by the specified map.  The resulting set displays
5431      * the same ordering, concurrency, and performance characteristics as the
5432      * backing map.  In essence, this factory method provides a {@link Set}
5433      * implementation corresponding to any {@link Map} implementation.  There
5434      * is no need to use this method on a {@link Map} implementation that
5435      * already has a corresponding {@link Set} implementation (such as {@link
5436      * HashMap} or {@link TreeMap}).
5437      *
5438      * <p>Each method invocation on the set returned by this method results in
5439      * exactly one method invocation on the backing map or its {@code keySet}
5440      * view, with one exception.  The {@code addAll} method is implemented
5441      * as a sequence of {@code put} invocations on the backing map.
5442      *
5443      * <p>The specified map must be empty at the time this method is invoked,
5444      * and should not be accessed directly after this method returns.  These
5445      * conditions are ensured if the map is created empty, passed directly
5446      * to this method, and no reference to the map is retained, as illustrated


< prev index next >