Alternate approach to simulating @Unique in Java

This tip is in response to Mike Marcavage's tip Java version of @Unique. It is an alternate approach to simulating @Unique in Java.

This Content Component encountered an error

This tip is in response to Mike Marcavage's tip Java version of @Unique. Below is one of many available alternate approaches to simulating @Unique in Java.

I suspect this approach will probably be noticeably faster than Mike's, as the size of the Vector was increased because there's no (explicit) searching for duplicates. Instead, it uses the HashSet class to eliminate duplicates on demand. The class was thrown together for demonstration purposes only and as such, is obviously not well-suited for implementation in a "production" environment, but you get the idea.

- CODE -

import java.util.Vector; 
import java.util.HashSet; 
import java.util.Collections; 
import java.util.Collection; 

public class UniqueVector extends Vector { 
        public UniqueVector() { 
                super(); 
        } 
        
        public UniqueVector(Collection pcol) { 
                super(pcol); 
        } 

        public UniqueVector(int piInitialCapacity) { 
                super(piInitialCapacity); 
        } 
        
        public UniqueVector
(int piInitialCapacity, int piCapacityIncrement) { 
                super(piInitialCapacity, 
piCapacityIncrement); 
        } 
        
        public static Vector
 getUnsortedUniqueVector(Vector pvecTarget) { 
HashSet ahsetUniqueVals = 
new HashSet(pvecTarget); 
                pvecTarget = 
new Vector(ahsetUniqueVals); 
                return pvecTarget; 
        } 

        public static Vector 
getSortedUniqueVector(Vector pvecTarget) { 
                HashSet 
ahsetUniqueVals = new HashSet(pvecTarget); 
                pvecTarget = 
new Vector(ahsetUniqueVals); 
                Collections.sort(pvecTarget); 
                return pvecTarget; 
        } 

        public Vector 
toSortedUniqueVector() { 
                HashSet ahsetUniqueVals 
= new HashSet(this); 
                Vector avecOutput
 = new Vector(ahsetUniqueVals); 
                Collections.sort(avecOutput); 
                return avecOutput; 
        } 

        public Vector toUnsortedUniqueVector() { 
                HashSet ahsetUniqueVals 
= new HashSet(this); 
                Vector avecOutput
 = new Vector(ahsetUniqueVals); 
                return avecOutput; 
        } 

        public String toSortedUniqueString() { 
                String astrOutput = 
this.toSortedUniqueVector().toString(); 
                
return astrOutput.substring(1, astrOutput.length() - 1); 
        } 
    
        public String toUnsortedUniqueString() { 
                String astrOutput = 
this.toUnsortedUniqueVector().toString(); 
                
return astrOutput.substring(1, astrOutput.length() - 1); 
        } 
    
        public static void main(String[] args) { 
                Vector avecNew; 
                Vector avecTmp; 
                UniqueVector avecTest; 
                
                long alStartTime = 
System.currentTimeMillis(); 
                avecTmp = new Vector(); 
                String[] astrTestVals = 
{"MNO", "VWX", "GHI", "ABC", 
"JKL", "YZ", "DEF", "ABC", "PQR", "STU", "VWX"}; 
                for (int i = 0; i 
< astrTestVals.length; i++) { 
                        avecTmp.add(astrTestVals[i]); 
                } 
                
                System.out.println
("IN THE BEGINNING: There are " + avecTmp.size
() + " non-unique unsorted 
values as follows:\n\t" + avecTmp); 
                                
                avecNew = 
UniqueVector.getUnsortedUniqueVector(avecTmp); 
               
System.out.println
("Operation #1: We now have " + avecNew.size
() + " unique, 
unsorted values as follows:\n\t" + avecNew); 
                
                avecNew = 
UniqueVector.getSortedUniqueVector(avecTmp); 
                System.out.println
("Operation #2: We now have " + avecNew.size
() + " unique, sorted values 
as follows:\n\t" + avecNew); 
                System.out.println("\n\n"); 
                
                
                avecTest = new UniqueVector(); 
                String astrNewTestVals[] = 
{"100", "105", "110", "115", "120",
 "125", "130", "135", "140", "150", "135", "1
60", "170", "180", "125", "190", "200", "105", "145", "155"}; 
                for (int i = 0; i < astrNewTestVals.length; i++) { 
                        avecTest.add(astrNewTestVals[i]); 
                } 

                System.out.println
("IN THE BEGINNING: There are " + 
avecTest.size() + " non-unique unsorted values as follows:\n\t" + 
avecTest); 

                avecNew = 
avecTest.toUnsortedUniqueVector(); 
                System.out.println
("Operation #1: We now have " + avecNew.size
() + " unique, unsorted values 
as follows:\n\t" + avecNew); 

                avecNew = 
avecTest.toSortedUniqueVector(); 
                System.out.println
("Operation #2: We now have " + avecNew.size
() + " unique, sorted values 
as follows: \n\t" + avecNew); 

                System.out.println
("Unique UNSORTED string: \n\t" + 
avecTest.toUnsortedUniqueString()); 
                System.out.println
("Unique SORTED string: \n\t" + 
avecTest.toSortedUniqueString()); 
                
                System.out.println("Operations completed in " + 
(System.currentTimeMillis() - alStartTime) + " milliseconds . . ."); 
        } 
} 

The output from the "main" method 
above should be something like: 
IN THE BEGINNING: There are 
11 non-unique unsorted values as follows: 
        [MNO, VWX, GHI, ABC, JKL, YZ,
 DEF, ABC, PQR, STU, VWX] 
Operation #1: We now have 9 unique, unsorted values as follows: 
        [STU, DEF, JKL, YZ, PQR, 
ABC, VWX, GHI, MNO] 
Operation #2: We now have 9 unique, sorted values as follows: 
        [ABC, DEF, GHI, JKL, MNO, PQR, STU, VWX, YZ] 


IN THE BEGINNING: There are 20 non-unique unsorted values as follows: [100, 105, 110, 115, 120, 125, 130, 135, 140, 150, 135, 160, 170, 180, 125, 190, 200, 105, 145, 155] Operation #1: We now have 17 unique, unsorted values as follows: [130, 145, 180, 105, 140, 200, 155, 100, 190, 115, 150, 110, 125, 160, 120, 135, 170] Operation #2: We now have 17 unique, sorted values as follows: [100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 170, 180, 190, 200] Unique UNSORTED string: 130, 145, 180, 105, 140, 200, 155, 100, 190, 115, 150, 110, 125, 160, 120, 135, 170 Unique SORTED string: 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 170, 180, 190, 200 Operations completed in 8 milliseconds . . .

Do you have comments on this tip? Let us know.

This tip was submitted to the SearchDomino.com tip exchange by member Dallas Gimpel. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

This was first published in February 2005

Dig deeper on Java for Lotus Notes Domino

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.UK

Close