0

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

  • Are you asking what the fastest way to find the `valueToDelete` is? Because you are already iterating through the collection. – forgivenson Feb 28 '14 at 15:29
  • yes, i don't know about the alternative ways, the only way in my mind is that i have to iterates each of those classes, but the codes run slow... –  Feb 28 '14 at 15:31
  • Using a list you can't go no faster than that. If efficiency is what you are looking for then you are going to have to use a different data structure, probably a hash-based collection, or by saving some extra information than actually required. – Bhesh Gurung Feb 28 '14 at 15:33
  • @febri23 Perhaps if you gave us more info on the type of data you are storing, and what you are trying to accomplish, someone could suggest a more efficient data structure. – forgivenson Feb 28 '14 at 15:35
  • @forgivenson is right, in order for us to be able to help you, you should help us first by putting up a clear explanation of the your situation/requirement. – Bhesh Gurung Feb 28 '14 at 15:38
  • the problem is simple, if u created a lot ArrayList classes , then u're forced to iterate each of those classes (example you iterate those to find specific value) , then what is the best way (faster way) to do that , of course it's fast if just iterates a class, but how about , let's say there are 10 ArrayList classes ?? –  Feb 28 '14 at 15:43
  • @ Bhesh Gurung , that's the answer !!! , and thank u guys for taking time answering my question .... –  Feb 28 '14 at 16:11

4 Answers4

1

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))
Brian English
  • 466
  • 2
  • 8
  • Agree, yes it's true. For a long time i thought that '.contains(String ..)' will result same result with '=='. thank you for taking time answer my question.. –  Feb 28 '14 at 16:07
  • You're welcome. The .contains() method will use the class's .equals() method to do the comparison, not ==. – Brian English Feb 28 '14 at 16:14
0

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
}
Kakarot
  • 4,252
  • 2
  • 16
  • 18
0

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;
    }
}
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • @febri23 Added an explaination and a suggestion about how this could be solved. – Marco13 Feb 28 '14 at 15:42
  • @febri23 No, it was probably for me, but I edited the answer (sorry), but the new answer subsumes what I previously wrote – Marco13 Feb 28 '14 at 15:50
  • WOW IT WORKS, THE POINT HERE IS 'List' is faster than just 'ArrayList', so i modify my classes to List column = new ArrayList(); THANK U VERY MUCH Marco.. –  Feb 28 '14 at 16:00
  • @febri23 **NO** This is in no way related to the fact that I used `List` instead of `ArrayList`. The data structure that is used there is **still** an `ArrayList`. The only difference is that this method does no longer call `remove` on the list for arbitrary elements! – Marco13 Feb 28 '14 at 16:04
  • List ist not faster then ArrayList. List l = new ArrayList() is just an initialization with the specific type ArrayList which implements the List interface. This has nothing todo with fastness – Jan Koester Feb 28 '14 at 16:13
  • it's just strange, my codes work faster after i used 'List' , i think that 'List .. = new ArrayList... ()' will work different with 'ArrayList .. = new ArrayList... ()'. –  Feb 28 '14 at 16:16
  • @febri23 Again: This has **NOTHING** to do with using `List` or `ArrayList`. You should always use `List` when possible, but for different reasons ( http://stackoverflow.com/questions/383947 ). The performance improvement **ONLY** comes from the fact that you do no longer use `remove`. (You can verify this: Change `ArrayList` to `List` in your original program, and it will **NOT** be faster!) – Marco13 Feb 28 '14 at 16:19
  • this comes from the fact that a new list is created and so the existing arraylists must not re-arranged (like Marco13 described). The usage of the List interface has nothing todo with the problem. – Jan Koester Feb 28 '14 at 16:19
  • I never iterate 20 ArrayList classes before , so i got confused, WTH it's so slow . Anyway thanks again guys. !! –  Feb 28 '14 at 16:30
0

Try putting a break after you find the element to delete.

sergiu
  • 389
  • 1
  • 7
  • NO you should not...what if there are multiple elements in the list with the same value – Kakarot Feb 28 '14 at 15:34
  • @sergiu, i already thinked that way , but how about the value is being on 'before -last' element ???, so it doesn't make any difference.. –  Feb 28 '14 at 15:38
  • A LinkedList is faster than ArrayList at removing an element. – sergiu Feb 28 '14 at 15:47
  • yes it's true. i never think before ArrayList is that slow.., thank u guys for taking time to answer.. –  Feb 28 '14 at 16:10