简体   繁体   English

迭代集合类的最佳方法?

[英]Best way to Iterate collection classes?

Guys i wanna ask about the best way to iterate collection classes ?? 伙计们,我想问一问迭代收集类的最佳方法?

private ArrayList<String> no = new ArrayList<String>();
private ArrayList<String> code = new ArrayList<String>();
private ArrayList<String> name = new ArrayList<String>();
private ArrayList<String> colour = new ArrayList<String>();
private ArrayList<String> size = new ArrayList<String>();



 // method for finding specific value inside ArrayList, if match then delete that element
 void deleteSomeRows(Collection<String> column, String valueToDelete) {

        Iterator <String> iterator = column.iterator();

        do{ 
            if (iterator.next()==valueToDelete){
                           iterator.remove();     
                           }

                      }while(iterator.hasNext());   

}

deleteSomeRows(no, "value" );
deleteSomeRows(code, "value" );
deleteSomeRows(name , "value");
deleteSomeRows(colour ,"value" );
deleteSomeRows(size , "value");

THE PROBLEM WITH CODES ABOVE IS THAT IT TAKES AMOUNT OF TIME JUST TO ITERATE EACH OF THOSE CLASSES ? 上面编码的问题是,要重复上述每个类需要花费大量时间? ANY SOLUTION TO MAKE IT FASTER ? 有什么解决方案可以使它更快? pls help if u care :D.. 请帮助,如果你在乎:D ..

You could simplify your code: 您可以简化代码:

while column.contains(valueToDelete)
{
    column.remove(valueToDelete);
}

You're not going to be able to speed up your ArrayList iteration, especially if your list is not sorted. 您将无法加快ArrayList的迭代速度,尤其是在列表未排序的情况下。 You're stuck at O(n) for this problem. 您被困在O(n)这个问题上。 If you sorted it and inserted logic to binary search for the item to remove until it is no longer found, you could speed up access. 如果对它排序并插入逻辑以二进制搜索要删除的项目,直到不再找到它,则可以加快访问速度。

This next suggestion isn't directly related to the time it takes, but it will cause you problems. 下一条建议与所需时间没有直接关系,但是会给您带来麻烦。

You should never compare String objects for equality using the == operator. 永远不要使用==运算符比较String对象是否相等。 This will cause a comparison of their pointer values. 这将导致它们的指针值的比较。

Use this instead: 使用此代替:

if (iterator.next().equals(valueToDelete))

If you want to modify the collection while iterating them then you should use Iterators, otherwise you can use the for-each loop. 如果要在迭代集合时修改集合,则应使用迭代器,否则可以使用for-each循环。

For -each : 对于-each:

// T is the type f elements stored in myList
for(T val : myList)
{
   // do something
}

EDIT: The problem here is not the iteration. 编辑:这里的问题不是迭代。 The problem is removing the elements from the ArrayList . 问题是从ArrayList 删除元素。 When you remove the first element from an ArrayList , then all subsequent elements have to be shifted one position to the left. 当您从ArrayList删除第一个元素时,所有后续元素都必须向左移动一个位置。 So in the worst case, your current approach will have quadratic complexity. 因此,在最坏的情况下,您当前的方法将具有二次复杂性。

It's difficult to avoid this in general. 通常很难避免这种情况。 But in this case, the best tradeoff between simplicity and performance can probably be achieved like this: Instead of removing the elements from the original list, you create a new list which only contains the elements that are not equal to the "valueToDelete". 但是在这种情况下,可以像这样实现简单性和性能之间的最佳权衡:您无需从原始列表中删除元素,而是创建一个仅包含等于“ valueToDelete”元素的列表。

This could, for example, look like this: 例如,它可能如下所示:

import java.util.ArrayList;
import java.util.List;

public class QuickListRemove
{
    public static void main(String[] args)
    {
         List<String> size = new ArrayList<String>();
         size = deleteAll(size, "value");
    }

     private static <T> List<T> deleteAll(List<T> list, T valueToDelete) 
     {
         List<T> result = new ArrayList<T>(list.size());
         for (T value : list)
         {
             if (!value.equals(valueToDelete))
             {
                 result.add(value);
             }
         }
         return result;
    }
}

找到要删除的元素后,尝试休息一下。

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

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