关于导入数据到Excel中对数据库进行去重以及对导入的Excel文件进行去重

插入到数据库去重:

1.将你循环读取的List进行遍历

2.在你即将插入到数据库的方法之前获取你需要查询的数据,执行查询方法

1 devList=deviceDao.findDevice(device.getRfid());
2              if(devList.size()>0){
3                       messageStr = "数据重复,请重新导入!";
4 
5              }else{
6                        deviceDao.save(device);
7                         messageStr = "数据导入成功!";
8                     }

对导入的Excel文件去重:

(1)循环List中的元素对于重复的进行删除

   

1 //循环读取的devList
2 for  ( int  i  =   0 ; i  <  devList.size()  -   1 ; i ++ )  {       
3       for  ( int  j  =  devList.size()  -   1 ; j  >  i; j -- )  {       
4            if  (devList.get(j).equals(devList.get(i)))  {       
5              devList.remove(j);       
6             }        
7         }        
8       }        

(2)通过HashSet去除重复元素

1 //同上,list为获取到Excel数据的list
2 HashSet h = new HashSet(list);   
3 list.clear();   
4 list.addAll(h);

(3)遍历List,看数据是否存在,使用contain

1         List listTemp = new ArrayList();  
2         for(int i=0;i<list.size();i++){  
3             if(!listTemp.contains(list.get(i))){  
4                 listTemp.add(list.get(i));  
5             }  
6         }  
原文地址:https://www.cnblogs.com/jbml-154312/p/6951622.html