map 속성 에 따라 정렬, map 앞 n 개 후 n 개 꺼 내기
/**
* map key
*
* @param map
* @return
*/
private Map<String, Object> sortByKey(Map<String, Object> map) {
Map<String, Object> result = new LinkedHashMap<>(map.size());
map.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
return result;
}
map 속성 에 따라 정렬, map 전 n 개, 후 n 개 꺼 내기
/**
* map value
* flag = 1
* flag = 0
*
* @param map
* @param flag
* @return
*/
public static <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortByValue(Map<K, V> map, int flag) {
LinkedHashMap<K, V> sortMap = new LinkedHashMap<>();
if (flag == 1) {
map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue)).forEach(entry -> sortMap.put(entry.getKey(), entry.getValue()));
} else {
map.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue())).forEach(entry -> sortMap.put(entry.getKey(), entry.getValue()));
}
return sortMap;
}
/**
* map n
*
* @param map
* @param length
* @return
*/
public LinkedHashMap<String, Double> subMap(LinkedHashMap<String, Double> map, int length) {
List<Map.Entry<String, Double>> lists = new ArrayList<>(map.entrySet());
LinkedHashMap<String, Double> sortedMap = new LinkedHashMap<>();
if (lists.size() >= length) {
for (Map.Entry<String, Double> set : lists.subList(0, length)) {
sortedMap.put(set.getKey(), set.getValue());
}
} else {
for (Map.Entry<String, Double> set : lists) {
sortedMap.put(set.getKey(), set.getValue());
}
}
return sortedMap;
}
/**
* map n
*
* @param map
* @param length
* @return
*/
protected LinkedHashMap<Date, BigDecimal> subMap(LinkedHashMap<Date, BigDecimal> map, int length) {
List<Map.Entry<Date, BigDecimal>> lists = new ArrayList<>(map.entrySet());
LinkedHashMap<Date, BigDecimal> sortedMap = new LinkedHashMap<>();
int size = lists.size();
if (size >= length) {
for (Map.Entry<Date, BigDecimal> set : lists.subList(size -length, size)) {
sortedMap.put(set.getKey(), set.getValue());
}
} else {
for (Map.Entry<Date, BigDecimal> set : lists) {
sortedMap.put(set.getKey(), set.getValue());
}
}
return sortedMap;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JAVA 객체 작성 및 제거 방법정적 공장 방법 정적 공장 방법의 장점 를 반환할 수 있습니다. 정적 공장 방법의 단점 류 공유되거나 보호된 구조기를 포함하지 않으면 이불류화할 수 없음 여러 개의 구조기 파라미터를 만났을 때 구축기를 고려해야 한다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.