@author pavlo
| 24 | * @author pavlo |
| 25 | */ |
| 26 | public abstract class CollectionUtil { |
| 27 | |
| 28 | /** |
| 29 | * Put all the values of an Iterator into a List |
| 30 | * |
| 31 | * @param <T> |
| 32 | * @param it |
| 33 | * @return |
| 34 | */ |
| 35 | public static <T> List<T> list(Iterator<T> it) { |
| 36 | List<T> list = new ArrayList<>(); |
| 37 | CollectionUtil.addAll(list, it); |
| 38 | return (list); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Add all the items in the array to a Collection |
| 43 | * |
| 44 | * @param <T> |
| 45 | * @param data |
| 46 | * @param items |
| 47 | */ |
| 48 | @SuppressWarnings("unchecked") |
| 49 | public static <T> Collection<T> addAll(Collection<T> data, T... items) { |
| 50 | data.addAll(Arrays.asList(items)); |
| 51 | return (data); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Add all of the items from the Iterator into the given collection |
| 56 | * |
| 57 | * @param <T> |
| 58 | * @param data |
| 59 | * @param items |
| 60 | */ |
| 61 | public static <T> Collection<T> addAll(Collection<T> data, Iterator<T> items) { |
| 62 | while (items.hasNext()) { |
| 63 | data.add(items.next()); |
| 64 | } |
| 65 | return (data); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param <T> |
| 70 | * @param <U> |
| 71 | * @param map |
| 72 | * @return |
| 73 | */ |
| 74 | public static <T, U extends Comparable<U>> T getGreatest(Map<T, U> map) { |
| 75 | T max_key = null; |
| 76 | U max_value = null; |
| 77 | for (Map.Entry<T, U> e : map.entrySet()) { |
| 78 | T key = e.getKey(); |
| 79 | U value = e.getValue(); |
| 80 | if (max_value == null || value.compareTo(max_value) > 0) { |
| 81 | max_value = value; |
| 82 | max_key = key; |
| 83 | } |
nothing calls this directly
no outgoing calls
no test coverage detected