0

我的程序接收数千个 PL/SQL 函数、过程和视图,将它们保存为对象,然后将它们添加到数组列表中。我的数组列表存储具有以下格式的对象:

ArrayList<PLSQLItemStore> storedList = new ArrayList<>(); 
storedList.add(new PLSQLItemStore(String, String, String,   Long            ));
storedList.add(new PLSQLItemStore(Name,   Type,   FileName, DatelastModified));

我想做的是根据它们的名称从数组列表中删除重复的对象。旧对象将根据其 dateLastModified 变量被删除。我采用的方法是有一个外循环和一个内循环,每个对象将自己与其他所有对象进行比较,然后如果它被认为是旧的,则将名称更改为“删除”。然后程序最后一次通过数组列表向后传递,删除名称设置为“remove”的任何对象。虽然这很好用,但似乎效率极低。1000 个对象意味着需要进行 1,000,000 次传球。我想知道是否有人可以帮助我提高效率?谢谢。

样本输入:

storedList.add(new PLSQLItemStore("a", "function", "players.sql", 1234));
storedList.add(new PLSQLItemStore("a", "function", "team.sql", 2345));
storedList.add(new PLSQLItemStore("b", "function", "toon.sql", 1111));
storedList.add(new PLSQLItemStore("c", "function", "toon.sql", 2222));
storedList.add(new PLSQLItemStore("c", "function", "toon.sql", 1243));
storedList.add(new PLSQLItemStore("d", "function", "toon.sql", 3333));

ArrayList 迭代器:

for(int i = 0; i < storedList.size();i++)
{
    for(int k = 0; k < storedList.size();k++)
    {
        if (storedList.get(i).getName().equalsIgnoreCase("remove"))
        {
            System.out.println("This was already removed");
            break;
        }

        if (storedList.get(i).getName().equalsIgnoreCase(storedList.get(k).getName()) &&  // checks to see if it is valid to be removed
           !storedList.get(k).getName().equalsIgnoreCase("remove") &&
           i != k )
        {
            if(storedList.get(i).getLastModified() >= storedList.get(k).getLastModified())
            {
                storedList.get(k).setName("remove");
                System.out.println("Set To Remove");
            }
            else
            {
                System.out.println("Not Older");
            }
        }
    } 
}

移除对象的最终通道:

System.out.println("size: " + storedList.size());
for (int i= storedList.size() - 1; i >= 0; i--)
{
    if (storedList.get(i).getName().equalsIgnoreCase("remove"))
    {
        System.out.println("removed: " + storedList.get(i).getName());

        storedList.remove(i);                
    }
}
System.out.println("size: " + storedList.size());
4

3 回答 3

0

把它们放在番石榴ArrayListMultimap<String,PLSQLItemStore>中。

添加每个PLSQLItemStoreusingname作为键。

添加完成后,循环遍历多图,List使用 a Comparator<PLSQLItemStore>which sort by 对每个元素进行排序dateLastModified,然后取出每个元素的最后一个条目List- 这将是最新的PLSQLItemStore.

将这些条目放在另一个中Map<String,PLSQLItemStore>(或者List<PLSQLItemStore>,如果您不再关心名称)并丢弃ArrayListMultimap.

于 2013-11-12T15:41:47.627 回答
0

根据 Petr Mensik 的回答,您应该实施equalsand hashCode. 从那里,您可以将项目放入地图中。如果遇到重复项,您可以决定接下来该怎么做:

Map<String, PLSQLItemStore> storeMap = new HashMap<String, PLSQLItemStore>();
for(PLSQLItemStore currentStore : storedList) {

    // See if an item exists in the map with this name
    PLSQLItemStore buffStore = storeMap.get(currentStore.getName();

    // If this value was never in the map, put it in the map and move on
    if(buffStore == null) {
        storeMap.put(currentStore.getName(), currentStore);
        continue;
    }

    // If we've gotten here, then something is in buffStore.
    // If buffStore is newer, put it in the map.  Otherwise, do nothing
    // (this might be backwards --  I didn't quite follow your logic.  
    // Feel free to correct me
    if(buffStore.getLastModified() > currentStore.getLastModified())
        storeMap.put(currentStore.getName(), currentStore);

}

您的地图是无重复的。因为Mapis a Collection,您可以稍后在代码中遍历它:

for(PLSQLItemStore currentStore : storeMap) {
    // Do whatever you want with your items
}
于 2013-11-12T15:45:12.020 回答
0

您需要制作PLSQLItemStore实现hashCodeequals方法,然后您可以使用它Set来删除重复项。

public class PLSQLItemStore {

    private String name;   

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PLSQLItemStore other = (PLSQLItemStore) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name))     {
            return false;
        }
        return true;
    }
}

然后就做Set<PLSQLItemStore> withoutDups = new HashSet<>(storedList);

PSequalshashCode由 NetBeans IDE 生成。

于 2013-11-12T15:03:57.203 回答