Java 在数组列表上迭代的更有效方法
我的程序接收数千个PL/SQL函数、过程和视图,将它们保存为对象,然后将它们添加到数组列表中。“我的数组列表”以以下格式存储对象:Java 在数组列表上迭代的更有效方法,java,performance,arraylist,Java,Performance,Arraylist,我的程序接收数千个PL/SQL函数、过程和视图,将它们保存为对象,然后将它们添加到数组列表中。“我的数组列表”以以下格式存储对象: ArrayList<PLSQLItemStore> storedList = new ArrayList<>(); storedList.add(new PLSQLItemStore(String, String, String, Long )); storedList.add(new PLSQLItemStore
ArrayList<PLSQLItemStore> storedList = new ArrayList<>();
storedList.add(new PLSQLItemStore(String, String, String, Long ));
storedList.add(new PLSQLItemStore(Name, Type, FileName, DatelastModified));
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");
}
}
}
}
您需要使
PLSQLItemStore
实现hashCode
和equals
方法,然后可以使用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 withoutDups=newhashset(storedList)代码>
p.S.equals
和hashCode
是由NetBeans IDE生成的。将它们放在番石榴ArrayListMultimap
中
使用name
作为键添加每个PLSQLItemStore
添加完成后,在多重映射中循环,使用比较器对每个列表进行排序,比较器按dateLastModified
进行排序,然后拉出每个列表的最后一个条目,它将是最新的PLSQLItemStore
将这些条目放在另一个映射
(或列表
,如果您不再关心名称)中,并抛弃基于Petr Mensik答案的ArrayListMultimap
构建,您应该实现equals
和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);
}
使用映射
,其中键和值是您的对象。为什么不使用映射?当您知道要多次访问同一元素时,将其存储在局部变量中。您是否测试过这实际需要多长时间?是的,我有,取决于对象的数量,但对于2000个对象,大约需要30秒。没那么糟糕,但我会对此进行更多的探讨使用集合的缺点是,除非您再次遍历所有集合
,否则无法检索对象。对于映射
中的值,您不需要实现hashCode
和equals
,只需实现键。
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;
}
}
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);
}
for(PLSQLItemStore currentStore : storeMap) {
// Do whatever you want with your items
}