Solved

Is this String Unique?

Posted on 2004-04-20
21
197 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
ID: 10871070
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
ID: 10872217
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
ID: 10872290
>>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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10874326
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
ID: 10875660
>> 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
ID: 10876219
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
ID: 10876243
>> 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
ID: 10876264
>> 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
ID: 10876495
>>>>
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 10876544
>> 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
ID: 10876549
Let the author decide...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10876565
>>(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
ID: 10876604
As I said...
0
 
LVL 86

Expert Comment

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

Author Comment

by:highlyevolved
ID: 10880729
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
ID: 10880806
>>...seems to store its objects in a completely random order and therefore ...

Use a LinkedHashSet
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10885665
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
ID: 11676671
>> 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.

791 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