码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-01-17 00:04:28      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:zookeeper   boot   static   string   sum   etc   测试   lis   zook   

一、读取配置文件通用:

       

 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泛型通用,读取配置文件通用

标签:zookeeper   boot   static   string   sum   etc   测试   lis   zook   

原文地址:https://www.cnblogs.com/wangyaobk/p/8297686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!