Solved

Is this String Unique?

Posted on 2004-04-20
21
189 Views
Last Modified: 2010-03-31
Hi all,

I have a textfile containing lines of text. Each line is entered into this file when a selection has been made on a menu on a HTML page.

I have written the code to insert the text into the textfile, however I wish that each entry in the file should be unique.

ie textfile contains

[code]

<option>option1</option>
<option>option2<option>
<option>option3<option>
<option>option1<option>
<option>option1<option>

[/code]

I want to check the text to be inserted into the textfile in my JAva Program before it is entered so that it has not already appreared before.


Any thoughts

-He
0
Comment
Question by:highlyevolved
  • 8
  • 6
  • 2
  • +1
21 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 63 total points
Comment Utility
Insert the lines into a HashSet first then save the contents of that Set to a file - it'll remove duplicates
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 62 total points
Comment Utility
CEHJ's comment in code:

HashSet theSet = new HashSet();

theSet.add("<option>option1</option>");
theSet.add("<option>option2</option>");
theSet.add("<option>option3</option>");
theSet.add("<option>option1</option>");  // due to the nature of a Set, this will overwrite the existing entry
theSet.add("<option>option1</option>");  // idem

Iterator it = theSet.iterator();
while (it.hasNext())
   System.out.println((String)it.next());  // This will only print all different entries

But you asked:
>> I want to check the text to be inserted into the textfile in my JAva Program before it is entered
Then I think you should start with an empty Set:

HashSet theSet = new HashSet();

and before you write a line to the file you should check if it is already in:

if (theSet.contains(theLineToWrite)) {
   // Don't write it
}
else {
   // 1) Write the line to file
   // 2) Add it to the Set, so that it won't be written a second time
   theSet.add(theLineToWrite);
}  
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>and before you write a line to the file you should check if it is already in:

Why? That's the whole reason for using a Set, otherwise you may as well use a List
0
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
Hi,
below is the example i have written, which removes the duplicates....you can call this method in your application...

// *********************************************************************************/
// *********************************************************************************/

      // Method to Remove the Duplicate EF#'s from the Vector
      public Vector removeDuplicate(Vector vDup)
      {
            AnnualTesting1_1 theAT      = new AnnualTesting1_1() ;
      
            removeDup = new Vector();
            String empno1       = "";
            String empno2      = "";
            boolean userflg = false;
      
                  if(!theAT.vCEmpList.isEmpty())
                  {
                        for(int v=0;v < theAT.vCEmpList.size();v++)
                        {
                              empno2 = (String)theAT.vCEmpList.elementAt(v);
                              add(empno2);
                        }
                  }
            return removeDup;      
      }

// *********************************************************************************/
// *********************************************************************************/

      // Method calling in add()
      // to check for the Duplicate
      // Values Inserted or Not
      // it won't allow any duplication.
      public boolean contains(Object obj)
      {
            Iterator it = removeDup.iterator();

            Object member;
            while( it.hasNext() )
            {
                  member = it.next();
                  if ( member.equals( obj ) )
                       return true;
          }

          return false;
      }
      
      // Method calling for Iteration
      public Iterator iterator()
      {
            return removeDup.iterator();
      }
      
      // Method calling in removeDuplicate()
      // For Adding Values
      public boolean add( Object obj )
      {
            if (!contains(obj))
            {
                  removeDup.add(obj);
                  return true;
            }

            return false;
      }
      
// *********************************************************************************/
// *********************************************************************************/

best of luck.,,,
R.K.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> Why? That's the whole reason for using a Set, otherwise you may as well use a List
No since a list doesn't eleminate duplicates by nature.
0
 

Author Comment

by:highlyevolved
Comment Utility
zzynx,

Thanks for the reply.

Which method would be most efficient taking that there may be thousands of entries in the set?
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> Which method
What do you mean exactly? Between what methods do I have to choose?

I'd go for:

HashSet theSet = new HashSet();

and before you write a line to the file you should check if it is already in:

if (theSet.contains(theLineToWrite)) {
   // Don't write it
}
else {
   // 1) Write the line to file
   // 2) Add it to the Set, so that it won't be written a second time
   theSet.add(theLineToWrite);
}
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> there may be thousands of entries in the set
Then have a look at the explanation about initial capacity & loadFactor that influnence the performance.
(http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html [it's about HashMap but it's also for HashSet])

Use
            new HashSet(int initialCapacity);
or even
            new HashSet(int initialCapacity, float loadFactor);
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>>>
>> Why? That's the whole reason for using a Set, otherwise you may as well use a List
No since a list doesn't eleminate duplicates by nature.

>>>>

You're missing the point i think:

>>and before you write a line to the file you should check if it is already in:

The idea of using a Set is to put the lines in there first *before* writing them to file. There's no need to check.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> You're missing the point i think
I think you're missing mine too :)
I read:
>>Each line is entered into this file when a selection has been made on a menu on a HTML
>>I want to check the text to be inserted into the textfile in my Java Program *before* it is entered

*You* let the user make selections. Remember all in the set and then (but when is that?) you write all in the file.
I mean: how do you know that the user quits making selections?

*I* think the author whishes that with every selection made there is somethig written in that file...
...unless it is already in.
That's how the code in *my* comment works.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Let the author decide...
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>(but when is that?)

Presumably on end program (unless there's a irretrievable path away from the menu gui), when the user will no longer be able to make selections, or there wouldn't be much point in doing it. So the Set contents would be written to file then
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
As I said...
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
...so there'd be no reason to check for duplicates
0
 

Author Comment

by:highlyevolved
Comment Utility
Hey guys,

 after a bit of tweeking that seems to have worked fine,




>>HashSet theSet = new HashSet();

theSet.add("<option>option1</option>");
theSet.add("<option>option2</option>");
theSet.add("<option>option3</option>");
theSet.add("<option>option1</option>");  // due to the nature of a Set, this will overwrite the existing entry
theSet.add("<option>option1</option>");  // idem

Iterator it = theSet.iterator();
while (it.hasNext())
   System.out.println((String)it.next());  // This will only print all different entries




The only other issue I'm having is that I want to limit the HashSet to the last 20 entries to the textFile, the problem with this is that HashSet seems to store its objects in a completely random order and therefore I have no guarentee that the oldest of the entries will be deleted first.

Infact it seems just as likely that the latest entry will be deleted as the first entery.

Any Ideas?

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>...seems to store its objects in a completely random order and therefore ...

Use a LinkedHashSet
0
 
LVL 23

Expert Comment

by:rama_krishna580
Comment Utility
Can try this here....you will get more detailed information......

http://www-106.ibm.com/developerworks/java/library/j-mer0821/



0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> after a bit of tweeking that seems to have worked fine
Code of mine based on idea of CEHJ.
Hence my recommendation: a split between CEHJ and zzynx
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 44
topping3 challenge 14 48
base64 decode encode 12 94
what is a "java.lang.System Property"   ? 20 27
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now