• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collection in java

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sneha Kashyap
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Sneha Kashyap
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The set will contain only the unique words, right?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Thanks a lot. I was able to implement it.
Java Ranch rocks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic