Alternative nested loop for comparison

I am currently writing a program that should compare each file in an ArrayList with a variable size. Right now, I am doing this through a nested code loop:

         if(tempList.size()>1){
            for(int i=0;i<=tempList.size()-1;i++)
                //Nested loops.  I should feel dirty?
                for(int j=i+1;j<=tempList.size()-1;j++){
                    //*Gets sorted.
                    System.out.println(checkBytes(tempList.get(i), tempList.get(j)));
                }
            }

I read several different opinions about the need for nested loops, and I was wondering if anyone has a more efficient alternative.

At first glance, each comparison will need to be done in any case, so that the performance should be fairly stable, but I am confident that there is a cleaner way to do this. Any pointers?

EDIT :: This is only part of the function, for clarity. Files were compared and placed in buckets in length - after going through a dialing map and finding a bucket that is more than one, it launches it. So - all files are the same size. I will compare the checksum until I get the bytes, but now I'm just trying to clear the loop.

In addition, a holy cow, this site responds quickly. Thanks guys.

EDIT2:: , : , - -, , , - , , ArrayList, , . , , , , .

+5
5

EDIT2

, , . O(N**2) O(N). , N , , . , , N .

, O(N) . , .

  • FileHash - . equals(Object) hashCode(), .

  • HashMap<FileHash, List<File>>.

  • File ArrayList:

    • FileHash.
    • FileHash :
    • , , . , BINGO! .
    • , "FileHash" , - .

( , - , , Apache Google. . )

:

  • - , 3.3, , , , - , , . , , .

  • , , , . , HashMap<FileHash, List<FileTuple>>, FileTuple - , a File, .

  • , () . , , ; . , . (, , 256 , ... - !)

+3

, .

, , O (1) , , .

:

HashSet<YourFile> fileSet = new HashSet<YourFile>();
ArrayList<YourFile> files = new ArrayList<YourFile>();

class YourFile
{
  int hashcode = -1;

  public int hashCode()
  {
     // override it to provide an hashcode based on file contents
     // you can also cache it to avoid recalculating anything

     if (hashcode == -1)
       hashcode = calculateIt();

     return hashcode;
  }
}

// fill up files
files.add(...);

// do comparisons
for (YourFile f : files)
{
  if (fileSet.contains(f))
    // f and fileSet.get(f) are equal: this is a tricky utilization of the hashCode() method so be careful about it!
  else
  {
    fileSet.put(f);
    // since there not a file with same hashcode you just add this one
  }
}

, hashSet.contains , O (1).

doublep, , , , , , . , . , .

+3

, , , . , ( ), .

EDIT:

. -, , : , , , .

-, , ( ), , . , (.. ), . , "" (, 10 ), .

+2

, , O (nΒ²). , . - ; , - , , , ( , ). , , ; Set - , , .

+1

One small cleanup will be to remove the initial dimensional test - if the size is less than 2, it will simply drop out without making any comparisons. Better adherence to Java coding conventions would be a loop i < tempList.size()instead instead i <= tempList.size() - 1- it would simply simplify your code for other programmers. None of these changes affect performance.

for (int i = 0; i < tempList.size(); i++)
    for (int j = i + 1; j < tempList.size(); j++) {
        //*Gets sorted.
        System.out.println(checkBytes(tempList.get(i), tempList.get(j)));
    }
+1
source

All Articles