[Home] DMelt member access . Java source code: com.datumbox.framework.core.common.utilities.MapMethods
/**
* Copyright (C) 2013-2017 Vasilis Vryniotis
*
* 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.
*/
package com.datumbox.framework.core.common.utilities;
import com.datumbox.framework.common.dataobjects.AssociativeArray;
import com.datumbox.framework.common.dataobjects.TypeInference;
import java.util.*;
/**
* The MapMethods class contains a list of convenience methods for Maps and
AssociativeArrays.
*
* @author Vasilis Vryniotis
*/
public class MapMethods {
/**
* Selects the key-value entry with the largest value.
*
* @param keyValueMap
* @return
*/
public static Map.Entry selectMaxKeyValue(AssociativeArray keyValueMap) {
Double maxValue=Double.NEGATIVE_INFINITY;
Object maxValueKey = null;
for(Map.Entry entry : keyValueMap.entrySet()) {
Double value = TypeInference.toDouble(entry.getValue());
if(value!=null && value>maxValue) {
maxValue=value;
maxValueKey=entry.getKey();
}
}
return new AbstractMap.SimpleEntry<>(maxValueKey, keyValueMap.get(maxValueKey));
}
/**
* Selects the key-value entry with the largest value.
*
* @param keyValueMap
* @return
*/
public static Map.Entry selectMaxKeyValue(Map keyValueMap) {
Double maxValue=Double.NEGATIVE_INFINITY;
Object maxValueKey = null;
for(Map.Entry entry : keyValueMap.entrySet()) {
Double value = entry.getValue();
if(value!=null && value>maxValue) {
maxValue=value;
maxValueKey=entry.getKey();
}
}
return new AbstractMap.SimpleEntry<>(maxValueKey, keyValueMap.get(maxValueKey));
}
/**
* Selects the key-value entry with the smallest value.
*
* @param keyValueMap
* @return
*/
public static Map.Entry selectMinKeyValue(AssociativeArray keyValueMap) {
Double minValue=Double.POSITIVE_INFINITY;
Object minValueKey = null;
for(Map.Entry entry : keyValueMap.entrySet()) {
Double value = TypeInference.toDouble(entry.getValue());
if(value!=null && value(minValueKey, keyValueMap.get(minValueKey));
}
/**
* Selects the key-value entry with the smallest value.
*
* @param keyValueMap
* @return
*/
public static Map.Entry selectMinKeyValue(Map keyValueMap) {
Double minValue=Double.POSITIVE_INFINITY;
Object minValueKey = null;
for(Map.Entry entry : keyValueMap.entrySet()) {
Double value = entry.getValue();
if(value!=null && value(minValueKey, keyValueMap.get(minValueKey));
}
/**
* Sorts by Key a Map in ascending order.
*
* @param
* @param
* @param map
* @return
*/
public static Map sortNumberMapByKeyAscending(Map map) {
return sortNumberMapByKeyAscending(map.entrySet());
}
/**
* Sorts by Key a Map in ascending order.
*
* @param
* @param
* @param entrySet
* @return
*/
public static Map sortNumberMapByKeyAscending(Set> entrySet) {
ArrayList> entries = new ArrayList<>(entrySet);
Collections.sort(entries, (Map.Entry a, Map.Entry b) -> {
Double va = TypeInference.toDouble(a.getKey());
Double vb = TypeInference.toDouble(b.getKey());
return va.compareTo(vb);
});
Map sortedMap = new LinkedHashMap<>();
for (Map.Entry entry : entries) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
/**
* Sorts by Key a Map in descending order.
*
* @param
* @param
* @param map
* @return
*/
public static Map sortNumberMapByKeyDescending(Map map) {
return sortNumberMapByKeyDescending(map.entrySet());
}
/**
* Sorts by Key a Map in descending order.
*
* @param
* @param
* @param entrySet
* @return
*/
public static Map sortNumberMapByKeyDescending(Set> entrySet) {
ArrayList> entries = new ArrayList<>(entrySet);
Collections.sort(entries, (Map.Entry a, Map.Entry b) -> {
Double va = TypeInference.toDouble(a.getKey());
Double vb = TypeInference.toDouble(b.getKey());
return -va.compareTo(vb);
});
Map sortedMap = new LinkedHashMap<>();
for (Map.Entry entry : entries) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
/**
* Sorts by Value a Map in ascending order.
*
* @param
* @param
* @param map
* @return
*/
public static Map sortNumberMapByValueAscending(Map map) {
ArrayList> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, (Map.Entry a, Map.Entry b) -> {
Double va = TypeInference.toDouble(a.getValue());
Double vb = TypeInference.toDouble(b.getValue());
return va.compareTo(vb);
});
Map sortedMap = new LinkedHashMap<>();
for (Map.Entry entry : entries) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
/**
* Sorts by Value a Map in descending order.
*
* @param
* @param
* @param map
* @return
*/
public static Map sortNumberMapByValueDescending(Map map) {
ArrayList> entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, (Map.Entry a, Map.Entry b) -> {
Double va = TypeInference.toDouble(a.getValue());
Double vb = TypeInference.toDouble(b.getValue());
return -va.compareTo(vb);
});
Map sortedMap = new LinkedHashMap<>();
for (Map.Entry entry : entries) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
/**
* Sorts by Value a Associative Array in ascending order.
*
* @param associativeArray
* @return
*/
public static AssociativeArray sortAssociativeArrayByValueAscending(AssociativeArray associativeArray) {
ArrayList> entries = new ArrayList<>(associativeArray.entrySet());
Collections.sort(entries, (Map.Entry a, Map.Entry b) -> {
Double va = TypeInference.toDouble(a.getValue());
Double vb = TypeInference.toDouble(b.getValue());
return va.compareTo(vb);
});
AssociativeArray sortedAssociativeArray = new AssociativeArray();
for (Map.Entry entry : entries) {
sortedAssociativeArray.put(entry.getKey(), entry.getValue());
}
return sortedAssociativeArray;
}
/**
* Sorts by Value a Associative Array in descending order.
*
* @param associativeArray
* @return
*/
public static AssociativeArray sortAssociativeArrayByValueDescending(AssociativeArray associativeArray) {
ArrayList> entries = new ArrayList<>(associativeArray.entrySet());
Collections.sort(entries, (Map.Entry a, Map.Entry b) -> {
Double va = TypeInference.toDouble(a.getValue());
Double vb = TypeInference.toDouble(b.getValue());
return -va.compareTo(vb);
});
AssociativeArray sortedAssociativeArray = new AssociativeArray();
for (Map.Entry entry : entries) {
sortedAssociativeArray.put(entry.getKey(), entry.getValue());
}
return sortedAssociativeArray;
}
}
Ads help maintain this website.