0

I have a Map of key pair as (String,Set(Integer)). In which each entry denotes an occurrences of an element. Now what I want to do is to have a set of elements from that keys and find the occurrences where all those have simultaneously happened. The set will be incremented gradually.

I am stuck on how to form a method with Set of String and Map of String,Set-Integer as input which will return an Set of Integer as output which will contain the intersection of all integers in set in map corresponding to keys from a set of strings.

this is my map

Map<String,Set<Integer>> transactions = new HashMap<String,Set<Integer>>();

This is the Set of string

Set<String> check_set

Now question is how to make this method

Set<Integer> getIntersection(Map<String,Set<Integer>> transactions, Set<String> check_set)

Which will return intersection of all the Set of integers corresponding to the keys in set of string. I know i have to use retainAll function, but what i cant figure out is how to use it. It is a bit confusing.

r_yu
  • 41
  • 1
  • 7
  • It looks to me as though all you need to do is create a result Set containing the elements of any arbitrary value from `transactions`. Iterate over check_set looking up the corresponding value from transactions, and intersect that with your result set. – Patricia Shanahan Jun 16 '14 at 23:23

1 Answers1

0

It's already mentioned here Set Interface Bulk Operations in Oracle Java Tutorials and explained in detail along with examples.

s1.retainAll(s2) — transforms s1 into the intersection of s1 and s2. (The intersection of two sets is the set containing only the elements common to both sets.)


To calculate the union, intersection, or set difference of two sets nondestructively (without modifying either set), the caller must copy one set before calling the appropriate bulk operation.

Set<Type> intersection = new HashSet<Type>(s1);
intersection.retainAll(s2);
Braj
  • 46,415
  • 5
  • 60
  • 76
  • I understand that. I know in order to have an intersection of two sets i have to use retainAll() function. What i cant figure out is how to make a method to get an intersection of a SET of set of Strings(get it?) from inside a map. – r_yu Jun 16 '14 at 22:36
  • Are you talking about intersection between Set and keys of Map that are strings? It will be better if you can explain it using sample code. – Braj Jun 16 '14 at 22:38
  • do you want to retrieve unique keys or values from Map? Simply loop over values and call `intersection.retainAll(values)`, check for size after it if `intersection.size()==0` then there is no need to iterate all the values, simply come out of the loop. – Braj Jun 16 '14 at 22:45
  • I added some more information in question. – r_yu Jun 16 '14 at 22:51
  • The set of keys will keep on increasing which is why i cant figure out how to formulate a logic for it. – r_yu Jun 16 '14 at 22:52
  • why `check_set` is a `Set` instead of `Set`? – Braj Jun 16 '14 at 22:56
  • because it is a set of keys and not values. – r_yu Jun 16 '14 at 22:58
  • Coffee=[1, 4], Bread=[1, 2, 3, 4, 5], Sundae=[3, 4, 5] ... If this is my map then check_set can have {"Bread","Sundae"} and the method should return {3,4,5} – r_yu Jun 16 '14 at 23:00
  • Now finally I got it what you want? why did you not mentioned it at first time when you posted it here? – Braj Jun 16 '14 at 23:02
  • this is my first question here. I guess i didn't know how to ask. – r_yu Jun 16 '14 at 23:05
  • In this sample why {"Coffee","Bread"} is not an answer where {1,4} is common? your requirement is some what not correct. think about it again. – Braj Jun 16 '14 at 23:07
  • yes it could be. that was just an example. There will be a large map with many keys and value. The check_set will be a subset of keys of map and method should return intersections of those keys only. – r_yu Jun 16 '14 at 23:12
  • now you are making me confused. last comment :`method should return intersections of those keys` previous comment : `method should return {3,4,5}` – Braj Jun 16 '14 at 23:16
  • sorry, what i meant was that method should return an intersection of values of keys present in check_set. – r_yu Jun 16 '14 at 23:19
  • So in this example there will be no match, am I right? if yes then what will be the resultant keys and values? – Braj Jun 16 '14 at 23:21
  • in example with check_set having {"Coffee","Bread"} should return {1,4} as both of these integers are present in the sets of Coffee and Bread. – r_yu Jun 16 '14 at 23:24
  • why `{"Bread","Sundae"}` is not an answer where `{3,4,5}` is common as you already suggested in your example? If we consider both the cases then there is no common between all three values. – Braj Jun 16 '14 at 23:26
  • Answer should be a set of integer. If you check the declaration above, check_set is an input parameter. {3,4,5} is the value which should be returned for {"Bread","sundae"}.. I chose a very poor example.. let me come up with some other less confusing one. – r_yu Jun 16 '14 at 23:31
  • It already got your point, you need method returns values and set the keys in passed argument, that's OK but my concern is that it's a valid example and until and unless you answer this question the requirement is not clear. – Braj Jun 16 '14 at 23:32
  • If the MAP is tea=[1,3,5,7,9], milk=[2,3,6,7,9], sugar=[1,4,6,8,9]... then if check_set is {"tea","milk"} then the method should return values which are present in both tea and milk i.e {3,7,9}. Or if it has {"milk","sugar"} then it should return {6,9}. if it has all three {"sugar","milk",tea"} then it should return {3,9}. – r_yu Jun 16 '14 at 23:37
  • are my requirements clear now? – r_yu Jun 16 '14 at 23:41
  • if `super = [1,9]` then what will be the output? My concern is that if there are more than one possibilities than what will be output? – Braj Jun 17 '14 at 06:14
  • If sugar=[1,9] then the output will be [1,9] as it is the intersection between the sets of tea and sugar . – r_yu Jun 17 '14 at 13:35