场景:今日boss叫我把一个包含数千条数据的Excel表格导入数据库,要求去除重复数据,并保证性能。其中有两个字段可以判断该条数据是否唯一。
需求其实不难,使用工具,如ExcelUtil将数据导入,封装个成对象,通过两个字段进行比较,判断唯一,再存入集合中批量插入。
然而,在对象唯一性校验时,我使用了map集合的方式,虽然效果出来了,但是越看越觉得这个代码low。(如下是省略导入步骤,只做唯一校验)
Map map = new HashMap<> ();
HappyPupy happyPupy = new HappyPupy ();
happyPupy.setAge (18);
happyPupy.setName ("张三");
happyPupy.setScore ("50");
happyPupy.setTile ("小学");
map.put (happyPupy.getName ()+happyPupy.getScore (),happyPupy);
HappyPupy happyPupy1 = new HappyPupy ();
happyPupy1.setAge (20);
happyPupy1.setName ("张四");
happyPupy1.setScore ("60");
happyPupy1.setTile ("中学");
map.put (happyPupy1.getName ()+happyPupy1.getScore (),happyPupy1);
HappyPupy happyPupy2 = new HappyPupy ();
happyPupy2.setAge (20);
happyPupy2.setName ("张四");
happyPupy2.setScore ("60");
happyPupy2.setTile ("中学");
map.put (happyPupy2.getName ()+happyPupy2.getScore (),happyPupy2);
HappyPupy happyPupy3 = new HappyPupy ();
happyPupy3.setAge (22);
happyPupy3.setName ("张四");
happyPupy3.setScore ("60");
happyPupy3.setTile ("中学");
map.put (happyPupy3.getName ()+happyPupy3.getScore (),happyPupy3);
List<HappyPupy> list = new ArrayList<> ();
Set<Map.Entry<String, Object>> eSet = map.entrySet ();
Iterator<Map.Entry<String, Object>> it3 = eSet.iterator ();
while (it3.hasNext ()) {
list.add ((HappyPupy) it3.next ().getValue ());
}
...........
其中name和score组合为唯一条件,通过HashMap键不能重复的特性,将两个字段组合作为Key,对象作为值存入到map集合中,再将map集合转成List集合进行批量插入。
其实也没啥问题,功能是实现了,但是左看右看,就是觉得low,于是绞尽脑汁(网上白嫖)做了如下改进。
HappyPupy happyPupy = new HappyPupy ();
happyPupy.setAge (18);
happyPupy.setName ("张三");
happyPupy.setScore ("50");
happyPupy.setTile ("小学");
HappyPupy happyPupy1 = new HappyPupy ();
happyPupy1.setAge (20);
happyPupy1.setName ("张四");
happyPupy1.setScore ("60");
happyPupy1.setTile ("中学");
HappyPupy happyPupy2 = new HappyPupy ();
happyPupy2.setAge (20);
happyPupy2.setName ("张四");
happyPupy2.setScore ("60");
happyPupy2.setTile ("中学");
HappyPupy happyPupy3 = new HappyPupy ();
happyPupy3.setAge (22);
happyPupy3.setName ("张四");
happyPupy3.setScore ("60");
happyPupy3.setTile ("中学");
List<HappyPupy> read = new ArrayList<> ();
read.add (happyPupy);
read.add (happyPupy1);
read.add (happyPupy2);
read.add (happyPupy3);
//去重后的数据集合
ArrayList<HappyPupy> collect = read.parallelStream().
collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<> (Comparator.comparing(o -> o.getName () +":" + o.getAge ()))), ArrayList::new));
..........
这样子的代码是不是要简洁很多。如有更加优秀的方法,欢迎评论区讨论。