List转Map泛型通用,读取配置文件通用

Posted 程是成功的乘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了List转Map泛型通用,读取配置文件通用相关的知识,希望对你有一定的参考价值。

一、读取配置文件通用:

       

 1 public class ReadConfigureClass {
 2 
 3     private static final String server_port = "server.port";
 4 
 5     private static final String spring_dubbo_scan = "spring.dubbo.scan";
 6 
 7     public static String server;
 8     public static String spring;
 9 
10     static {
11         Properties properties = new Properties();
12         try {
13             properties.load(ReadConfigureClass.class.getClassLoader().getResourceAsStream("application.properties"));
14             server = (String) properties.get(server_port);
15             spring = (String) properties.get(spring_dubbo_scan);
16 
17         } catch (IOException e) {
18             e.printStackTrace();
19         }
20     }
21 }
22 
23 
24 ## 避免和 server 工程端口冲突
25 server.port=8081
26 
27 ## Dubbo 服务消费者配置
28 spring.dubbo.application.name=consumer
29 spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
30 spring.dubbo.scan=org.spring.springboot.dubbo

 

 

二、List转Map泛型通用:

  Java类:

 1 public class Parent {
 2     private Integer id;
 3 
 4     private String name;
 5 
 6     public Parent(Integer id, String name) {
 7         this.id = id;
 8         this.name = name;
 9     }
10 
11     public Integer getId() {
12         return id;
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19 }

测试类:

 

 1 package org.spring.springboot.dubbo;
 2 
 3 import org.junit.Test;
 4 
 5 import java.lang.reflect.Method;
 6 import java.util.ArrayList;
 7 import java.util.HashMap;
 8 import java.util.List;
 9 import java.util.Map;
10 
11 public class TestJavaFanXing {
12 
13 
14     @Test
15     public void test() {
16         List<Parent> list = new ArrayList<>();
17         for (int i = 0; i < 10000; i++) {
18             list.add(new Parent(i, "aaa" + i));
19         }
20         System.out.println(list.size());
21         Long start=System.currentTimeMillis();
22         Map<Long,Parent> map=list2Map(list, "getId", Parent.class);
23         System.out.println((System.currentTimeMillis()-start));
24         System.out.println(JSONObject.toJSON(map).toString());
25         //======================================================
26         System.out.println(ReadConfigureClass.server);
27         System.out.println(ReadConfigureClass.spring);
28     }
29 
30 
31 
32     public static <K, V> Map<K, V> list2Map(List<V> list, String keyMethodName, Class<V> c) {
33         Map<K, V> map = new HashMap<K, V>();
34         if (list != null) {
35             try {
36                 Method methodGetKey = c.getMethod(keyMethodName);
37                 for (int i = 0; i < list.size(); i++) {
38                     V value = list.get(i);
39                     @SuppressWarnings("unchecked")
40                     K key = (K) methodGetKey.invoke(list.get(i));
41                     map.put(key, value);
42                 }
43             } catch (Exception e) {
44                 throw new IllegalArgumentException("field can‘t match the key!");
45             }
46         }
47 
48         return map;
49     }
50 
51 
52 }

 

以上是关于List转Map泛型通用,读取配置文件通用的主要内容,如果未能解决你的问题,请参考以下文章

Eclipse 中的通用代码片段或模板

是否有通用方法通过C#中的反射填充各种泛型集合?

JAVA Collection接口中List Map 和Set的区别(转)

spring List,Set,Map,Properties,array的配置文件注入方式

更好地理解C#泛型

JSON反序列化泛型对象;泛型是变化的,如何写出通用代码?(源码分析)