简体   繁体   中英

Can't convert tuple list to hashmap java

I want to convert a javax.persistence.Tuple into a HashMap , but like this, it inserts the last element of the tuple and takes also the alias and data type. How can I improve this method so it takes values of the tuple?

public Map<String, Object> tuplesToMap(List<Tuple> data){
    Map<String, Object> values = new HashMap<>();
    data.forEach(tuple -> {
        tuple.getElements().forEach(
            element -> {
                values.put(element.getAlias(), tuple.get(element));
            }
        );
    });
    return values;
}

with java 8 is simply :

return data.stream()
    .collect(Collectors.toMap(
        t -> t.get(0, String.class),
        t -> t.get(1, Object.class)));

Seems to be working :

public static List<Map<String/*UPPERCASE*/, Object>> jpaTuplesToMaps(
  List<javax.persistence.Tuple> data
){
    return data.stream()
        .map(tuple -> { // per each tuple of the input List
            // creating a new HashMap
            Map<String, Object> resultItem =  new HashMap<>();
            // filling the created HashMap with values of
            tuple.getElements().forEach( // each column of the tuple
                col -> { resultItem.put(col.getAlias(), tuple.get(col)); } 
            );
            // returning the created HashMap instead of  the current Tuple
            return resultItem;
        })
         // collecting & returning all the created HashMap-s as a List
        .collect(Collectors.toList());
}

But usualy both single & list conversions are required, so let's combine them :

public static Map<String/*UPPERCASE*/, Object> jpaTupleToMap(
    javax.persistence.Tuple data /*CASE INSENSITIVE*/
){
    Map<String, Object> result = 
        new HashMap<>(); // exactly HashMap since it can handle NULL keys & values
    data.getElements().forEach(
        col -> { result.put(col.getAlias(), data.get(col)); } 
    );
    return result;
}

//-------------------------
    
public static List<Map<String/*UPPERCASE*/, Object>> jpaTuplesToMaps(
    List<javax.persistence.Tuple> data /*CASE INSENSITIVE*/
){
    return data.stream() // List<Tuple> -> Tuple1,..TupleN
        .map(tuple -> jpaTupleToMap(tuple)) // Tuple1 -> HashMap1,..TupleN -> HashMapN
        .collect(Collectors.toList()); // HashMap1,..HashMapN -> List
}

The element.getAlias() you're using as the key for the hashmap is probably same for some of the elements.

Map keys are unique, meaning, if you insert entries (1, "one") and then (1, "two"), the first value will be overridden by the latter. If you want to have multiple values mapped to one key, use Map<String, Collection<Object>> , or a Multimap from Guava, which is exactly the same thing.

You can insert into multimap with this function - if the key is not in the map, create a new ArrayList and add it to the map, otherwise return the existing one. Then, insert the value to the list:

values
    .computeIfAbsent(element.getAlias, k -> new ArrayList<>())
    .add(tuple.get(element));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM