您的位置:首页 > 编程语言 > Java开发

java工具类之将map范型集合转化为list的有序集合工具类

2016-06-14 20:27 393 查看
   在项目开发当中,不断形成通用的类库或者说工具库,对于以后代码重用,提高项目的开发效率和质量都是至关重要的。后续会发布java工具类系列文章,希望对各位开发者有开发方面的帮助。

    本篇介绍的是将map范型集合转化为list的有序集合工具类,

   1、将Map<K,V>格式的按list排序输出

    如下边的map集合:

                Map<String,Integer> map=new HashMap<String,Integer>();
map.put("1班",11);
map.put("2班",22);
map.put("3班",33);
map.put("4班",44);

    经过工具类处理后的形成的list的代码段为:

                List<Map.Entry<String, Integer>> sortList = mapTranUtil
.mapToSortList4NotListValue(map,mapTranUtil.defaultComparator4NotListValue);
for (Map.Entry entry : sortList) {
System.out.println(entry);
}

     其实际输出为:

           4班=44
   3班=33
   2班=22

           1班=11

其实整代码:

package com.letv.zel.portrait.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 将一个map无序集合,转化成list的有序集合 k,v为范型
*
* @author zel
*
* @param <K>
* @param <V>
*/
public class MapToSortListUtil<K, V> {

/**
* 默认的Map<k,List<v>>转化成List<k,List<v>>的排序器,按List<v>的size大小降序排列
*/
public Comparator<Map.Entry<K, List<V>>> defaultComparator4List = new Comparator<Map.Entry<K, List<V>>>() {
public int compare(Map.Entry<K, List<V>> mapping1,
Map.Entry<K, List<V>> mapping2) {
return mapping2.getValue().size() - mapping1.getValue().size();
}
};

/**
* 默认的Map<k,v>转化成List<k,v>的排序器,按v的值降序排列
*/
public Comparator<Map.Entry<K, V>> defaultComparator4NotListValue = new Comparator<Map.Entry<K, V>>() {
public int compare(Map.Entry<K, V> mapping1, Map.Entry<K, V> mapping2) {
Comparable comp1 = (Comparable) mapping1.getValue();
Comparable comp2 = (Comparable) mapping2.getValue();
return comp2.compareTo(comp1);
}
};

public List<Map.Entry<K, List<V>>> mapToSortList4List(Map<K, List<V>> map,
Comparator<Map.Entry<K, List<V>>> comparator) {

List<Map.Entry<K, List<V>>> mapToSortList = new ArrayList<Map.Entry<K, List<V>>>(
map.entrySet());

Collections.sort(mapToSortList, comparator);

return mapToSortList;
}

public List<Map.Entry<K, V>> mapToSortList4NotListValue(Map<K, V> map,
Comparator<Map.Entry<K, V>> comparator) {
List<Map.Entry<K, V>> mapToSortList = new ArrayList<Map.Entry<K, V>>(
map.entrySet());
Collections.sort(mapToSortList, comparator);

return mapToSortList;
}

public Comparator<Map.Entry<K, List<V>>> getDefaultComparator4List() {
return defaultComparator4List;
}

public static void main(String[] args) {
MapToSortListUtil<String, Integer> mapTranUtil = new MapToSortListUtil<String, Integer>();
Map<String, Integer> map = new HashMap<String, Integer>();

map.put("1班", 11);
map.put("2班", 22);
map.put("3班", 33);
map.put("4班", 44);

List<Map.Entry<String, Integer>> sortList = mapTranUtil
.mapToSortList4NotListValue(map,
mapTranUtil.defaultComparator4NotListValue);

for (Map.Entry entry : sortList) {
System.out.println(entry);
}

// MapToSortListUtil<String, String> mapTranUtil = new MapToSortListUtil<String, String>();
// Map<String, List<String>> map = new HashMap<String, List<String>>();
//
// List<String> list_1 = new ArrayList<String>();
// list_1.add("1");
//
// List<String> list_2 = new ArrayList<String>();
// list_2.add("1");
// list_2.add("1");
//
// List<String> list_3 = new ArrayList<String>();
// list_3.add("1");
// list_3.add("1");
// list_3.add("1");
//
// List<String> list_4 = new ArrayList<String>();
// list_4.add("1");
// list_4.add("1");
// list_4.add("1");
// list_4.add("1");
//
// map.put("1班", list_1);
// map.put("2班", list_2);
// map.put("3班", list_3);
// map.put("4班", list_4);
//
// List<Map.Entry<String, List<String>>> sortList = mapTranUtil
// .mapToSortList4List(map, mapTranUtil.defaultComparator4List);
//
// for (Map.Entry entry : sortList) {
// System.out.println(entry);
// }

}
}


2、将Map<K,List<V>>格式的按list排序输出

                如下边的map集合:
Map<String, List<String>> map = new HashMap<String, List<String>>();

List<String> list_1 = new ArrayList<String>();
list_1.add("1");

List<String> list_2 = new ArrayList<String>();
list_2.add("1");
list_2.add("1");

List<String> list_3 = new ArrayList<String>();
list_3.add("1");
list_3.add("1");
list_3.add("1");

List<String> list_4 = new ArrayList<String>();
list_4.add("1");
list_4.add("1");
list_4.add("1");
list_4.add("1");

map.put("1班", list_1);
map.put("2班", list_2);
map.put("3班", list_3);
map.put("4班", list_4);

    经过工具类处理后的形成的list的代码段为:

List<Map.Entry<String, List<String>>> sortList = mapTranUtil
.mapToSortList4List(map, mapTranUtil.defaultComparator4List);

for (Map.Entry entry : sortList) {
System.out.println(entry);
}

其实际输出为:

4班=[1, 1, 1, 1]

3班=[1, 1, 1]

2班=[1, 1]

1班=[1]

完整的代码段为:

package com.letv.zel.portrait.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 将一个map无序集合,转化成list的有序集合 k,v为范型
*
* @author zel
*
* @param <K>
* @param <V>
*/
public class MapToSortListUtil<K, V> {

/**
* 默认的Map<k,List<v>>转化成List<k,List<v>>的排序器,按List<v>的size大小降序排列
*/
public Comparator<Map.Entry<K, List<V>>> defaultComparator4List = new Comparator<Map.Entry<K, List<V>>>() {
public int compare(Map.Entry<K, List<V>> mapping1,
Map.Entry<K, List<V>> mapping2) {
return mapping2.getValue().size() - mapping1.getValue().size();
}
};

/**
* 默认的Map<k,v>转化成List<k,v>的排序器,按v的值降序排列
*/
public Comparator<Map.Entry<K, V>> defaultComparator4NotListValue = new Comparator<Map.Entry<K, V>>() {
public int compare(Map.Entry<K, V> mapping1, Map.Entry<K, V> mapping2) {
Comparable comp1 = (Comparable) mapping1.getValue();
Comparable comp2 = (Comparable) mapping2.getValue();
return comp2.compareTo(comp1);
}
};

public List<Map.Entry<K, List<V>>> mapToSortList4List(Map<K, List<V>> map,
Comparator<Map.Entry<K, List<V>>> comparator) {

List<Map.Entry<K, List<V>>> mapToSortList = new ArrayList<Map.Entry<K, List<V>>>(
map.entrySet());

Collections.sort(mapToSortList, comparator);

return mapToSortList;
}

public List<Map.Entry<K, V>> mapToSortList4NotListValue(Map<K, V> map,
Comparator<Map.Entry<K, V>> comparator) {
List<Map.Entry<K, V>> mapToSortList = new ArrayList<Map.Entry<K, V>>(
map.entrySet());
Collections.sort(mapToSortList, comparator);

return mapToSortList;
}

public Comparator<Map.Entry<K, List<V>>> getDefaultComparator4List() {
return defaultComparator4List;
}

public static void main(String[] args) {
// MapToSortListUtil<String, Integer> mapTranUtil = new MapToSortListUtil<String, Integer>();
// Map<String, Integer> map = new HashMap<String, Integer>();
//
// map.put("1班", 11);
// map.put("2班", 22);
// map.put("3班", 33);
// map.put("4班", 44);
//
// List<Map.Entry<String, Integer>> sortList = mapTranUtil
// .mapToSortList4NotListValue(map,
// mapTranUtil.defaultComparator4NotListValue);
//
// for (Map.Entry entry : sortList) {
// System.out.println(entry);
// }

MapToSortListUtil<String, String> mapTranUtil = new MapToSortListUtil<String, String>();
Map<String, List<String>> map = new HashMap<String, List<String>>();

List<String> list_1 = new ArrayList<String>();
list_1.add("1");

List<String> list_2 = new ArrayList<String>();
list_2.add("1");
list_2.add("1");

List<String> list_3 = new ArrayList<String>();
list_3.add("1");
list_3.add("1");
list_3.add("1");

List<String> list_4 = new ArrayList<String>();
list_4.add("1");
list_4.add("1");
list_4.add("1");
list_4.add("1");

map.put("1班", list_1);
map.put("2班", list_2);
map.put("3班", list_3);
map.put("4班", list_4);

List<Map.Entry<String, List<String>>> sortList = mapTranUtil
.mapToSortList4List(map, mapTranUtil.defaultComparator4List);

for (Map.Entry entry : sortList) {
System.out.println(entry);
}

}
}


更多学习讨论,      请加入官方QQ技术群320349384,
                                 官方天亮论坛:http://bbs.yuqing36524.com/
                                 天亮教育视频链接:http://pan.baidu.com/s/1pJJrcqJ


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息