wood burning stoves 2.0*
The moose likes Java in General and the fly likes Collection in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collection in java" Watch "Collection in java" New topic
Author

Collection in java

Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Hi,

I have a set<String> with some data and I would like to perform something like this..

for each set<String> compare it with all the Set<String> in a file. I am trying to search if the elements in Set are present in the file if so increment the count variable

I am not able to write code for the same please help

I tried using two for-each loops but I get a cross product of all the sets. I know that the logic to implement this is something similar to bubble sort but I would like to know how to implement it using Set.

Please Help ASAP.
Jonas Isberg
Ranch Hand

Joined: Mar 18, 2003
Posts: 118
If you would just look for one specifik string in the file, just to say if it is precent or not,
how would you do that?

Lets say that the string you are searching for is James and the file is
thefilewithstrings wrote:Sara
James
Lisa
What code would you write to solve this?
Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Hi,

data in the file(will be read into a Set) (I am not exactly sure as to which collection to be used for this purpose) Please suggest the collection to be used.

abc def ghi
abc ghi
def ghi abc
uvw xyz
xyz abc
xyz def

Set<String> contains
abc
def
ghi
uvw
xyz

Each Set<String> must be compared with all the lines in the file and then the occurance count of the corresponding Set<String> and its value has to be stored on the HashMap.
This is to be done but I'm unable to figure out how to do it. Please help.

Thanks in advance.
ravindra patil
Ranch Hand

Joined: Sep 01, 2006
Posts: 214
Hi

split the values you are reading from file using Split with space as delim
convert set into array iterate it or you can directly iterator set itself ..do string comparison using equals ...

let me know you still dont understand
Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Hi Thanks,

I would be grateful if you could write some sample code.

BTW I have a HashMap<List<String>,Integer> I am not able to display the values in the map,I am not able to iterate
I have used
for(Entry<List<String>, Integer> entry:candidates.entrySet()){
System.out.println("Item:"+entry);
}
But the there are no values displayed.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

The java.lang.Set class has a really nifty method that lets you know if it contains another Set or object.

http://java.sun.com/javase/6/docs/api/java/util/Set.html

-Hunter M.


"If the facts don't fit the theory, get new facts" --Albert Einstein
Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Hi Thanks,

I know that containsAll() method can be used to fine if a set is the subset of another. My problem is I am not able to write code to traverse the set and the file.
Please help me with this
ravindra patil
Ranch Hand

Joined: Sep 01, 2006
Posts: 214
HI Sneha ,

try this i dont know how you have written your code



Set set = candidate.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
System.out.println(entry.getKey() + " : " + entry.getValue());

check out whether this work hope it will work
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Sneha Kashyap wrote:Please Help ASAP.

Please EaseUp.

ravindra patil wrote:Set set = candidate.entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
System.out.println(entry.getKey() + " : " + entry.getValue());

Surely you meant to a) UseCodeTags, and b) use generics and the for-each loop?

Sneha Kashyap wrote:I am not able to write code to traverse the set and the file.

You know how to use a for-each loop? For traversing the file check out BufferedReader and its readLine() method, although since you want to split the file into all separate words a Scanner seems to be better:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Hi Thanks Rob,
Let me explain my problemin detail
1. I want to read a file with each line stored into a seperate set.
2. I want to compare these sets(data from file) (to find if it is a subset) with another set containing similar data (I have used containsAll)
3. Find the frequency of each set and store the set and its count in a hashMap and all these has to be done recursively with the length of the set increasing.
Help me with this .
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1400
    
    6

The set will contain only the unique words, right?


Swastik
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Sneha Kashyap wrote:1. I want to read a file with each line stored into a seperate set.

BufferedReader.readLine() or Scanner.nextLine() is the way to go. For each line create a new Set (String.split() is your friend) and store those in a List<Set<String>>.

2. I want to compare these sets(data from file) (to find if it is a subset) with another set containing similar data (I have used containsAll)

Set.containsAll returns whether or not the argument is a subset of the set. You just iterate through the List from step 1, and compare each element with the other set.

3. Find the frequency of each set and store the set and its count in a hashMap

What is the frequency of a set?

and all these has to be done recursively with the length of the set increasing.

I think I get the final requirement.

With the example you've given in your second post in this thread, you want something like this:
[abc] occurs 4 times
[def] occurs 3 times
[ghi] occurs 3 times
// other 1 element sets here
[abc, def] occurs 2 times
[abc, ghi] occurs 3 times
[def, ghi] occurs 2 times
// other 2 element sets here
[abc, def, ghi] occurs 2 times
// other 3 element sets here
// 4 element sets here
// etc

That last step will be a bit hard. This includes generating all sub sets from a set. There has been a thread before on how to do that, and the answer was that it would be quite complex.
Sneha Kashyap
Greenhorn

Joined: May 06, 2009
Posts: 22
Hey Thanks a lot. I was able to implement it.
Java Ranch rocks
 
 
subject: Collection in java
 
Similar Threads
StringTokenizer Class......
Sun JSSE -- Weblogic Compatibility
Extracting a WAR file
Populate multiple pojo's.
New Line in Java?