简体   繁体   中英

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..

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. You're stuck at O(n) for this problem. 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. 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 :

// 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 . When you remove the first element from an ArrayList , then all subsequent elements have to be shifted one position to the left. 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".

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;
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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