简体   繁体   English

如何从对象的ArrayList中提取唯一字段?

[英]How to extract unique fields from an ArrayList of objects?

Okay, I have been banging my head for this question. 好吧,我一直在想这个问题。 I feel the solution may be simple but I'm confused. 我觉得解决方案可能很简单,但我很困惑。 Here goes: 开始:

I have an ArrayList of objects of type DbConnection . 我有一个ArrayList类型的对象DbConnection Each DbConnection object has the fields- DbName, SchemaName, AppId, Host, OsUser 每个DbConnection对象都具有以下字段-DbName,SchemaName,AppId,Host,OsUser

Eg. 例如。 sample rows: 样本行:

Database Schema ApplId Host OsUser 数据库架构ApplId主机OsUser


EGPRD SYSTEM AAA dh7y7hdu oracle EGPRD系统AAA dh7y7hdu甲骨文
EGPRD SYSTEM AAA d6f7d6fd linux EGPRD系统AAA d6f7d6fd Linux
EGPRD ADM RDA d6f7d6fd linux EGPRD ADM RDA d6f7d6fd linux
SOPRD DBLINK ACT fdf7f87e8 oracle SOPRD DBLINK ACT fdf7f87e8甲骨文

This ArrayList can have around 1000 objects like the above according to the application. 根据应用程序,此ArrayList可以具有大约1000个上述对象。 Now, from this ArrayList, I want to extract those rows which are of a particular app id / database / schema combination and write this to a table in the database (table has fields database, schema, applId). 现在,我要从此ArrayList中提取具有特定应用程序ID /数据库/架构组合的那些行,并将其写入数据库中的表中(该表具有字段database,schema,applId)。 It should extract that information from the arraylist and save in the table. 它应该从arraylist中提取该信息并保存在表中。

So this table will look like this when unique rows are inserted: 因此,当插入唯一行时,该表将如下所示:

Request Id Database Schema ApplId 请求ID数据库架构ApplId


1234 EGPRD SYSTEM AAA 1234 EGPRD系统AAA
1234 EGPRD ADM RDA 1234 EGPRD ADM RDA
1234 SOPRD DBLINK ACT 1234 SOPRD DBLINK ACT

1234 is the request id that has been generated when all the 1000 rows were updated in some other table. 1234是在其他表中更新所有1000行时生成的请求ID。

How do I pick out unique combination of values from an arraylist of objects? 如何从对象数组列表中选择值的唯一组合? I mean each object has many fields (db, schema, appid, host, osuser) but I need only three combinations (db, schema, appid), so unique combinations will be inserted in the database table. 我的意思是每个对象都有许多字段(db,schema,appid,host,osuser),但是我只需要三个组合(db,schema,appid),因此唯一的组合将插入数据库表中。

If you want to extract unique fields, you need to create a function that goes through your list , item by item, and checks if there are any item that are 'equal' to the one you're currently on. 如果要提取唯一字段,则需要创建一个函数,该函数逐项遍历列表 ,并检查是否有任何项与您当前使用的项“相等”。

(You also need to skip the one on the same index of the list) (您还需要跳过列表相同索引上的一个)

for (i = 0; i < list.size(); ++i) {
    for (j = 0; i < list.size(); ++j) { 
        if (list[i] == list[j] && i != j) {
            // Do your stuff here.
        }
    }
}

You could overload the '==' operator if this is a custom class, or you could simply check one attribute like the name, etc. 如果这是一个自定义类,则可以重载'=='运算符,也可以只检查一个属性,例如名称等。

if (list[i].requestID == list[j].requestID && i != j) { ...

Here is your DbConnection class. 这是您的DbConnection类。 You have to overwrite the equal method to check each element is equal or not. 您必须重写equal方法,以检查每个元素是否相等。 DbConnection.java DbConnection.java

public class DbConnection {

    private String DbName;
    private String SchemaName;
    private String ApplId;
    private String Host;
    private String OsUser;

    public DbConnection(String DbName, String SchemaName, String ApplId, String Host, String OsUser) {
        this.DbName = DbName;
        this.SchemaName = SchemaName;
        this.ApplId = ApplId;
        this.Host = Host;
        this.OsUser = OsUser;
    }

    public String getDbName() {
        return DbName;
    }

    public String getSchemaName() {
        return SchemaName;
    }

    public String getApplId() {
        return ApplId;
    }

    public String getHost() {
        return Host;
    }

    public String getOsUser() {
        return OsUser;
    }

    @Override
    public boolean equals(Object obj) {

        if (obj instanceof DbConnection) {
            DbConnection dbConnection = (DbConnection) obj;

            if (this.ApplId.equals(dbConnection.getApplId()) && 
                    this.DbName.equals(dbConnection.getDbName()) && 
                    this.SchemaName.equals(dbConnection.getSchemaName())) {
                return true;
            }
        }

        return false;

    }

}

Main Mathod: First insert all element to array list and Make another arraylist where you store all of your unique element. 主要方法:首先将所有元素插入数组列表,然后创建另一个arraylist,在其中存储所有唯一元素。 Then iterate your first list and check first list's already exist or not in unique list. 然后迭代您的第一个列表,并检查第一个列表是否已经存在或是否在唯一列表中。 If not exist then add this item to unique list. 如果不存在,则将此项目添加到唯一列表。

public static void main(String[] args) throws IOException {

    ArrayList<DbConnection> arrayList = new ArrayList<>();
    arrayList.add(new DbConnection("EGPRD", "SYSTEM", "AAA", "dh7y7hdu", "oracle"));
    arrayList.add(new DbConnection("EGPRD", "SYSTEM", "AAA", "d6f7d6fd", "linux"));
    arrayList.add(new DbConnection("EGPRD", "ADM", "RDA", "dh7y7hdu", "linux"));
    arrayList.add(new DbConnection("SOPRD", "DBLINK", "ACT", "fdf7f87e8", "oracle"));

    ArrayList<DbConnection> newList = new ArrayList<>();
    for (DbConnection dbConnection : arrayList) {

        if (newList.isEmpty()) {
            newList.add(dbConnection);
        } else {

            boolean existenceStatus = false;

            for (DbConnection newSingleElement : newList) {
                if (newSingleElement.equals(dbConnection)) {
                    existenceStatus = true;
                    break;
                }
            }

            if(!existenceStatus) {
                newList.add(dbConnection);
            }
        }

    }

    for (DbConnection dbConnection : newList) {
        System.out.println(dbConnection.getDbName() + " : "
                + dbConnection.getSchemaName() + " : "
                + dbConnection.getApplId() + " : ");
    }
}

I think you will like this... 我想你会喜欢...

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Exam {
    public static void main(String[] args) {
        DbConnection dc1 = new DbConnection("EGPRD","SYSTEM","AAA","dh7y7hdu", "oracle");
        DbConnection dc2 = new DbConnection("EGPRD","SYSTEM","AAA","d6f7d6fd", "linux");
        DbConnection dc3 = new DbConnection("EGPRD","ADM","RDA","d6f7d6fd", "linux");
        DbConnection dc4 = new DbConnection("SOPRD","DBLINK","ACT","fdf7f87e8", "oracle");

        List<DbConnection> dcList = new ArrayList(4);
        dcList.add(dc1);
        dcList.add(dc2);
        dcList.add(dc3);
        dcList.add(dc4);

        Map uniqueTable = new Hashtable();
        for( DbConnection fDc : dcList ){
            uniqueTable.put(fDc.key, fDc);
        }

        for( Iterator it = uniqueTable.keySet().iterator();it.hasNext(); ){
            DbConnection mDc = (DbConnection)uniqueTable.get(it.next());
            System.out.println( "1234" + "\t"+ mDc.DbName + "\t" + mDc.SchemaName + "\t" + mDc.AppId);
        }
    }
}

class DbConnection{

    public DbConnection(String dbName, String schemaName, String appId, String host, String osUser) {
        super();
        DbName = dbName;
        SchemaName = schemaName;
        AppId = appId;
        Host = host;
        OsUser = osUser;
        key = DbName + SchemaName + AppId; // key generation 
    }
    public String key;
    public String DbName;
    public String SchemaName;
    public String AppId;
    public String Host;
    public String OsUser;
}

If you can not modify a Db Connection guess you could use the inherited or wrappers. 如果您无法修改Db Connection,则可以使用继承的或包装器。

class DbConnectionEx extends DbConnection{
    public String key;
    public DbConnectionEx(String dbName, String schemaName, String appId, String host, String osUser) {
        super(dbName, schemaName, appId, host, osUser);
        key = DbName + SchemaName + AppId; // key generation 
    }   
}

You can use TreeSet to remove doublets, pseudocode: 您可以使用TreeSet删除双峰,伪代码:

TreeSet<DbConnection> set= new TreeSet<>((l, m) -> (l.getSchema() +l.getDababase()...).compareTo(m.getSchema() + l.getDatabase()+ ...));
for (DbConnection item : list) {
    if (/*condition*/) {
        set.add(item);
    }
}
for (DbConnection item : set) {
      //add to db
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM