精华内容
下载资源
问答
  • 集合的转换-Map转换为List ** public EchartHistogram getAgePie(StatParamBo data) throws Exception { String userInfoPieTitle = "用户年龄占比"; //初始化各年龄段的人数和年龄段 Integer value0 = 0, value1...

    **

    集合的转换-Map转换为List

    **

    public EchartHistogram getAgePie(StatParamBo data) throws Exception {
    	String userInfoPieTitle = "用户年龄占比";
    	//初始化各年龄段的人数和年龄段
    	Integer value0 = 0, value1 = 0, value2 = 0, value3 = 0, value4 = 0;
    	String name0 = "0-20岁", name1 = "20-30岁", name2 = "30-40岁", name3 = "40-50岁", name4 = "50岁以上";
       Map<String, Integer> map = new TreeMap<String, Integer>();
    	//获取从数据库查询到的数据
    	List<SgGuiduserEntity> list = userInfoStatDao.getAgeList(data);
         //定义泛型为NameValueEntity的List集合(接收处理之后的数据)
    	List<NameValueEntity> list1 = new ArrayList<NameValueEntity>();
     //遍历身份证(获取年龄)
    	for (SgGuiduserEntity sg : list) {
        	//获取身份证号码
    		String idCard = sg.getCardId();
    		//获取年龄
    		Integer age  = this.getIdCaidToAge(idCard);
    		if (age <= 20) {
    			value0++;
    		}else if (age > 20 && age <= 30){
    			value1++;
    		}else if (age > 30 && age <= 40){
    			value2++;
    		}else if (age > 40 && age <= 50){
    			value3++;
    		}else {
    			value4++;
    		}
    	}	
    	/*
    	 * 按顺序存贮数据(上面的Map是用TreeMap的形式存储,所以自带顺序)
    	*/
    	map.put(name0, value0);
    	map.put(name1, value1);
    	map.put(name2, value2);
    	map.put(name3, value3);
    	map.put(name4, value4);
    	/*
    	 * 将map转换为list
    	 */
      //使用keySet()返回map中所有key值的列表。
    	Set<String> listSet = map.keySet();
     //遍历listSet(即:获取到的key值)
    	Iterator<String> iterator =  listSet.iterator();
    	while(iterator.hasNext()) {
         //将获取到的值赋给NameValueEntity类的Name和Value属性。
    		NameValueEntity itemEntity = new NameValueEntity();
    		String key =  iterator.next();
    		Integer value = map.get(key);
    		itemEntity.setName(key);
    		itemEntity.setValue(value);
    		list1.add(itemEntity);
       }
       EchartHistogram agePie = EchartTransForm.transFormHistogramNew(userInfoPieTitle, list1);
    	return agePie;
    }
    
    展开全文
  • Java 8 将Map转换为List

    万次阅读 2018-12-26 11:00:51
    将一个Java示例转换Map为List 汇总: Map&lt;String, String&gt; map = new HashMap&lt;&gt;(); // Convert all Map keys to a List List&lt;String&gt; result = new ArrayList(map....

    将一个Java示例转换MapList

    汇总:

    Map<String, String> map = new HashMap<>();
    // Convert all Map keys to a List
    List<String> result = new ArrayList(map.keySet());
    // Convert all Map values to a List
    List<String> result2 = new ArrayList(map.values());
    // Java 8, Convert all Map keys to a List
    List<String> result3 = map.keySet().stream()
    	.collect(Collectors.toList());
    // Java 8, Convert all Map values  to a List
    List<String> result4 = map.values().stream()
    	.collect(Collectors.toList());
    // Java 8, seem a bit long, but you can enjoy the Stream features like filter and etc.
    List<String> result5 = map.values().stream()
    	.filter(x -> !"apple".equalsIgnoreCase(x))
    	.collect(Collectors.toList());
    // Java 8, split a map into 2 List, it works!
    // refer example 3 below
    

    ~~~~~~~~~~~~~~~~~~~~~~~~~~

    分类:

    1.映射到List

    为了简单MapList转换,只需使用下面的代码:

    ConvertMapToList.java

    package com.mkyong;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    public class ConvertMapToList {
        public static void main(String[] args) {
            Map<Integer, String> map = new HashMap<>();
            map.put(10, "apple");
            map.put(20, "orange");
            map.put(30, "banana");
            map.put(40, "watermelon");
            map.put(50, "dragonfruit");
            System.out.println("\n1. Export Map Key to List...");
            List<Integer> result = new ArrayList(map.keySet());
            result.forEach(System.out::println);
            System.out.println("\n2. Export Map Value to List...");
            List<String> result2 = new ArrayList(map.values());
            result2.forEach(System.out::println);
        }
    }
    

    Output

    1. Export Map Key to List...
    50
    20
    40
    10
    30
    2. Export Map Value to List...
    dragonfruit
    orange
    watermelon
    apple
    banana
    

    2.Java 8 - 映射到List

    对于Java 8,您可以将其Map转换为流,将其处理并将其返回List

    ConvertMapToList.java

    package com.mkyong;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    public class ConvertMapToList {
        public static void main(String[] args) {
            Map<Integer, String> map = new HashMap<>();
            map.put(10, "apple");
            map.put(20, "orange");
            map.put(30, "banana");
            map.put(40, "watermelon");
            map.put(50, "dragonfruit");
            System.out.println("\n1. Export Map Key to List...");
            List<Integer> result = map.keySet().stream()
                    .collect(Collectors.toList());
            result.forEach(System.out::println);
            System.out.println("\n2. Export Map Value to List...");
            List<String> result2 = map.values().stream()
                    .collect(Collectors.toList());
            result2.forEach(System.out::println);
            System.out.println("\n3. Export Map Value to List..., say no to banana");
            List<String> result3 = map.keySet().stream()
                    .filter(x -> !"banana".equalsIgnoreCase(x))
                    .collect(Collectors.toList());
            result3.forEach(System.out::println);
        }
    }
    

    Output

    1. Export Map Key to List...
    50
    20
    40
    10
    30
    2. Export Map Value to List...
    dragonfruit
    orange
    watermelon
    apple
    banana
    3. Export Map Value to List..., say no to banana
    dragonfruit
    orange
    watermelon
    apple
    

    3.Java 8 - 将Map转换成List

    这个例子有点极端,map.entrySet()用来转换MapList

    ConvertMapToList.java

    package com.mkyong;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    //https://www.mkyong.com/java8/java-8-how-to-sort-a-map/
    public class ConvertMapToList {
        public static void main(String[] args) {
            Map<Integer, String> map = new HashMap<>();
            map.put(10, "apple");
            map.put(20, "orange");
            map.put(30, "banana");
            map.put(40, "watermelon");
            map.put(50, "dragonfruit");
            // split a map into 2 List
            List<Integer> resultSortedKey = new ArrayList<>();
            List<String> resultValues = map.entrySet().stream()
                    //sort a Map by key and stored in resultSortedKey
                    .sorted(Map.Entry.<Integer, String>comparingByKey().reversed())
                    .peek(e -> resultSortedKey.add(e.getKey()))
                    .map(x -> x.getValue())
                    // filter banana and return it to resultValues
                    .filter(x -> !"banana".equalsIgnoreCase(x))
                    .collect(Collectors.toList());
            resultSortedKey.forEach(System.out::println);
            resultValues.forEach(System.out::println);
        }
    }
    

    Output

    //resultSortedKey
    50
    40
    30
    20
    10
    //resultValues
    dragonfruit
    watermelon
    orange
    apple
    

     

    转载自:https://blog.csdn.net/hatsune_miku_/article/details/73435627

    展开全文
  • Map转换为List

    2019-11-15 10:19:02
    Map<String, String> map = new HashMap<>(); // Convert all Map keys to a List List result = new ArrayList(map.keySet()); // Convert all Map values to a ...List result2 = new ArrayList(map...

    Map<String, String> map = new HashMap<>();
    // Convert all Map keys to a List
    List result = new ArrayList(map.keySet());
    // Convert all Map values to a List
    List result2 = new ArrayList(map.values());
    // Java 8, Convert all Map keys to a List
    List result3 = map.keySet().stream()
    .collect(Collectors.toList());
    // Java 8, Convert all Map values to a List
    List result4 = map.values().stream()
    .collect(Collectors.toList());
    // Java 8, seem a bit long, but you can enjoy the Stream features like filter and etc.
    List result5 = map.values().stream()
    .filter(x -> !“apple”.equalsIgnoreCase(x))
    .collect(Collectors.toList());
    // Java 8, split a map into 2 List, it works!
    // refer example 3 below

    展开全文
  • java将Map转换为List

    2021-01-21 20:06:14
    Map数据转换为自定义对象的List,例如把map的key,value分别对应Person对象两个属性: List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey())) .
    1. 实例
    List<BlogComment> blogCommentListResult = new ArrayList<>(blogCommentMap.values());
    
    1. Map数据转换为自定义对象的List,例如把map的key,value分别对应Person对象两个属性:
    List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(e -> e.getKey()))
    		.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
    
    List<Person> list = map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue))
    		.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
    
    List<Person> list = map.entrySet().stream().sorted(Map.Entry.comparingByKey())
    	.map(e -> new Person(e.getKey(), e.getValue())).collect(Collectors.toList());
    

    以上三种方式不同之处在于排序的处理

    展开全文
  • 我们知道 HashMap不能直接排序。 TreeMap可以排序但是只能以键值对里面的key 排序,不能有value排序。 如果我们的需求恰好需要...以HashMap转换为list,并以value排序举例,TreeMap同理,只要换个名字就行。 代码奉上:
  • //创建map集合 Map<String,Integer> map=new HashMap<String,Integer>(); map.put(“a”,2); map.put(“b”,4); map.put(“c”,3); map.put(“d”,1); //先把map中储存在entry中的数据拿出
  • public static void retunKeyAndValue(Map<String,Object> map){ System.out.println("通过Map.entrySet遍历key和value"); for (Map.Entry<String, Object> entry : map.entrySet...
  • list,此代码视伪代码,单独不能运行,仅仅记录一下流程 /** * * 去重,整理,一个key多个value 此代码视伪代码,单独不能运行,仅仅记录一下流程 * * map为{img_path2=/bidAttachment/b10a3fe2-f25b-...
  • 定义Map与随机数 int[] arrInt = new int[1000]; Map<Integer,Integer> map = new HashMap<Integer,Integer>(); Random r = new Random(); 将随机数存入Map中 for(int i=0;i<1000;i++) { ...
  • Java实现将Map转换为List的小代码

    千次阅读 2009-12-24 13:26:00
    import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List;... public class ConvertMapToList { /** * 实现将HashMap转换成为ArrayList,并将map
  • Java实现将Map转换为List的小代码

    千次阅读 2007-10-31 21:45:00
    import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List;... public class ConvertMapToList { /** * 实现将HashMap转换成为ArrayList,并将ma
  • List排序、Map按照value值排序,以及list如何转换为mapmap如何转换为list 在写算法的时候,会频繁的用到list以及map的排序问题,当我们想对list进行排序,我们可以直接使用Collections类中的方法,比如说对list...
  • JDK 8 新特性stream流操作String转换为Map , List 转换为Map,String 转换为List 链式编程 Stream流操作 1.使用字符串将字符串转为list: //要求使用stream流的方式将下面字符串转换为List<Integer>形式,且按照...
  • List转换为List<Map>

    2014-11-04 22:02:33
    List转换为List<Map>, List转换为List<Object> list转化为 JsonArray ,list转换JsonArray
  • # T对象转换成map对象/List对象 * 或者map对象/List对象转换为T对象 - **把T对象转换为Map对象** - **Map对象转换为T对象** - **获得T对象的list中的一个 **
  • java List集合转换为List Map集合

    千次阅读 2018-04-20 11:47:14
    转换为List&lt;Map&lt;String,Object&gt;&gt; * @param list * @return */ private List&lt;Map&lt;String,Object&gt;&gt; convertListMap(List&lt;Object&gt; list).....

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,611
精华内容 644
关键字:

map转换为list