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.