六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 108|回复: 0

Java collection 集合差操作

[复制链接]

升级  75%

129

主题

129

主题

129

主题

举人

Rank: 3Rank: 3

积分
425
 楼主| 发表于 2013-1-25 22:07:25 | 显示全部楼层 |阅读模式
package com.suypower.xssystem.temp;/* *电量数据同步 *@author chengYu *@param connection *@return arrayA,arrayB */import java.sql.Connection;import java.sql.Statement;import java.sql.ResultSet;import java.sql.*;import java.util.ArrayList;import com.suypower.xssystem.dao.dbconn;import java.util.List;import java.util.Collection;import org.apache.commons.collections.CollectionUtils;import java.util.Arrays;public class ReturnArray{  public ReturnArray(){}  public static void main(String[] args) {                 dbconn.isPool = false;                 String filename = null;                 if (args.length == 0)                 return;                 if (args[0] == null) {                 return;                 } else {                 filename = ".//" + args[0];                 }                 dbconn.setDataSourse(filename);                 dbconn db = new dbconn();    /**     * connect to db2 tb_gd_xl and tb_fxs     * param lyid and xldm     * return arrayA,arrayB     */    Connection con = null;    try    {      con = db.getConnection();      Statement stmt = con.createStatement();      String sqlA = "select xldm from tb_gd_xl where xldm is not null";      ResultSet rsA =stmt.executeQuery(sqlA);      ArrayList arrayA = new ArrayList();      ArrayList arrayB = new ArrayList();      while(rsA.next())      {        String xldm = rsA.getString("xldm");        arrayA.add(xldm);      }      String sqlB = "select lyid from tb_fxs where lyid is not null";      ResultSet rsB = stmt.executeQuery(sqlB);      while(rsB.next())      {        String lyid = rsB.getString("lyid");        arrayB.add(lyid);      }     /**      * 实现差操作      */     List a = Arrays.asList(arrayA.toArray());     List b = Arrays.asList(arrayB.toArray());     Collection subtract = CollectionUtils.subtract(a,b);     ArrayList arraylistC = new ArrayList(subtract);     for(int i=0;i<arraylistC.size();i++)     {       System.out.println((String)arraylistC.get(i));     }     /**      * 实现 插入 数据库 相应 节点 更新      */     }catch(Exception e)    {      e.printStackTrace();    }     finally       {        try {          if (con != null) {            con.close();          }        }        catch (SQLException ex) {          ex.printStackTrace();        }         }       }}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表