Solved

Is this String Unique?

Posted on 2004-04-20
21
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 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