197 * For example, to sort a collection of {@code String} based on the length |
197 * For example, to sort a collection of {@code String} based on the length |
198 * and then case-insensitive natural ordering, the comparator can be |
198 * and then case-insensitive natural ordering, the comparator can be |
199 * composed using following code, |
199 * composed using following code, |
200 * |
200 * |
201 * <pre>{@code |
201 * <pre>{@code |
202 * Comparator<String> cmp = Comparator.comparing(String::length) |
202 * Comparator<String> cmp = Comparator.comparingInt(String::length) |
203 * .thenComparing(String.CASE_INSENSITIVE_ORDER); |
203 * .thenComparing(String.CASE_INSENSITIVE_ORDER); |
204 * }</pre> |
204 * }</pre> |
205 * |
205 * |
206 * @param other the other comparator to be used when this comparator |
206 * @param other the other comparator to be used when this comparator |
207 * compares two objects that are equal. |
207 * compares two objects that are equal. |
268 /** |
268 /** |
269 * Returns a lexicographic-order comparator with a function that |
269 * Returns a lexicographic-order comparator with a function that |
270 * extracts a {@code int} sort key. |
270 * extracts a {@code int} sort key. |
271 * |
271 * |
272 * @implSpec This default implementation behaves as if {@code |
272 * @implSpec This default implementation behaves as if {@code |
273 * thenComparing(comparing(keyExtractor))}. |
273 * thenComparing(comparingInt(keyExtractor))}. |
274 * |
274 * |
275 * @param keyExtractor the function used to extract the integer sort key |
275 * @param keyExtractor the function used to extract the integer sort key |
276 * @return a lexicographic-order comparator composed of this and then the |
276 * @return a lexicographic-order comparator composed of this and then the |
277 * {@code int} sort key |
277 * {@code int} sort key |
278 * @throws NullPointerException if the argument is null. |
278 * @throws NullPointerException if the argument is null. |
279 * @see #comparing(ToIntFunction) |
279 * @see #comparingInt(ToIntFunction) |
280 * @see #thenComparing(Comparator) |
280 * @see #thenComparing(Comparator) |
281 * @since 1.8 |
281 * @since 1.8 |
282 */ |
282 */ |
283 default Comparator<T> thenComparing(ToIntFunction<? super T> keyExtractor) { |
283 default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) { |
284 return thenComparing(comparing(keyExtractor)); |
284 return thenComparing(comparingInt(keyExtractor)); |
285 } |
285 } |
286 |
286 |
287 /** |
287 /** |
288 * Returns a lexicographic-order comparator with a function that |
288 * Returns a lexicographic-order comparator with a function that |
289 * extracts a {@code long} sort key. |
289 * extracts a {@code long} sort key. |
290 * |
290 * |
291 * @implSpec This default implementation behaves as if {@code |
291 * @implSpec This default implementation behaves as if {@code |
292 * thenComparing(comparing(keyExtractor))}. |
292 * thenComparing(comparingLong(keyExtractor))}. |
293 * |
293 * |
294 * @param keyExtractor the function used to extract the long sort key |
294 * @param keyExtractor the function used to extract the long sort key |
295 * @return a lexicographic-order comparator composed of this and then the |
295 * @return a lexicographic-order comparator composed of this and then the |
296 * {@code long} sort key |
296 * {@code long} sort key |
297 * @throws NullPointerException if the argument is null. |
297 * @throws NullPointerException if the argument is null. |
298 * @see #comparing(ToLongFunction) |
298 * @see #comparingLong(ToLongFunction) |
299 * @see #thenComparing(Comparator) |
299 * @see #thenComparing(Comparator) |
300 * @since 1.8 |
300 * @since 1.8 |
301 */ |
301 */ |
302 default Comparator<T> thenComparing(ToLongFunction<? super T> keyExtractor) { |
302 default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) { |
303 return thenComparing(comparing(keyExtractor)); |
303 return thenComparing(comparingLong(keyExtractor)); |
304 } |
304 } |
305 |
305 |
306 /** |
306 /** |
307 * Returns a lexicographic-order comparator with a function that |
307 * Returns a lexicographic-order comparator with a function that |
308 * extracts a {@code double} sort key. |
308 * extracts a {@code double} sort key. |
309 * |
309 * |
310 * @implSpec This default implementation behaves as if {@code |
310 * @implSpec This default implementation behaves as if {@code |
311 * thenComparing(comparing(keyExtractor))}. |
311 * thenComparing(comparingDouble(keyExtractor))}. |
312 * |
312 * |
313 * @param keyExtractor the function used to extract the double sort key |
313 * @param keyExtractor the function used to extract the double sort key |
314 * @return a lexicographic-order comparator composed of this and then the |
314 * @return a lexicographic-order comparator composed of this and then the |
315 * {@code double} sort key |
315 * {@code double} sort key |
316 * @throws NullPointerException if the argument is null. |
316 * @throws NullPointerException if the argument is null. |
317 * @see #comparing(ToDoubleFunction) |
317 * @see #comparingDouble(ToDoubleFunction) |
318 * @see #thenComparing(Comparator) |
318 * @see #thenComparing(Comparator) |
319 * @since 1.8 |
319 * @since 1.8 |
320 */ |
320 */ |
321 default Comparator<T> thenComparing(ToDoubleFunction<? super T> keyExtractor) { |
321 default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) { |
322 return thenComparing(comparing(keyExtractor)); |
322 return thenComparing(comparingDouble(keyExtractor)); |
323 } |
323 } |
324 |
324 |
325 /** |
325 /** |
326 * Returns a comparator that imposes the reverse of the <em>natural |
326 * Returns a comparator that imposes the reverse of the <em>natural |
327 * ordering</em>. |
327 * ordering</em>. |
482 * @return a comparator that compares by an extracted key |
482 * @return a comparator that compares by an extracted key |
483 * @see #comparing(Function) |
483 * @see #comparing(Function) |
484 * @throws NullPointerException if the argument is null |
484 * @throws NullPointerException if the argument is null |
485 * @since 1.8 |
485 * @since 1.8 |
486 */ |
486 */ |
487 public static <T> Comparator<T> comparing(ToIntFunction<? super T> keyExtractor) { |
487 public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) { |
488 Objects.requireNonNull(keyExtractor); |
488 Objects.requireNonNull(keyExtractor); |
489 return (Comparator<T> & Serializable) |
489 return (Comparator<T> & Serializable) |
490 (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); |
490 (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); |
491 } |
491 } |
492 |
492 |
503 * @return a comparator that compares by an extracted key |
503 * @return a comparator that compares by an extracted key |
504 * @see #comparing(Function) |
504 * @see #comparing(Function) |
505 * @throws NullPointerException if the argument is null |
505 * @throws NullPointerException if the argument is null |
506 * @since 1.8 |
506 * @since 1.8 |
507 */ |
507 */ |
508 public static <T> Comparator<T> comparing(ToLongFunction<? super T> keyExtractor) { |
508 public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) { |
509 Objects.requireNonNull(keyExtractor); |
509 Objects.requireNonNull(keyExtractor); |
510 return (Comparator<T> & Serializable) |
510 return (Comparator<T> & Serializable) |
511 (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); |
511 (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); |
512 } |
512 } |
513 |
513 |
524 * @return a comparator that compares by an extracted key |
524 * @return a comparator that compares by an extracted key |
525 * @see #comparing(Function) |
525 * @see #comparing(Function) |
526 * @throws NullPointerException if the argument is null |
526 * @throws NullPointerException if the argument is null |
527 * @since 1.8 |
527 * @since 1.8 |
528 */ |
528 */ |
529 public static<T> Comparator<T> comparing(ToDoubleFunction<? super T> keyExtractor) { |
529 public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) { |
530 Objects.requireNonNull(keyExtractor); |
530 Objects.requireNonNull(keyExtractor); |
531 return (Comparator<T> & Serializable) |
531 return (Comparator<T> & Serializable) |
532 (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); |
532 (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); |
533 } |
533 } |
534 } |
534 } |