LangOps { @SuppressWarnings("unchecked") public static <K, V> Map<K, V> arrayToMap(Object... arrays){ return Stream.iterate(0, i->i+2) .limit(arrays.length/2) .map(i->new Object[]{arrays[i], arrays[i+1]}) .collect(Collectors.toMap(array->(K)array[0], array->(V)array[1])); } private  LangOps(); static BinaryOperator<T> throwingMerger(); @SuppressWarnings("unchecked") static Map<K, V> arrayToMap(Object... arrays); @SuppressWarnings("unchecked") static Map<K, V> kvArrayToMap(K[] keys, V[] values); static Map<K, V> toMap(List<V> data, Function<V, K> keyExtractor); static Object[] toArray(Map<?, ?> map); @SuppressWarnings("unchecked") static R[] toArray(List<T> list, Function<? super T, ? extends R> mapper); static Map<Long, R> generateMap(Long count, Function<Long, ? extends R> mapper); static List<R> generateList(int count, Function<Integer, ? extends R> mapper); static List<R> generateList(Long count, Function<Long, ? extends R> mapper); static void splitTaskAndConsume(List<T> taskList, int taskSize, Consumer<List<T>> subTaskDataConsumer, boolean parallel); static void splitTaskAndConsume(List<T> taskList, int taskSize, BiConsumer<List<T>, Integer> subTaskDataConsumer); static void splitTaskAndConsume(List<T> taskList, int taskSize, BiConsumer<List<T>, Integer> subTaskDataConsumer, boolean parallel); static void timeIt(String tag, Integer times, Closure closure); static void ntimesRun(Integer times, Closure closure); static void ntimesRun(Integer times, Consumer<Integer> consumer); static void ntimesRun(String printTimeTag, Integer times, Closure closure); static void ntimesRun(String printTimeTag, Integer times, Consumer<Integer> consumer); static List<R> ntimesMap(Integer times, Function<Integer, R> mapper); static BigDecimal sumBigDecimal(List<T> datas, Function<T, BigDecimal> mapper); @SuppressWarnings("unchecked") static Map<K, List<T>> groupByProperty(List<T> datas, String propName); @SuppressWarnings("unchecked") static Map<K, List<V>> groupByProperty(List<T> datas, String keyProperty, String valueProperty); static Map<K, List<T>> groupBy(List<T> datas, Function<? super T, ? extends K> keyer); static Map<K, List<V>> groupBy(List<T> datas, Function<? super T, ? extends K> keyer, Function<? super T, ? extends V> valuer); static int parseSize(String size); static int parseSize(String size, Integer def); static long timeToSeconds(String time, long def); static long timeToMills(String time, long def); static long timeToMinutes(String time, long def); static long parseTime(String time, long def, BiFunction<Integer, TimeUnit, Long> convert); static Pair<Integer, TimeUnit> parseTimeUnit(String time); static Color parseColor(String color); @SafeVarargs static List<T> asList(T...values);  }
@Test public void testArrayToMap(){ Object[] arrays = new Object[]{"key1", 1, "key2", "value2"}; Map<String, Object> map = LangOps.arrayToMap(arrays); System.out.println("map:"+map); Assert.assertNotNull(map); Assert.assertEquals(Integer.valueOf(1), map.get("key1")); Assert.assertEquals("value2", map.get("key2")); arrays = new Object[]{"key1", 1, "key2", "value2", "key3"}; map = LangOps.arrayToMap(arrays); System.out.println("map:"+map); Assert.assertTrue(map.size()==2); Assert.assertNotNull(map); Assert.assertEquals(Integer.valueOf(1), map.get("key1")); Assert.assertEquals("value2", map.get("key2")); Assert.assertFalse(map.containsKey("key3")); String[] keys = new String[]{"test1", "test3"}; Map<String, Integer> arrayMap = LangOps.kvArrayToMap(keys, new Integer[]{111, 333}); assertThat(arrayMap.size()).isEqualTo(keys.length); assertThat(arrayMap).containsKeys(keys[0], keys[1]); }