tags:

views:

108

answers:

4

Are there any De-Duplication API in Java? I want to eliminate redundant items (for e.g. duplicate fingerprints) from the database, how is it possible through Java programming?

A: 

Here's a simple example in pseudo-code. If you want a better answer, you're gonna have to give us a better question.

public void deDuplicate(Array items) {
  for(int i=0; i<items.length-1; i++) {
    for(int j=i+1; j<items.length; j++) {
      if(items[i].equals(items[j])) {
        items.removeItemAt(j);
        j--;
      }
    }
  }    
}
davr
== != .equals() - this probably isn't going to produce the results you want with anything but Strings and primitive types. For Object references this is going to test reference equality instead of value equality - use .equals() instead to test value equality.
Nate
This is just pseudo-code... ie I didn't bother putting in all the Java-specific syntax idiosyncrasies.
davr
+5  A: 

Without more information, I would recommend just using a Set. The various flavors of Set have different pros and cons but they all have one fundamental principle. Each one is:

A collection that contains no duplicate elements.

If you add your elements to a Set and then read them back out, each unique element will only appear once.

Bob Cross
A: 

The simple answer is that there is no Java API for removing duplicates from a database. There IS a Java API for querying and updating SQL database. It is called JDBC. But you are going to have to figure out how to detect duplicates (whatever that means) and remove them. The SQL to do this will be highly dependent on the schema of your database and the means by which you detect duplicates.

Stephen C
A: 

if you already know how to spot duplicates, you could use the "distinct" keyword in your sql queries.

Maxime ARNSTAMM