5

I have an ArrayList which stores Area names. I want to check this list to find whether arbitrary people are from different area. If they are from different area, i take a decision. I achieved this with the following code. Note that area_IdList and area_IdListduplicate are essentially the same ArrayList. Is this code efficient or can anyone suggest more efficient code ? Thanks in Advance.

public List<String> area_IdList = new ArrayList<String>();
public List<String> area_IdListduplicate = new ArrayList<String>();

for (int i = 0; i < area_IdList.size(); i++) 
{  
    for (int k = 1; k< area_IdListduplicate.size(); k++)
    {
        String sa= area_IdListduplicate.get(k);
        String sb= area_IdList.get(i);
        if (!sa.equalsIgnoreCase(sb))
        { 
            some decision
        }
    }             
}
kzs
  • 1,111
  • 5
  • 20
  • 35
  • Can you explain better what you want to achieve? Right now you are taking a decision if they are NOT from the same area! – Carnal Oct 18 '12 at 06:24
  • for (int k = i+1;......will more efficient – zetsin Oct 18 '12 at 06:27
  • @carnal i edited my question..sorry for the mistake. i want to find whether they are from different area – kzs Oct 18 '12 at 06:29
  • 1
    @zetsin: k = i +1 will skip the first index (0) and give him OutOfBoundException when attempting to get the last element if they have the same size – Carnal Oct 18 '12 at 06:29
  • @Carnal: Note that area_IdList and area_IdListduplicate are essentially the same ArrayList. as his meaning, the first index(0) is not necessary to compare. And if they have the same size, OutOfBoundException will not be happended – zetsin Oct 18 '12 at 06:36
  • @zetsin: read my comment again. – Carnal Oct 18 '12 at 06:36
  • @Carnal: As his meaning, the first index(0) is not necessary to be compared. And if they have the same size, it will not step into the second for loop, so OutOfBoundException will not be happended – zetsin Oct 18 '12 at 06:40
  • @zetsin: you're right mate about the not getting "OutOfBoundException", I thought about list.get(i + 1), misread it. – Carnal Oct 18 '12 at 06:45

3 Answers3

8
for (String area : area_IdList) 
{  
    for (String duplicatedArea : area_IdListduplicate)
    {
        if (!area.equalsIgnoreCase(duplicatedArea))
        { 
            // some decision
        }
    }             
}

This is more efficient, goes faster instead of iterating by indexes. So this will check step by step each element in area_IdList with all elements in area_idListduplicate and each time they don't mach, this decision will be made. (If that's what you want achieve)

Carnal
  • 21,744
  • 6
  • 60
  • 75
  • `goes faster instead of iterating by indexes`. What do you mean? – iTurki Oct 18 '12 at 06:45
  • When you iterate by indexes = it starts over from index 0 again. When you iterate with what I wrote = it goes straight through with out starting over. This is the way it works, nothing you will see in code though. – Carnal Oct 18 '12 at 06:47
  • @Carnal don't understand what you mean by that. – TJ Thind Oct 18 '12 at 07:38
  • @TJ Thind: http://stackoverflow.com/questions/10486507/why-would-iterating-over-a-list-be-faster-than-indexing-through-it/10486601#10486601 – Carnal Oct 18 '12 at 07:49
  • @Carnal Are you referring to linked list traversal? That wouldn't be an issue here since the collection being used is an ArrayList which is backed by an array. – TJ Thind Oct 18 '12 at 08:01
  • @TJ Thind: you're right. I was wrong about it being faster. – Carnal Oct 18 '12 at 08:05
  • 1
    @Your solution isn't necessarily wrong. It might be faster if it were using a different collection or on a device without JIT. A better way would be to reduce the complexity of the algorithm. This can be done without nested loops. – TJ Thind Oct 18 '12 at 08:12
6

This is a O(2N) solution at some cost of 2N memory instead of N^2 time and N memory cost. Depends on the number of items that you have, but this solution will cost significantly less than the N^2 solutions.

  Set<String> list=new Set<String>();
  for (String area : area_IdList) 
  {  
    list.add(area.toLowerCase());
  }

  for (String duplicatedArea : area_IdListduplicate)
  {
    if(list.contains(duplicatedArea.toLowerCase())){
      //Do something
    }
  }

As for not using index, see Use Enhanced For Loop Syntax

Edison
  • 5,961
  • 4
  • 23
  • 39
0

You can try like this

 for(String areaId : area_IdList){
    if(!area_IdListduplicate.contains(areaId){
    //some decision
    }
    }
abbas.aniefa
  • 2,855
  • 21
  • 30