• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Is this String Unique?

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
highlyevolved
Asked:
highlyevolved
  • 8
  • 6
  • 2
  • +1
2 Solutions
 
CEHJCommented:
Insert the lines into a HashSet first then save the contents of that Set to a file - it'll remove duplicates
0
 
zzynxSoftware engineerCommented:
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
 
CEHJCommented:
>>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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
rama_krishna580Commented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
highlyevolvedAuthor Commented:
zzynx,

Thanks for the reply.

Which method would be most efficient taking that there may be thousands of entries in the set?
0
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
>> 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
 
CEHJCommented:
>>>>
>> 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
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
Let the author decide...
0
 
CEHJCommented:
>>(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
 
zzynxSoftware engineerCommented:
As I said...
0
 
CEHJCommented:
...so there'd be no reason to check for duplicates
0
 
highlyevolvedAuthor Commented:
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
 
CEHJCommented:
>>...seems to store its objects in a completely random order and therefore ...

Use a LinkedHashSet
0
 
rama_krishna580Commented:
Can try this here....you will get more detailed information......

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



0
 
zzynxSoftware engineerCommented:
>> 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 8
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now