Java Map 내의 요소 정렬(sort)하는 코드 샘플

By | 9월 16, 2019
private Map<String, OwnerVO> getSortedMapByOwnerTotChgVol(Map<String, OwnerVO> ownerMap) {
    
    List<Map.Entry<String, OwnerVO>> sortList = new LinkedList<>(ownerMap.entrySet());
    Collections.sort(sortList, new Comparator<Map.Entry<String, OwnerVO>>() {
        @Override
        public int compare(Entry<String, OwnerVO> o1, Entry<String, OwnerVO> o2) {
            return ((o1.getValue().getTotChgVol() - o2.getValue().getTotChgVol()) < 0 ? 1 : -1);
        }
    });
    Map<String, OwnerVO> sortedMap = new LinkedHashMap<>();
    Iterator<Map.Entry<String, OwnerVO>> iter = sortList.iterator();
    while(iter.hasNext()) {
        Map.Entry<String, OwnerVO> e = iter.next();
        sortedMap.put(e.getKey(), e.getValue());
    }
    return sortedMap;
}

 

 

 

 

 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments