<dependency>
<groupId>com.github.hioo520</groupId>
<artifactId>collections-plus</artifactId>
<version>1.4.0</version>
</dependency>
集合工具提取和填充器
也許對(duì)我們常常為這樣的問題苦惱(不寫重復(fù)的輪子提高你處理數(shù)據(jù)的能力和效率)
A.從一個(gè)Map數(shù)據(jù)中提取數(shù)據(jù)時(shí)get set 特別不方便 不煩把他轉(zhuǎn)換成 entity
使用這個(gè)方法直接轉(zhuǎn)換為對(duì)象類型 map--轉(zhuǎn)換--->entity >>>fillEntity(Map map, Object e) 返回對(duì)象類型(哈哈接下來你就好處理了)
B.對(duì)于已經(jīng)存在的List 我只想提取其中的某些數(shù)據(jù)一個(gè)或者多個(gè)key(哦還有就是我只是想提取某些key對(duì)應(yīng)的value對(duì)嘞 我還想去重)
你不妨嘗試下面方法:Set pickValue(List<E> list, String... parameter)--->提取某些key值(一個(gè)或者多個(gè)的key并且去重)
List<Map> pickList(List<Map> list, String... key)這個(gè)必須對(duì)于大的map我只是取特定的幾個(gè)key返回給我就好了
不知道 從對(duì)象中取出幾個(gè)值可不可以呢
Map pickValue(E e, String... key)而且他可以返回你想要的Map
C.對(duì)于對(duì)象拷貝我們知道用到的不是很多,可是 每次到Excel時(shí)無意間發(fā)覺我控制不了在寫一個(gè)
List<E> fillClass(List<T> list, Object obj, String... param)
不好意思上面的這個(gè)方法是拷貝整個(gè)對(duì)象(其實(shí)這個(gè)就是解決excel從一個(gè)list對(duì)象到另一個(gè)list對(duì)象)
E fillClass(T e, Object obj, String... param)
這個(gè)方法才是對(duì)象對(duì)象拷貝居然支持(只拷貝其中的某些段)
D.再次擲出大招.上面的方法都支持自定義(比如 我提取出來的Map我想改動(dòng)key的大小寫 我想對(duì)時(shí)間做統(tǒng)一格式 我想對(duì)空值 空字符串 null處理 你想要的應(yīng)該也都有)
E. 至于效率嗎 看看下面測(cè)試結(jié)果就知道了(只是豐富了你的開發(fā)方式)
1.工具說明
- 技術(shù) : 反射+泛型+集合+可變參數(shù)+Threadlocal
- 優(yōu)點(diǎn) :
支持所有數(shù)據(jù)類型
支持動(dòng)態(tài)緩存
支持時(shí)間和key自定義
支持多線程并發(fā)
支持遞歸提取父類屬性進(jìn)行取值塞值
支持空值(防止null報(bào)異常)
2.使用方法
FillFactory.beach().需要方法見下圖()
PickFactoryTest.beach().需要方法見下圖()
3.效率效果
一千萬數(shù)據(jù)map<String,Object>數(shù)據(jù)填充為對(duì)象(list<Object>)需要27秒(不存在棧溢出)
4.測(cè)試類
/**
* tips
*
* @author:hihuzi 2018/7/23 9:21
*/
public class FillFactoryTest implements Runnable {
private MockHttpServletRequest request;
private FillFactory fillTool;
private static Map map;
private static String tip;
public void setMap(Map map) {
this.map = map;
}
public void setTip(String tip) {
this.tip = tip;
}
@Before
public void setUp() {
request = new MockHttpServletRequest();
request.setCharacterEncoding("utf-8");
fillTool = new FillTool();
}
/**
* tips HttpServletRequest-->MAP
*
* @author:hihuzi 2018/7/23 15:05
*/
@Test
public void fill() {
request.setParameter("integerMax", "123456");
request.setParameter("stringMax", "你好師姐!!!");
request.setParameter("longMax", "12.3");
request.setParameter("intMin", " ");
request.setParameter("intMin", " ");
request.setParameter("doubleMin", "");
/**tips 填充到request---->Map*/
Map map = FillFactory.batch().fill(request);
map.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
System.out.println("");
/**tips 舍棄掉特定字段*/
Map map0 = FillFactory.batch().fill(request, "stringMax");
map0.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
System.out.println("");
/**tips 舍棄掉空值*/
Map map1 = FillFactory.batch().fill(request, new FillConfig(FillConfig.SaveStyleEnum.REMOVE_NULL_EMPTY));
map1.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
System.out.println("");
/**tips 默認(rèn)屬性不舍棄空值*/
Map map2 = FillFactory.batch().fill(request, new FillConfig(FillConfig.SaveStyleEnum.DEFAULT));
map2.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
System.out.println("");
/**tips 舍棄空值 并且去掉特定字段*/
Map map3 = FillFactory.batch().fill(request, new FillConfig(FillConfig.SaveStyleEnum.REMOVE_NULL_EMPTY), "stringMax");
map3.forEach((o, o2) -> System.out.print(o + "=" + o2 + " "));
}
/**
* tips HttpServletRequest--> obj
* tips 對(duì)于空字符串處理
*
* @author:hihuzi 2018/6/14 14:50
*/
@Test
public void fill_entity_request() throws Exception {
request.setParameter("booleanMax", "");
request.setParameter("byteMax", "");
request.setParameter("shortMax", "");
request.setParameter("integerMax", "");
request.setParameter("longMax", "");
request.setParameter("floatMax", "");
request.setParameter("doubleMax", "");
request.setParameter("stringMax", " ");
request.setParameter("bigdecimalMax", "");
request.setParameter("dateMax", "");
request.setParameter("booleanMin", "");
request.setParameter("charMin", "");
request.setParameter("byteMin", "");
request.setParameter("shortMin", "");
request.setParameter("intMin", "");
request.setParameter("longMin", "");
request.setParameter("floatMin", "");
request.setParameter("doubleMin", "");
long start = System.currentTimeMillis();
TestBean map1 = null;
for (int i = 0; i < 10000000; i++) {
map1 = FillFactory.batch().fillEntity(request, new TestBean());
}
long end = System.currentTimeMillis();
System.err.println("------>一千萬 耗時(shí)" + (end - start) / 1000 + "秒<------");
System.out.println(Arrays.asList(map1));
}
/**
* tips HttpServletRequest--> obj
*
* @author:hihuzi 2018/6/14 14:50
*/
@Test
public void fill_entity_request1() throws Exception {
request.setParameter("booleanMax", "true");
request.setParameter("byteMax", "1");
request.setParameter("shortMax", "129");
request.setParameter("integerMax", "123456");
request.setParameter("longMax", "132542435");
request.setParameter("floatMax", "12.9");
request.setParameter("doubleMax", "3.55");
request.setParameter("stringMax", "你好師姐!!!");
request.setParameter("bigdecimalMax", "9825485.61551");
request.setParameter("dateMax", "2012-12-12");
request.setParameter("booleanMin", "true");
request.setParameter("charMin", "a");
request.setParameter("byteMin", "2");
request.setParameter("shortMin", "5");
request.setParameter("intMin", "55");
request.setParameter("longMin", "555");
request.setParameter("floatMin", "0.9");
request.setParameter("doubleMin", "1.94");
TestBean map1 = null;
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
map1 = FillFactory.batch().fillEntity(request, new TestBean());
}
long end = System.currentTimeMillis();
System.err.println("------>一千萬 耗時(shí)" + (end - start) / 1000 + "秒<------");
System.out.println(Arrays.asList(map1).toString());
}
/**
* tips HttpServletRequest--> obj
* tips 針對(duì)不同格式針對(duì)的處理
*
* @author:hihuzi 2018/6/14 14:50
*/
@Test
public void fill_entity_request2() throws Exception {
request.setParameter("stringMax", "你好師姐!!!");
request.setParameter("dateMax", "2012-12-12");
TestBean map = null;
map = FillFactory.batch().fillEntity(request, new TestBean(),
new FillConfig());
System.out.println(Arrays.asList(map).toString());
map = FillFactory.batch().fillEntity(request, new TestBean(),
new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")));
System.out.println(Arrays.asList(map).toString());
request.setParameter("dateMax", "2012-12-12 22:21:20");
map = FillFactory.batch().fillEntity(request, new TestBean(),
new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd HH:mm:ss")));
System.out.println(Arrays.asList(map).toString());
}
/**
* tips Map--> obj
* tips 針對(duì)不同格式針對(duì)的處理
*
* @author:hihuzi 2018/6/14 14:50
*/
@Test
public void fill_entity_map() throws Exception {
Map map = new HashMap(20);
map.put("booleanMax", "true");
map.put("byteMax", "1");
map.put("shortMax", "129");
map.put("integerMax", "123456");
map.put("longMax", "132542435");
map.put("floatMax", "12.99");
map.put("stringMax", "你好師姐!!!");
map.put("bigdecimalMax", "9825485.6");
map.put("dateMax", "2012-12-12");
map.put("booleanMin", "true");
map.put("charMin", "a");
map.put("byteMin", "2");
map.put("shortMin", "5");
map.put("intMin", "55");
map.put("longMin", "555");
map.put("floatMin", "0.9");
map.put("doubleMin", "1.94");
TestBean bean = FillFactory.batch().fillEntity(map, new TestBean());
Map map1 = new HashMap(5);
/**tips 從對(duì)象中取出map*/
Map map2 = FillFactory.batch().fillMap(bean, map1);
System.out.println(bean.toString());
map2.forEach((o, o2) -> System.out.print(o + " " + o2));
}
/**
* tips 針對(duì)不同時(shí)間格式處理不同時(shí)間配置(錯(cuò)誤的屬性直接丟掉)
*/
@Test
public void fill_entity_map0() throws Exception {
Map map = new HashMap(20);
map.put("stringMax", "你好師姐!!!");
map.put("dateMax", "2012-12-12");
/**tips 錯(cuò)誤的屬性直接丟掉*/
map.put("dat32eMax", "2012-12-12");
TestBean bean = FillFactory.batch().fillEntity(map, new TestBean());
System.out.println(bean.toString() + "hashCode" + bean.hashCode());
map.put("dateMax", "2012-12-12 24:23:22");
TestBean bean0 = FillFactory.batch().fillEntity(map, new TestBean(),
new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd HH:mm:ss")));
System.out.println(bean0.toString() + "hashCode" + bean.hashCode());
Map map1 = new HashMap(5);
/**tips 從對(duì)象中取出map*/
Map map2 = FillFactory.batch().fillMap(bean0, map1,
new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")));
map2.forEach((o, o2) -> System.out.print(o + "-->" + o2));
}
/**
* tips List<Map> --> E --> List<E>
* tips 針對(duì)不同格式對(duì)應(yīng)處理
*
* @author: hihuzi 2018/6/26 14:51
*/
@Test
public void fill_entity_list() throws Exception {
List list = new ArrayList();
List list0 = new ArrayList();
Map map = new HashMap(20);
map.put("booleanMax", "true");
map.put("byteMax", "1");
map.put("shortMax", "129");
map.put("integerMax", "123456");
map.put("longMax", "132542435");
map.put("floatMax", "12.99");
map.put("doubleMax", "3.55");
map.put("stringMax", "你好師姐!!!");
map.put("bigdecimalMax", "9825485.6");
map.put("dateMax", "2012-12-12");
map.put("booleanMin", "true");
map.put("charMin", "a");
map.put("byteMin", "2");
map.put("shortMin", "5");
map.put("intMin", "55");
map.put("longMin", "555");
map.put("floatMin", "0.9");
map.put("doubleMin", "1.94");
list.add(map);
List<TestBean> bean = FillFactory.batch().fillEntity(list, new TestBean());
/**tips 特殊的時(shí)間格式處理*/
map.put("dateMax", "2012!12@12#12-12:12");
list0.add(map);
List<TestBean> bean0 = FillFactory.batch().fillEntity(list0, new TestBean(),
new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy!MM@dd#HH-mm:ss")));
System.out.println(bean.get(0).toString());
System.out.println(bean0.get(0).toString());
// long start = System.currentTimeMillis();
// List<TestBean> bean3;
// for (int i = 0; i < 1000000; i++) {
// bean3 = FillFactory.batch().fillEntity(list, new TestBean(),
// new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy!MM@dd#HH-mm:ss")));
// }
// long end = System.currentTimeMillis();
// System.err.println("------>一百萬 耗時(shí)" + (end - start) / 1000 + "秒<------");
}
/**
* tips list<String> --> E --> list<E> 針對(duì)數(shù)據(jù)庫與實(shí)體類名有區(qū)別
*
* @author:hihuzi 2018/6/26 14:51
*/
@Test
public void fill_map() throws Exception {
Map map = new HashMap(20);
map.put("stringMax", "你好師姐!!!");
map.put("dateMax", "2012-12-12");
map.put("booleanMin", "");
TestBean bean = FillFactory.batch().fillEntity(map, new TestBean());
System.out.println(bean);
Map map1 = new HashMap(5);
/**tips 從對(duì)象中取出map*/
map1 = FillFactory.batch().fillMap(bean, map1);
map1.forEach((o, o2) -> System.out.print(o + "-->" + o2 + " "));
System.out.println("");
System.out.println("fillMap從對(duì)象中取出不為空的屬性 并且時(shí)間自定義");
map1 = FillFactory.batch().fillMap(bean, map1,
new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd HH:mm:ss")));
map1.forEach((o, o2) -> System.out.print(o + "-->" + o2 + " "));
}
/**
* tips E --> Map 針對(duì)E與map進(jìn)行填充
*
* @parameter: E e
* @parameter: Map map
* @parameter: E
* @Author: hihuzi 2018/6/26 14:51
*/
@Test
public void list_to_entity() throws Exception {
List list = new ArrayList();
list.add("true");
list.add("1");
list.add("129");
list.add("123456");
list.add("132542435");
list.add("12.99");
list.add("3.55");
list.add("你好師姐!!!");
list.add("9825485.6");
list.add("2012-12-12");
list.add("true");
list.add("a");
list.add("2");
list.add("5");
list.add("55");
list.add("555");
list.add("0.9");
list.add("1.94");
List<TestBean> bean = FillFactory.batch().listToEntity(list, new TestBean());
System.out.println(bean.get(0).toString());
// long start = System.currentTimeMillis();
// for (int i = 0; i < 10000000; i++) {
// List<TestBean> bean0 = FillFactory.batch().listToEntity(list, new TestBean());
// }
// long end = System.currentTimeMillis();
// System.err.println("------>一千萬 耗時(shí)" + (end - start) / 1000 + "秒<------");
Map<String, Map<String, TypeCache>> classCache = ClassCache.cache;
classCache.forEach((s, typeCache) -> System.err.println(typeCache.size()));
}
@Override
public void run() {
TestBean bean = null;
try {
bean = FillFactory.batch().fillEntity(map, new TestBean(),
new FillConfig(FillBase.DateStyleEnum.DEFAULT.setFormartStyle(this.tip)));
System.out.println(bean.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* tips 多線程測(cè)試
*
* @parameter:
* @return:
* @author: hihuzi 2018/10/21 3:51
*/
@Test
public void mains() {
Map map = new HashMap(1);
map.put("dateMax", "333-33-33");
FillFactoryTest test0 = new FillFactoryTest();
test0.setMap(map);
test0.setTip("yyyy-MM-dd");
Map maps = new HashMap(1);
maps.put("dateMax", "222!22@22#22-22:22");
FillFactoryTest test = new FillFactoryTest();
test0.setMap(maps);
test0.setTip("yyyy!MM@dd#HH-mm:ss");
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 666; i++) {
Thread thread;
if (i % 2 == 0) {
thread = new Thread(test0, "" + i);
} else {
thread = new Thread(test, "" + i);
}
threads.add(thread);
}
for (Thread thread : threads) {
thread.start();
}
}
public static void main(String[] args) {
Map map = new HashMap(1);
map.put("dateMax", "333-33-33");
FillFactoryTest test0 = new FillFactoryTest();
test0.setMap(map);
test0.setTip("yyyy-MM-dd");
Map maps = new HashMap(1);
maps.put("dateMax", "222!22@22#22-22:22");
FillFactoryTest test = new FillFactoryTest();
test0.setMap(maps);
test0.setTip("yyyy!MM@dd#HH-mm:ss");
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 666; i++) {
Thread thread;
if (i % 2 == 0) {
thread = new Thread(test0, "" + i);
} else {
thread = new Thread(test, "" + i);
}
threads.add(thread);
}
for (Thread thread : threads) {
thread.start();
}
}
}
執(zhí)行結(jié)果如下:
TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.99, doubleMax=3.55, stringMax=你好師姐!!!, bigdecimalMax=9825485.6, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)
TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.99, doubleMax=3.55, stringMax=你好師姐!!!, bigdecimalMax=9825485.6, dateMax=Wed Dec 12 12:12:12 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)
TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好師姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)hashCode-286194562
TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好師姐!!!, bigdecimalMax=null, dateMax=Thu Dec 13 00:23:22 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)hashCode-286194562
dateMax-->2012-12-13charMin--> shortMin-->0intMin-->0doubleMin-->0.0stringMax-->你好師姐!!!byteMin-->0floatMin-->0.0booleanMin-->falselongMin-->0TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好師姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)
dateMax-->2012-12-12 charMin--> shortMin-->0 intMin-->0 doubleMin-->0.0 stringMax-->你好師姐!!! byteMin-->0 floatMin-->0.0 booleanMin-->false longMin-->0
fillMap從對(duì)象中取出不為空的屬性 并且時(shí)間自定義
dateMax-->2012-12-12 00:00:00 charMin--> shortMin-->0 intMin-->0 doubleMin-->0.0 stringMax-->你好師姐!!! byteMin-->0 floatMin-->0.0 booleanMin-->false longMin-->0 [TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax= , bigdecimalMax=null, dateMax=null, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)]
------>一千萬 耗時(shí)9秒<------
------>一千萬 耗時(shí)32秒<------
[TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.9, doubleMax=3.55, stringMax=你好師姐!!!, bigdecimalMax=9825485.61551, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)]
[TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好師姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)]
[TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好師姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)]
[TestBean(booleanMax=null, byteMax=null, shortMax=null, integerMax=null, longMax=null, floatMax=null, doubleMax=null, stringMax=你好師姐!!!, bigdecimalMax=null, dateMax=Wed Dec 12 22:21:20 CST 2012, booleanMin=false, charMin= , byteMin=0, shortMin=0, intMin=0, longMin=0, floatMin=0.0, doubleMin=0.0, character=null)]
longMax=12.3 integerMax=123456 intMin= doubleMin= stringMax=你好師姐!!!
longMax=12.3 integerMax=123456 intMin= doubleMin=
stringMax=你好師姐!!! longMax=12.3 integerMax=123456
longMax=12.3 integerMax=123456 intMin= doubleMin= stringMax=你好師姐!!!
longMax=12.3 integerMax=123456 TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.99, doubleMax=null, stringMax=你好師姐!!!, bigdecimalMax=9825485.6, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)
longMax 132542435shortMin 5stringMax 你好師姐!!!byteMin 2floatMax 12.99integerMax 123456booleanMax truedateMax 2012-12-12charMin abyteMax 1intMin 55doubleMin 1.94bigdecimalMax 9825485.6floatMin 0.9booleanMin trueshortMax 129longMin 555TestBean(booleanMax=true, byteMax=1, shortMax=129, integerMax=123456, longMax=132542435, floatMax=12.99, doubleMax=3.55, stringMax=你好師姐!!!, bigdecimalMax=9825485.6, dateMax=Wed Dec 12 00:00:00 CST 2012, booleanMin=true, charMin=a, byteMin=2, shortMin=5, intMin=55, longMin=555, floatMin=0.9, doubleMin=1.94, character=null)
pick
/**
* tips 測(cè)試工具
*
* @author: hihuzi 2018/7/20 8:42
*/
public class PickFactoryTest implements Runnable {
private String tip;
/**
* tips 時(shí)間格式化 多級(jí)父類
*
* @parameter:
* @return:
* @author: hihuzi 2018/10/18 11:16
*/
@Test
public void pick0() throws Exception {
List<TestBean> list = new ArrayList<>();
for (int i = 2; i < 3; i++) {
TestBean userPost = new TestBean();
userPost.setName("你好師姐");
userPost.setId(12345 * i + "");
userPost.setEmail(null);
userPost.setAddress(" ");
list.add(userPost);
}
/**tips 時(shí)間格式化 多級(jí)父類*/
List<Map> batch7 = PickFactory.batch().pick(list, new PickConfig(PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY,
PickBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")), "date", "date0", "date1");
batch7.forEach(map -> System.out.println(map));
List<Map> batch6 = PickFactory.batch().pick(list, new PickConfig(PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY,
PickBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")), "date", "date0", "date1");
batch6.forEach(map -> System.out.println(map));
List<Map> batch5 = PickFactory.batch().pick(list, new PickConfig(PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY,
PickBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd HH:mm:ss")), "date", "date0", "date1");
batch5.forEach(map -> System.out.println(map));
List<Map> batch4 = PickFactory.batch().pick(list, new PickConfig(PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "date", "date0", "date1");
batch4.forEach(map -> System.out.println(map));
}
@Test
public void pick() throws Exception {
List<TestBean> list = new ArrayList<>();
for (int i = 2; i < 3; i++) {
TestBean userPost = new TestBean();
userPost.setName("你好師姐");
userPost.setId(12345 * i + "");
userPost.setEmail(null);
userPost.setAddress(" ");
list.add(userPost);
}
/**tips 默認(rèn)轉(zhuǎn)態(tài)*/
List<Map> batch0 = PickFactory.batch().pick(list, "id", "name", "email", "address");
batch0.forEach(map -> System.out.println(map));
/**tips 和 默認(rèn)一樣 首字母大寫*/
List<Map> batch = PickFactory.batch().pick(list, new PickConfig(
PickBase.ReturnNameEnum.INITIAL_CAPITAL), "id", "name", "email", "date");
batch.forEach(map -> System.out.println(map));
/** tips 空值丟掉(null 或者 "" " ") 并且全部大寫*/
List<Map> batch3 = PickFactory.batch().pick(list, new PickConfig(
PickBase.ReturnNameEnum.UPPER_CASE,
PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "id", "name", "email", "date");
batch3.forEach(map -> System.out.println(map));
/**tips 空值不丟掉 并且全部小寫*/
List<Map> batch2 = PickFactory.batch().pick(list, new PickConfig(
PickBase.ReturnNameEnum.LOWER_CASE), "id", "name", "email", "date", "address");
batch2.forEach(map -> System.out.println(map));
/**tips 空值不丟掉 重新命名Key*/
List<Map> batch4 = PickFactory.batch().pick(list, new PickConfig(
PickBase.ReturnNameEnum.CUSTOM_SUFFIX.setKey("我就是我!!")), "id", "name", "email", "date", "address");
batch4.forEach(map -> System.out.println(map));
/**tips 時(shí)間格式化*/
List<Map> batch5 = PickFactory.batch().pick(list, new PickConfig(
PickBase.DateStyleEnum.DEFAULT.setFormartStyle("yyyy-MM-dd")), "date", "date0");
batch5.forEach(map -> System.out.println(map));
}
/**
* tips 同一對(duì)象集合 返回選定字段 返回value(去重)
*
* @author: hihuzi 2018/4/30 15:49
*/
@Test
public void pickValue() throws Exception {
List<TestBean> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
TestBean userPost = new TestBean();
userPost.setName("你好師姐" + i);
userPost.setId(12345 * i + "");
userPost.setEmail(null);
userPost.setAddress(" ");
list.add(userPost);
}
/**tips 默認(rèn)設(shè)置*/
Set batch1 = PickFactory.batch().pickValue(list, "id", "name", "email");
System.out.println(Arrays.asList(batch1).toString());
/**tips (去掉 NUll 和 "" or " ")*/
Set batch = PickFactory.batch().pickValue(list, new PickConfig(
PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "id", "name", "email", "address");
System.out.println(Arrays.asList(batch).toString());
}
/**
* tips 單個(gè)對(duì)象 返回選定字段
*
* @author: hihuzi 2018/4/30 15:49
*/
@Test
public void pickValue0() throws Exception {
TestBean bean = new TestBean();
bean.setName("你好師姐");
bean.setId(UUID.randomUUID().toString());
bean.setEmail("");
bean.setAddress(UUID.randomUUID().toString().substring(32) + "@163.com");
/**tips 默認(rèn) 保留 空值*/
Map batch0 = PickFactory.batch().pickValue(bean, "id", "name", "email", "date", "address");
System.out.println(batch0.toString());
/**tips 保留 空值*/
Map batch1 = PickFactory.batch().pickValue(bean, new PickConfig(
PickBase.ReturnNameEnum.DEFAULT), "id", "name", "email", "date", "address");
System.out.println(batch1.toString());
/**tips 舍棄 空值*/
Map batch = PickFactory.batch().pickValue(bean, new PickConfig(
PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "id", "name", "email", "date", "address");
System.out.println(batch.toString());
Map<String, Map<String, TypeCache>> classCache = ClassCache.cache;
classCache.forEach((s, typeCacheMap) -> System.err.println(typeCacheMap.size()));
}
/**
* tips 單個(gè)對(duì)象 返回選定字段
*
* @author: hihuzi 2018/4/30 15:49
*/
@Test
public void pickMap() throws Exception {
Map bean = new HashMap(5);
bean.put("id", UUID.randomUUID());
bean.put("name", "你好師姐");
bean.put("age", "");
bean.put("email", "54465@163.com");
/**tips 默認(rèn) 保留 空值*/
Map batch0 = PickFactory.batch().pickMap(bean, "id", "name", "email", "age");
System.out.println(batch0.toString());
/**tips 保留 空值*/
Map batch1 = PickFactory.batch().pickMap(bean, new PickConfig(
PickBase.ReturnNameEnum.DEFAULT), "id", "name", "email", "age");
System.out.println(batch1.toString());
/**tips 舍棄 空值*/
Map batch = PickFactory.batch().pickMap(bean, new PickConfig(
PickBase.SaveStyleEnum.REMOVE_NULL_EMPTY), "id", "name", "email", "age");
System.out.println(batch.toString());
Map<String, Map<String, TypeCache>> classCache = ClassCache.cache;
classCache.forEach((s, typeCacheMap) -> System.err.println(typeCacheMap.size()));
}
@Override
public void run() {
TestBean bean = new TestBean();
bean.setDate(new Date());
try {
Map map = PickFactory.batch().pickValue(bean,
new PickConfig(PickBase.DateStyleEnum.DEFAULT.setFormartStyle(this.tip)), "date");
System.out.println(map.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
public void setTip(String tip) {
this.tip = tip;
}
// @Test
// public void mutlThreadTest() {
//
// PickFactoryTest test0 = new PickFactoryTest();
// test0.setTip("yyyy-MM-----dd");
// Map maps = new HashMap(1);
// maps.put("dateMax", "2018!22@22#22-22:22");
// PickFactoryTest test = new PickFactoryTest();
// test.setTip("yyyy!MM@dd#HH-mm:ss");
// List<Thread> threads = new ArrayList<>();
// for (int i = 0; i < 666; i++) {
// Thread thread;
// if (i % 2 == 0) {
// thread = new Thread(test0, "" + i);
// } else {
// thread = new Thread(test, "" + i);
// }
// threads.add(thread);
// }
// for (Thread thread : threads) {
//
// thread.start();
// }
// }
// public static void main(String[] args) {
//
// PickFactoryTest test0 = new PickFactoryTest();
// test0.setTip("yyyy-MM-----dd");
// Map maps = new HashMap(1);
// maps.put("dateMax", "2018!22@22#22-22:22");
// PickFactoryTest test = new PickFactoryTest();
// test.setTip("yyyy!MM@dd#HH-mm:ss");
// List<Thread> threads = new ArrayList<>();
// for (int i = 0; i < 666; i++) {
// Thread thread;
// if (i % 2 == 0) {
// thread = new Thread(test0, "" + i);
// } else {
// thread = new Thread(test, "" + i);
// }
// threads.add(thread);
// }
// for (Thread thread : threads) {
//
// thread.start();
// }
// }
}
測(cè)試結(jié)果如下
{name=你好師姐, date=2018-11-06, id=cc93df6b-17fe-4338-8dd0-c1bd8bbbe4a5, address=ed15@163.com, email=}
{name=你好師姐, date=2018-11-06, id=cc93df6b-17fe-4338-8dd0-c1bd8bbbe4a5, address=ed15@163.com, email=}
{name=你好師姐, date=2018-11-06, id=cc93df6b-17fe-4338-8dd0-c1bd8bbbe4a5, address=ed15@163.com}
[[0, null, 49380, 24690, 37035, 你好師姐0, 12345, 你好師姐4, 你好師姐3, 你好師姐2, 你好師姐1]]
[[37035, 你好師姐0, 你好師姐4, 你好師姐3, 你好師姐2, 你好師姐1, 0, 49380, 24690, 12345]]
{name=你好師姐, id=95a8d201-b307-4a7d-a093-a6759250817e, email=54465@163.com, age=}
{name=你好師姐, id=95a8d201-b307-4a7d-a093-a6759250817e, email=54465@163.com, age=}
{name=你好師姐, email=54465@163.com, id=95a8d201-b307-4a7d-a093-a6759250817e}
{name=你好師姐, id=24690, address= , email=}
{Id=24690, Email=, Date=2018-11-06, Name=你好師姐}
5
{DATE=2018-11-06, ID=24690, NAME=你好師姐}
{name=你好師姐, date=2018-11-06, id=24690, address= , email=}
{我就是我!!id=24690, 我就是我!!name=你好師姐, 我就是我!!date=2018-11-06, 我就是我!!email=, 我就是我!!address= }
{date=2018-11-06, date0=2018-11-06}
{date=2018-11-06, date1=2018-11-06, date0=2018-11-06}
{date=2018-11-06, date1=2018-11-06, date0=2018-11-06}
{date=2018-11-06 22:45:57, date1=2018-11-06 22:45:57, date0=2018-11-06 22:45:57}
{date=2018-11-06, date1=2018-11-06, date0=2018-11-06}
5.readme
也是第一次在這里發(fā)點(diǎn)東西,希望有夢(mèng)想盆友多多指點(diǎn)
---hihuzi 2018-10-30 pm
如若有不足之處多多建議!
bug無處有 鹿死誰的手!!
---hi@hu.zi 20.19.3.12.10.05 am
4.地址
https://github.com/hioo520/collections-plug.git
https://gitee.com/hihuzi-top/collections-plug.git
<dependency>
<groupId>com.github.hioo520</groupId>
<artifactId>collections-plus</artifactId>
<version>1.4.0</version>
</dependency>