I am new to java 8 and would like to write a function which sorts hashmap by values and if values are the same sort by keys.
To sort the hashmap by values:
Map<String, Integer> map1 = new LinkedHashMap<>();
map.entrySet()
.stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.forEachOrdered(x -> map1.put(x.getKey(), x.getValue()));
map1.forEach((k,v) ->{ System.out.println(k +" "+v);} );
I have worked with Python 3 and it has sorting for both keys and values using mapSorted = sorted(map.items(), key = lambda item : (item[1], item[0])). Is there something similar in Java 8?
The API you are looking forward to is Comparator#thenComparing. The reason why the implementation for such sorting is not straightforward is because of the type-inference.
The type inference needs some help which can be provided such as :
Comparator.comparing(Map.Entry<String, Integer>::getValue)
.reversed().thenComparing(Map.Entry::getKey)
Apart from which you shall ideally collect the output to a Map that preserves the order, otherwise, the sorting is a waste of computations. Hence something like this shall work:
LinkedHashMap<String, Integer> sortedMap = map.entrySet()
.stream()
.sorted(Comparator.comparing(Map.Entry<String, Integer>::getValue)
.reversed().thenComparing(Map.Entry::getKey))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(a, b) -> a, LinkedHashMap::new));
define your map
HashMap<String, Integer>map1 = new HashMap();
map1.put("aa",5);
map1.put("bbb",2);
map1.put("ccccc",2);
map1.put("dddddd",3);
sort,if you want to compare with string your need defining it by yourself
List<Entry<String, Integer>> collect = map1.entrySet().stream().sorted(new Comparator<Entry<String, Integer>>(){
#Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
int ll=0;
if (o1.getValue()>o2.getValue()){
ll=-1;
}
else if(o1.getValue()<o2.getValue()){
ll=1;
}
else if (o1.getKey().length()>o2.getKey().length()) {
ll=-1;
}
else if (o1.getKey().length()<o2.getKey().length()) {
ll=1;
};
return ll;
}
}).collect(Collectors.toList());
the result like [aa=5, dddddd=3, ccccc=2, bbb=2]
Say I have a HashMap and I want to insert the same value to a list of keys. How can I do this with Java 8 without iterating through all the keys and inserting the value? This is more of a Java Streams question.
Here is the straight forward way of doing it. This is a sample code that I wrote to demonstrate what I wanted to achieve.
public void foo(List<String> keys, Integer value) {
Map<String, Integer> myMap = new HashMap<>();
for (String key : keys) {
myMap.put(key, value);
}
}
Is there a simpler way of doing the above using Java 8 streams? How can I avoid the for loop using Java 8 streams. Thanks!
[Edit-1] A better code snippet below.
public void foo() {
Map<String, Integer> myMap = new HashMap<>();
List<String> keys = getKeysFromAnotherFunction();
Integer value = getValueToBeInserted(); // Difficult to show my actual use case. Imagine that some value is getting computed which has to be inserted for the keys.
for (String key : keys) {
myMap.put(key, value);
}
List<String> keys2 = getNextSetOfKeys();
Integer newValue = getValueToBeInserted();
for (String key : keys2) {
myMap.put(key, newValue);
}
}
Using collector, something like:
Map<String, Integer> myMap = keys.stream()
.collect(Collectors.toMap(key -> key,
val -> value, (a, b) -> b));
I think that your question is about factoring out some piece of code more than converting traditional for loops into stream constructs.
Suppose you have the following generic utility method:
public static <K, V, M extends Map<K, V>> M fillMap(
Supplier<List<K>> keysFactory,
Supplier<V> singleValueFactory,
Supplier<M> mapFactory) {
M map = mapFactory.get();
List<K> keys = keysFactory.get();
V singleValue = singleValueFactory.get();
keys.forEach(k -> map.put(k, singleValue));
return map;
}
Then, you could use the above method as follows:
Map<String, Integer> myMap = fillMap(() -> getKeysFromAnotherFunction(),
() -> getValueToBeInserted(),
HashMap::new); // create HashMap
myMap = fillMap(() -> getNextSetOfKeys(),
() -> getValueToBeInserted(),
() -> myMap); // use previously created map
There are variants for the code above, i.e., the method could receive a Map<K, V> instance instead of a Supplier<Map<K, V>>, or it might even be overloaded to support both variants.
How can i create a sorted map which compares by value's bean's field.
Map key is Integer and value is the Car.
I'd like to compare by car's name it's can contains no only letters.
Something like this: (it's not work, only a bad example)
Comparator<Car> carNameComparator = new Comparator<Car>() {
#Override
public int compare(Car c1, Car c2) {
return c1.getName().compareToIgnoreCase(c2.getName());
}
};
SortedMap<Integer,Car> carMap = new TreeMap<>(carNameComparator);
Second try:
Map<Integer, Car> carMap = new HashMap<>();
carMap.put(1, car1);
carMap.put(2, car2);
Map<Integer, Car> sortedByValue = carMap.entrySet().stream()
.sorted(Map.Entry.<Integer, Car> comparingByValue(carNameComparator))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
sortedByValue.forEach((k,c)->System.out.println("Key : " + k + " Value : " + c.getName()));
Maybe the second can be the right solution, somebody know a better solution, where don't need to create an other map?
I am trying to convert Set to Map using java 8 using collectors.
Set<B2BUnitModel> payersList = .....
final Map<B2BUnitModel, List<B2BUnitPartnershipModel>> b2bUnitPartnershipMap = new HashMap<>();
final Map<B2BUnitModel, Object> map = payersList.stream().collect(Collectors.toMap(Function.identity(), Arrays::asList));
b2bUnitPartnershipMap.putAll(map); // Will throw Type Cast Error
I am not able to understand how do I convert Map values to B2BUnitPartnershipModel type since Arrays::asList only will return Object type.
Is there any way I can write something in Collectors.toMap(Function.identity(), Arrays::asList) itself so that the api will return the desired Map (Map<B2BUnitModel, List<B2BUnitPartnershipModel>> instead of Map<B2BUnitModel, Object>).
I want to create a Map with Set value as a key and empty B2BUnitPartnershipModel list.
Asuming B2BUnitModel is Key and B2BUnitPartnershipModel is Value the following code produces a map with keys from the set and empty lists as values using lambda expressions instead of method references.
Code
#Test
public void testMapCollector() {
Set<Key> keySet = new HashSet<>();
keySet.add(new Key("key1"));
keySet.add(new Key("key2"));
keySet.add(new Key("key3"));
Map<Key, List<Value>> map = keySet.stream().collect(
Collectors.toMap(k -> k, key -> new ArrayList<>()));
System.out.println(map);
}
class Key {
String key;
public Key(String value) {
this.key = value;
}
#Override
public String toString() {
return "Key [key=" + key + "]";
}
}
class Value {
}
Output
{Key [key=key1]=[], Key [key=key3]=[], Key [key=key2]=[]}
Your Arrays::asList causes problem. Try this:
Map<B2BUnitModel, List<B2BUnitPartnershipModel>> map = set.stream().collect(Collectors.toMap(Function.identity(), unit -> new ArrayList<>()));
I have two maps whose keys are Strings and whose values are Set<MyObject>. Given two Maps, what is the easiest way to merge them such that if two keys are identical, the value is a union of the two sets. You can assume values are never null and if it is useful, we can make these Maps SortedMaps.
You can do this with a stream fairly easily:
Map<T, Set<U>> merged = Stream.of(first, second)
.map(Map::entrySet)
.flatMap(Set::stream)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a, b) -> {
HashSet<U> both = new HashSet<>(a);
both.addAll(b);
return both;
}));
This splits the maps into their Entrys and then joins them with a Collector which resolves duplicates by adding both values to a new HashSet.
This also works for any number of maps.
Some variations which produce the same result:
Stream.of(first, second).flatMap(m -> m.entrySet().stream())
.collect(...);
Stream.concat(first.entrySet().stream(), second.entrySet().stream())
.collect(...); //from comment by Aleksandr Dubinsky
The third parameter for Collectors.toMap is not necessary if there are no duplicate keys.
There is another Collectors.toMap with a fourth parameter that lets you decide the type of the Map collected into.
Are we talking about HashMap instances. In that case lookup is O(1), so you can just take one map, iterate over the entries of that map, see whether the other map contains that key. If not, just add the set. If it contains the key, take the union of the two sets (by adding all elements of one set to another)
To illustrate with some code, where I used a Set to have autocompletion in my IDE
Map<String, Set<Double>> firstMap = new HashMap<String, Set<Double>>( );
Map<String, Set<Double>> secondMap = new HashMap<String, Set<Double>>( );
Set<Map.Entry<String, Set<Double>>> entries = firstMap.entrySet();
for ( Map.Entry<String, Set<Double>> entry : entries ) {
Set<Double> secondMapValue = secondMap.get( entry.getKey() );
if ( secondMapValue == null ) {
secondMap.put( entry.getKey(), entry.getValue() );
}
else {
secondMapValue.addAll( entry.getValue() );
}
}
static void mergeSet(Map<String, Set<String>> map1, Map<String, Set<String>> map2) {
map1.forEach((key1, value1) -> {
map2.merge(key1, value1, (key2, value2) -> key2).addAll(value1);
});
}
How about this (untested):
Map<String,Set<Whatever>> m1 = // input map
Map<String,Set<Whatever>> m2 = // input map
Map<String,Set<Whatever>> ret = // new empty map
ret.putAll(m1);
for(String key : m2.keySet()) {
if(ret.containsKey(key)) {
ret.get(key).addAll(m2.get(key));
} else {
ret.put(key,m2.get(key));
}
}
This solution doesn't modify the input maps, and because it is short and relies on API methods only, I find it quite readable.
Note that putAll() and addAll() are both optional methods in Map and Set. Consequently (and in order to get O(1) lookup), I'd recommend using HashMap and HashSet.
Note that because neither HashSet or HashMap are synchronised you will need to look for some other solution if you want thread-safe code.
The following should merge a map1 into map2 (untested):
for (Entry<String, Set<???>> entry : map1.entrySet( ))
{
Set<???> otherSet = map2.get(entry.getKey( ));
if (otherSet == null)
map2.put(entry.getKey( ), entry.getValue ( ));
else
otherSet.addAll(entry.getValue( ));
}
I don't know what you've parameterized your Sets on, hence the <???>: replace as appropriate.
Something like this (untested):
// Assume all maps are of the same generic type.
public static Map<String, Set<MyObject>> mergeAll(Map m1, Map m2) {
Map<String, Set<MyObject>> merged = new HashMap();
// Merge commom entries into the new map.
for (Map.Entry<String, Set<MyObject>> entry : m1.entrySet()) {
String key = entry.getKey();
Set<MyObject> s1 = new HashSet(entry.getValue());
Set<MyObject> s2 = m2.get(key);
if (s2 != null) s1.addAll(s2);
merged.put(key, s1);
}
// Add entries unique to m2 to the new map.
for (String key : m2.keys()) {
if (!s1.containsKey(key)) merged.put(key, new HashSet(m2.get(key)));
}
return merged;
}
Note that this solution does not mutate either of its arguments.
Map<Integer,String> m1=new HashMap<Integer,String>();
Map<Integer,String> m2=new HashMap<Integer,String>();
m1.put(1,"one");
m1.put(2,"two");
m2.put(3,"three");
m2.put(2,"two");
Set<Integer> s=m2.keySet();
for(int i:s){
if(m1.get(i)==null){
m1.put(i,m2.get(i));
}
}
System.out.println(m1);
Note that all other answers will eventually augment the original sets which you might not want for all use cases, if you don't want that just use a third map as output and create a new set for each key
public static void merge2Maps(Map<String, Set<Double>> a, Map<String, Set<Double>> b, Map<String, Set<Double>> c){
for (Map.Entry<String, Set<Double>> entry : a.entrySet()) {
Set<Double> set = new HashSet<Double>();
c.put(entry.getKey(), set);
set.addAll(entry.getValue());
}
for (Map.Entry<String, Set<Double>> entry : b.entrySet()) {
String key = entry.getKey();
Set<Double> set = c.get(key);
if (set == null) {
set = new HashSet<Double>();
c.put(entry.getKey(), set);
}
set.addAll(entry.getValue());
}
}
If you want to end up with immutable data structures to prevent manipulation of your merged map and map's Set instances then you can take this approach. This solution uses Google's Guava library.
public <K,T> Map<K, Set<T>> mergeToImmutable (
final Map<K, Set<T>> left,
final Map<K, Set<T>> right)
{
return Maps.toMap(
Sets.union(
checkNotNull(left).keySet(),
checkNotNull(right).keySet()
),
new Function<K, Set<T>> () {
#Override
public Set<T> apply (K input) {
return ImmutableSet.<T>builder()
.addAll(MoreObjects.firstNonNull(left.get(input), Collections.<T>emptySet()))
.addAll(MoreObjects.firstNonNull(right.get(input), Collections.<T>emptySet()))
.build();
}
}
);
}
If you define a method to unite non-null Sets as:
static <T> Set<T> union(Set<T>... sets) {
return Stream.of(sets)
.filter(s -> s != null)
.flatMap(Set::stream)
.collect(Collectors.toSet());
}
then merging two maps m1 and m2 having Set<V> values can be performed as follows:
Map<String, V> merged
= union(m1.keySet(), m2.keySet())
.stream()
.collect(Collectors.toMap(k -> k, k -> union(m1.get(k), m2.get(k))));
Or even simpler:
Map<String, V> merged = new HashMap<>();
for (String k : union(m1.keySet(), m2.keySet())
merged.put(k, union(m1.get(k), m2.get(k)));
<K, V> Map<K, List<V>> mergeMapOfLists(Stream<Map<K, List<V>>> stream) {
return stream
.map(Map::entrySet) // convert each map to set of map's entries
.flatMap(Collection::stream) // convert each map entry to stream and flat them to one stream
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue,
(list1, list2) -> {
list1.addAll(list2);
return list1;
})); // convert stream to map; if key is duplicated execute merge fuction (append exisitng list with elements from new list)
}