aspose file tools*
The moose likes Java in General and the fly likes comparing elements in one hash set to the elements in another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparing elements in one hash set to the elements in another" Watch "comparing elements in one hash set to the elements in another" New topic
Author

comparing elements in one hash set to the elements in another

Fazz
Greenhorn

Joined: Mar 16, 2006
Posts: 9
Hi,

The following code was repeated three times to store 3 files into 3
individual hash sets. I now need to compare file A to file B, i want to
see how many of the words in set A are also on set B. (Each set
contains the same number of words). Im not sure how to implement this,
can anyone help??



public FileIn() {


try {
// make sure there is a text file in the java directory where the java
// code is saved
in = new BufferedReader(new FileReader("C:\\Program
Files\\Java\\example.txt"));
set = new HashSet();


int Len = 1;
while(Len>0) {
String line = in.readLine();
try {
Len = line.length();
System.out.println(line);
set.add(line);


} catch(NullPointerException npe){


Len = 0; //no more file to read


I think using the retainAll method might be an idea:
HashSet ab = (HashSet)a.clone();
ab.retainAll(b);


...but im not sure how to incorporate this into my program?


Farah
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Farah Omarshah: I think using the retainAll method might be an idea:
HashSet ab = (HashSet)a.clone();
ab.retainAll(b);


...but im not sure how to incorporate this into my program?



What is casusing you concerns about incorporating these two lines into your code?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Fazz
Greenhorn

Joined: Mar 16, 2006
Posts: 9
what i meant was, is this all the code i need? is anything missing??
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
Does the code do what you want it to do ? If so then there is nothing missing. If not then there is something missing. You will then need to tell us what it is doing and what you were expecting it to do.


Joanne
Fazz
Greenhorn

Joined: Mar 16, 2006
Posts: 9
well the code compiles but how can i check that it works??
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You need to create a few test cases where you know what the output should be and use them to test your code.
Fazz
Greenhorn

Joined: Mar 16, 2006
Posts: 9
would a simple if statement suffice for a test case?
A n if statement to check that oth sets contain the same elements followed by a 'system.out.println' to show the output??


Farah
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The best way to write test cases is so that they are

- repeatable and
- fully automated

That is, you should be able to start your tests with the press of a single button (for example), and get a result such as "all tests passed" or "2 failures". Only if you have failures, you should need to take a closer look at how the actual results differed from the expected.

The most popular library that helps in doing such tests - in fact I'd call it the de facto standard for java - is JUnit: http://www.junit.org/

If you'd like to discuss this in more detail, our Testing forum probably would be the best place.

Hope this helps...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Fazz
Greenhorn

Joined: Mar 16, 2006
Posts: 9
I have decided to use the retainAll method from the java.util library.
The following presents my code, it wont compile, im not sure what ive done wrong. can anyone help please??

import java.io.*;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.StringTokenizer;


public class File {
// assigns BufferedReader an instance name to be used in the code
BufferedReader in;
BufferedReader inOne;
BufferedReader inTwo;
HashSet a;
HashSet b;
HashSet c;


public File() {

try {
// make sure there is a text file in the java directory where the java
// code is saved
in = new BufferedReader(new FileReader("U:\\example.txt"));
inOne = new BufferedReader(new FileReader("U:\\exampleOne.txt"));
inTwo = new BufferedReader(new FileReader("U:\\exampleTwo.txt"));
a = new HashSet();
b = new HashSet();
c = new HashSet();


int Len = 1;
while(Len>0) {
String line = in.readLine();
try {
Len = line.length();
System.out.println(line);
a.add(line);
} catch(NullPointerException npe){
Len = 0; //no more file to read
}
}

int LenOne = 1;
while(LenOne>0) {
String line = inOne.readLine();
try {
LenOne = line.length();
System.out.println(line);
b.add(line);
} catch(NullPointerException npe){
LenOne = 0; //no more file to read
}
}

int LenTwo = 1;
while(LenTwo>0) {
String line = inTwo.readLine();
try {
LenTwo = line.length();
System.out.println(line);
c.add(line);
} catch(NullPointerException npe){
LenTwo = 0; //no more file to read
}
}
private static void findDupsByRetainAll()
{

HashSet<String> dups = new HashSet<String>( a );


dups.retainAll( b );

for ( String a : dups )
{
System.out.println( "dup found by retainAll: " + a );
}
}

in.close();

// if any error occured during the try statement the system will display
// the error that it encountered
}
catch(IOException ioe) {
System.out.println(ioe.toString());
}
}


public static void main(String args[]) {
//this gets executed when the java file is run
//this then (starting from the top) does what u tell it
File newFile = new File();
findDupsByRetainAll();
// newFileIn().go;

}
}
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Originally posted by Farah Omarshah:
I have decided to use the retainAll method from the java.util library.
The following presents my code, it wont compile, im not sure what ive done wrong. can anyone help please??

Did you ever mention the funny output when the compiler refuses to finish without problems?

These messages contain:
- A line number, where the compiler encountered an error. Sometimes the correction has to be done at a different location, but most times the line is right or at least very close.
- A message, what the compiler is claiming about.

When you post to a forum, cite the errormessage, and mark the line, because the we can find the error very fast.
Else we have to compile the code ourself, or check every single line.

And please get used to the code-tags!
[ April 02, 2006: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Also calling your class File is likely to be confusing since there is a standard library class java.io.File.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing elements in one hash set to the elements in another