Home > Domino Tips > Developer > Java > Alternate approach to simulating @Unique in Java
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

JAVA

Alternate approach to simulating @Unique in Java


Dallas Gimpel
02.15.2005
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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.

Rate this Tip
To rate tips, you must be a member of SearchDomino.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Java
Java code inserts data from Notes documents into a SQL table
Java code shortens strings in a SQL table
How to execute a stored procedure in Lotus Notes Domino using Java
How to return an HTML representation of a Lotus Notes rich-text field
Shrink Lotus Notes databases with many attachments
Converting Lotus Notes Domino Web pages to PDF files with a Java agent
A bevy of Notes/Domino development tips
Converting Web pages to images using Java
Creating Microsoft Word documents from Lotus Notes
FAQ: Java for Lotus Notes and Domino

Java for Lotus Notes Domino
Top 10 Lotus Notes/Domino coding and development tips of 2008
Java code inserts data from Notes documents into a SQL table
Java code shortens strings in a SQL table
How to execute a stored procedure in Lotus Notes Domino using Java
Top 10 Lotus Notes Domino programming and development tips of 2007
How to return an HTML representation of a Lotus Notes rich-text field
Shrink Lotus Notes databases with many attachments
Converting Lotus Notes Domino Web pages to PDF files with a Java agent
Developing Eclipse plug-ins for Lotus Notes and Domino -- 7 tips in 7 minutes
A bevy of Notes/Domino development tips

Lotus Notes Domino Formula Language
View hidden fields on Lotus Notes/Domino forms
Case-insensitive @Unique version combines fields on Lotus Notes forms
Do I use Formula or LotusScript to include a doclink to a Notes view?
Top 10 Lotus Notes/Domino coding and development tips of 2008
Provide rich-text formatting via the Profile document and Formula
Top 10 Formula language tips
Using Formula language code to sort Lotus Notes messages by subject
How to create dynamic JavaScript in Notes Domino without formulas
Stop response documents from showing in a Lotus Notes form
Formula language button manages Deny Access list searches

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts