Tip

Java version of @Unique

View member feedback to this tip.

This is a static method of the Lotus formula @Unique, where you pass it a vector and it returns you a string without duplicate values.

  Code: Usage:
String returnList = Unique(vector value);

static String Unique(Vector vValue) {
    try
       {
         Vector hList = vValue;
         String gListH = hList.toString();
         String gListH1 = gListH.replace('[', ' ');
         String gList = gListH1.replace(']', ' ');
         String gListClean = gList.trim();
               
         Vector v1 = new Vector();
         Vector v2 = new Vector();
         Vector temp=new Vector();
         String test = gListClean;
         StringTokenizer st = new StringTokenizer(test,",");
         while(st.hasMoreTokens())
                   {
                     String tmp = st.nextToken();
                      v1.add(tmp);
                      v2.add(tmp);
                    }
         String item2="";
        
         for(int k=0; k < v1.size(); k++)
             {
               String item=(String)v1.get(k);
                if(!temp.contains(item))
                   temp.addElement(item);
             }
          v2 = temp;
               
         Vector hReturnList = v2;
         String gReturnListH = hReturnList.toString();
         String gReturnListH1 = gReturnListH.replace('[', ' ');
         String gReturnListH2 = gReturnListH1.replace(']', ' ');
         String gReturnListClean = gReturnListH2.trim();
         
         return gReturnListClean;
       }
       catch(Exception e)
                {                  
                  return "";  
               }
}               

MEMBER FEEDBACK TO THIS TIP

I have a feeling that @Unique can be done with some help from Java by using Set.

A Set is a collection that cannot contain duplicate elements. See: http://java.sun.com/docs/books/tutorial/collections/index.html

The code fragment to get unique entries is only one line of code:
Set set = new TreeSet(vector);

I include the whole program to show the Set usage:
(the Unique method is included as well)

import java.util.Arrays;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.Vector;

public class Main {

 static String Unique(Vector vValue) {
  try {
   Vector hList = vValue;
   String gListH = hList.toString();
   String gListH1 = gListH.replace('[', ' ');
   String gList = gListH1.replace(']', ' ');
   String gListClean = gList.trim();

   Vector v1 = new Vector();
   Vector v2 = new Vector();
   Vector temp = new Vector();
   String test = gListClean;
   StringTokenizer st = new StringTokenizer(test, ",");
   while (st.hasMoreTokens()) {
    String tmp = st.nextToken();
    v1.add(tmp);
    v2.add(tmp);
   }
   String item2 = "";

   for (int k = 0; k < v1.size(); k++) {
    String item = (String) v1.get(k);
    if (!temp.contains(item))
     temp.addElement(item);
   }
   v2 = temp;

   Vector hReturnList = v2;
   String gReturnListH = hReturnList.toString();
   String gReturnListH1 = gReturnListH.replace('[', ' ');
   String gReturnListH2 =
gReturnListH1.replace(']', ' ');
   String gReturnListClean = gReturnListH2.trim();

   return gReturnListClean;
  } catch (Exception e) {
   return "";
  }
 }

 
 /**
  * Return some data for the test.
  * @return
  */
 private static Vector getNewVector() {
  
  Integer inputArray[] = new Integer[5];

  for (int index = 0; index < inputArray.length; index++) {
   inputArray[index] = new Integer(index + 1);
  }
  
  Vector vector = new Vector();

  vector.addAll(Arrays.asList(inputArray));
  vector.addAll(Arrays.asList(inputArray));
  vector.addAll(Arrays.asList(inputArray));

  return vector;

 }

 public static void main(String[] args) {

  Vector vector = new Vector(getNewVector());

  System.out.println("The original vector: " + vector);

  System.out.println("The result of Unique Method: \n"
    + "(I think see a bug there.. the '1'
appears twice \n"
    + Unique(vector));

  // Using the set interface
  Set set = new TreeSet(vector);
  System.out.println("set content: " + set);
  vector.removeAllElements();
  vector.addAll(set);
  System.out.println("vector after removeAllElements()"
    + "and addAll(set) : " + vector);
 }
}


This is the result:

The original vector: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5] The result 
of Unique Method: 
(I think see a bug there.. the '1' appears twice 1,  2,  3,  4,  5,  1 
setcontent: [1, 2, 3, 4, 5] vector after removeAllElements()and addAll(set) : 
[1, 2, 3, 4, 5]

—Eitan R.

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

This tip was submitted to the SearchDomino.com tip exchange by member Mike Marcavage. Please let others know how useful it is via the rating scale at the end of the tip. 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 January 2005

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

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.