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 }

 

posted @ 2018-01-16 17:18  wanglittlehandsome  阅读(1121)  评论(0编辑  收藏  举报