
com.othelle.jtuples.MapUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jtuples Show documentation
Show all versions of jtuples Show documentation
Tuples library for Java. Inspired by Scala tuples.
package com.othelle.jtuples;
/*
* =============================================================================
*
* Copyright 2013, JTuples team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =============================================================================
*/
import java.util.*;
import static com.othelle.jtuples.Tuples.convert;
import static com.othelle.jtuples.Tuples.tuple;
/**
* author: v.vlasov
*/
public class MapUtils {
/**
* Flattens map to a list of tuples
*
* @param map
* @return
*/
public static List> flatten(Map map) {
ArrayList> result = new ArrayList>(map.size());
for (Map.Entry entry : map.entrySet()) result.add(convert(entry));
return result;
}
/**
* Builds a map from a collection of tuples, utilizes the size() method
*
* @param keyValues an iterable object of tuples
* @return a map constructed from the objects
*/
public static Map map(Collection> keyValues) {
return map(keyValues, false);
}
/**
* Pretty verbose function if some special configuration is required. Such as invoking for Collection with a known size.
*
* @param keyValues iterable object of tuples
* @param preserveOrder pass true to use LinkedList
* @return a map constructed from the objects with a given parameters
*/
public static Map map(Collection> keyValues, boolean preserveOrder) {
Map map;
int size = keyValues.size();
//to avoid the rebuilds in most cases, since load factor is <= 1
map = preserveOrder ? new LinkedHashMap(size * 2) : new HashMap(size * 2);
return putItems(map, keyValues);
}
private static Map putItems(Map map, Collection> keyValues) {
for (Tuple2 keyValue : keyValues) {
map.put(keyValue._1(), keyValue._2());
}
return map;
}
//Generated methods do not edit
/**
* Constructs a map from a given list of Tuple2, considering each first element as a key and the rest as a value Tuple1
*
* @param keyValues
* @return a map
*/
public static Map> map2(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple2 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple3, considering each first element as a key and the rest as a value Tuple2
*
* @param keyValues
* @return a map
*/
public static Map> map3(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple3 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple4, considering each first element as a key and the rest as a value Tuple3
*
* @param keyValues
* @return a map
*/
public static Map> map4(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple4 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple5, considering each first element as a key and the rest as a value Tuple4
*
* @param keyValues
* @return a map
*/
public static Map> map5(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple5 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple6, considering each first element as a key and the rest as a value Tuple5
*
* @param keyValues
* @return a map
*/
public static Map> map6(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple6 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple7, considering each first element as a key and the rest as a value Tuple6
*
* @param keyValues
* @return a map
*/
public static Map> map7(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple7 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple8, considering each first element as a key and the rest as a value Tuple7
*
* @param keyValues
* @return a map
*/
public static Map> map8(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple8 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7(), keyValue._8()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple9, considering each first element as a key and the rest as a value Tuple8
*
* @param keyValues
* @return a map
*/
public static Map> map9(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple9 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7(), keyValue._8(), keyValue._9()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple10, considering each first element as a key and the rest as a value Tuple9
*
* @param keyValues
* @return a map
*/
public static Map> map10(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple10 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7(), keyValue._8(), keyValue._9(), keyValue._10()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple11, considering each first element as a key and the rest as a value Tuple10
*
* @param keyValues
* @return a map
*/
public static Map> map11(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple11 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7(), keyValue._8(), keyValue._9(), keyValue._10(), keyValue._11()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple12, considering each first element as a key and the rest as a value Tuple11
*
* @param keyValues
* @return a map
*/
public static Map> map12(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple12 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7(), keyValue._8(), keyValue._9(), keyValue._10(), keyValue._11(), keyValue._12()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple13, considering each first element as a key and the rest as a value Tuple12
*
* @param keyValues
* @return a map
*/
public static Map> map13(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple13 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7(), keyValue._8(), keyValue._9(), keyValue._10(), keyValue._11(), keyValue._12(), keyValue._13()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple14, considering each first element as a key and the rest as a value Tuple13
*
* @param keyValues
* @return a map
*/
public static Map> map14(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple14 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7(), keyValue._8(), keyValue._9(), keyValue._10(), keyValue._11(), keyValue._12(), keyValue._13(), keyValue._14()));
}
return map;
}
/**
* Constructs a map from a given list of Tuple15, considering each first element as a key and the rest as a value Tuple14
*
* @param keyValues
* @return a map
*/
public static Map> map15(Collection> keyValues) {
Map> map = new HashMap>();
for (Tuple15 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7(), keyValue._8(), keyValue._9(), keyValue._10(), keyValue._11(), keyValue._12(), keyValue._13(), keyValue._14(), keyValue._15()));
}
return map;
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple2... keyValues) {
return map2(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple3... keyValues) {
return map3(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple4... keyValues) {
return map4(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple5... keyValues) {
return map5(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple6... keyValues) {
return map6(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple7... keyValues) {
return map7(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple8... keyValues) {
return map8(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple9... keyValues) {
return map9(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple10... keyValues) {
return map10(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple11... keyValues) {
return map11(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple12... keyValues) {
return map12(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple13... keyValues) {
return map13(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple14... keyValues) {
return map14(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple15... keyValues) {
return map15(Arrays.asList(keyValues));
}
/**
* Constructs a map from a given list of Tuple16, considering each first element as a key and the rest as a value Tuple15
*
* @param keyValues
* @return a map
*/
public static Map> map16(Iterable> keyValues) {
Map> map = new HashMap>();
for (Tuple16 keyValue : keyValues) {
map.put(keyValue._1(), tuple(keyValue._2(), keyValue._3(), keyValue._4(), keyValue._5(), keyValue._6(), keyValue._7(), keyValue._8(), keyValue._9(), keyValue._10(), keyValue._11(), keyValue._12(), keyValue._13(), keyValue._14(), keyValue._15(), keyValue._16()));
}
return map;
}
/**
* Constructs a map from a given keyValues
*
* @param keyValues
* @return map build from an array of tuples
*/
public static Map> map(Tuple16... keyValues) {
return map16(Arrays.asList(keyValues));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy