J'ai besoin de convertir un Java HashMap
en une instance de TreeMap
(y compris le contenu de la carte)
HashMap<String, Object> src = ...;
TreeMap<String, Object> dest = src.entrySet().stream()
.filter( ... )
.collect(Collectors.toMap( ???, ???, ???, TreeMap::new));
Que dois-je mettre à la place de ???
pour rendre ce code compilable?
De Collectors.toMap (...) javadoc :
* @param keyMapper a mapping function to produce keys
* @param valueMapper a mapping function to produce values
* @param mergeFunction a merge function, used to resolve collisions between
* values associated with the same key, as supplied
* to {@link Map#merge(Object, Object, BiFunction)}
* @param mapSupplier a function which returns a new, empty {@code Map} into
* which the results will be inserted
Par exemple:
HashMap<String, Object> src = ...;
TreeMap<String, Object> dest = src.entrySet().stream()
.filter( ... )
.collect(Collectors.toMap(Map.Entry::getKey , Map.Entry::getValue, (a,b) -> a, TreeMap::new));
Juste une autre façon de convertir une carte en flux:
Utilisation de Stream.of (t)
HashMap<String, Object> src = ...;
TreeMap<String, Object> dest = Stream.of(src)
.filter( ... )
.collect(Collectors.toMap(Map.Entry::getKey , Map.Entry::getValue, (a,b) -> a, TreeMap::new));