0
String [] keys0 =a.keySet(). toArray (new String[a.size()]);

Map< String , TreeSet <Integer>> Sub_hash=new HashMap< String, TreeSet <Integer>>();


for(ko  = 0 ; ko < keys0 . length ; ko ++) {

    Set<Integer> so = test.get( keys0[ko] );    

  System.out.println(""+so);

for(ko1 = ko+1 ; ko1 < keys0.length; ko1++) {

    Set<Integer> so1 = test.get( keys0[ko1] );

    boolean tr=so1.retainAll(so);

            if(tr && so1.size()>=2) {
                Sub_hash. put(keys0[ko]+keys0[ko1], (TreeSet<Integer>) so1);    

                System.out.println(""+Sub_hash.size()+""+Sub_hash);

            }
            }}

this is my second post and i dont know how to post neatly the requirement is i have a hash map with keys and values where i need to compare with one key in the map with another key in the map and retain the values and put the result in the sub_hash map but the problem is the original values of the map are changing as the values of the map are updated by the method retainAll(); but when the iteration comes to second key the values are changed completely but the comparison goes like this 2-3,2-4,2-5,2-6,2-7...etc but as the values are changed result is completely erroneous so is there any chance to make it constant like some variable final to the hashmap.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138

2 Answers2

1

Here's your problem:

Set<Integer> so1 = test.get( keys0[ko1] );
boolean tr=so1.retainAll(so);

You don't want to modify values in test. You want to modify a copy instead:

Set<Integer> so1 = new TreeSet<>(test.get( keys0[ko1] ));
boolean tr=so1.retainAll(so);
Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • i have actually created a copy of that object that is i have used test.putAll(object) and copied the data into test and trying to perform operation of intersection but unexpectedly the values of my map..are restored into my original object where i have referenced it... my requirement is even after intersection the map should be as it is and the values are stored in the sub_hash function.. –  Sep 24 '18 at 00:19
  • { cp2splab=[8, 45, 110, 132, 207], cp2e2lab=[4, 8, 45, 110, 132, 152, 207], cp2itws=[4, 8, 109, 110, 207], cp2cmlab=[4, 8, 45, 110, 132, 152, 207], cp2cp2lab=[4, 8, 45, 109, 110, 132, 152, 207], cp2m2=[4, 110, 132, 152], cp2m3=[4, 8, 110, 132, 152, 207] } –  Sep 24 '18 at 00:20
  • i have columns like {column name: e2; column name: sp; column name: m2; column name: m3; column name: cp2; column name: e2lab; column name: splab; column name: cmlab; column name: cp2lab; column name: itws;} --> my requirement is i need to compare every key with the successive key values and i should retain those values into a sub_hash map but as the above value changes the map for every iteration and i makes it finally an empty set ,,,where there will be no values to be compared... –  Sep 24 '18 at 00:28
  • @naveen that is because when you copy the ***Map*** it doesn't make a "deep copy"! That means the map and its copy both contain references to the ***same Set objects***! Do an internet search to understand what is the difference between a deep copy versus a shallow copy of a Map in java. See also: https://stackoverflow.com/questions/1175620/in-java-what-is-a-shallow-copy – Patrick Parker Sep 24 '18 at 05:00
0

A Map (or any collection) can only contain references to objects, not the objects themselves. When you put a copy of a reference into a collection or get a reference from a collection, you are copying just the reference, not the underlying object.

When you get a set from a Map, and you modify the set, there is only one copy of that set.

NOTE: The Map is not altered when you alter a Set in it.

If you want a copy of a collection, you have to do this explicitly.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • but i have columns like {column name: e2; column name: sp; column name: m2; column name: m3; column name: cp2; column name: e2lab; column name: splab; column name: cmlab; column name: cp2lab; column name: itws;} --> my requirement is i need to compare every key with the successive key values and i should retain those values into a sub_hash map but as the above value changes the map for every iteration and i makes it finally an empty set ,,,where there will be no values to be compared..and my solution becomes erroneous... –  Sep 24 '18 at 00:34
  • cp2=[4, 8, 45, 109, 110, 132, 152, 207], m2=[4, 7, 14, 16, 23, 27, 32, 35, 50, 55, 72, 86, 94, 98, 103, 104, 110, 116, 121, 131, 132, 135, 141, 143, 148, 152, 181, 183, 200, 206], –  Sep 24 '18 at 00:36
  • these are my sets cp2=[4, 8, 45, 109, 110, 132, 152, 207], m2=[4, 7, 14, 16, 23, 27, 32, 35, 50, 55, 72, 86, 94, 98, 103, 104, 110, 116, 121, 131, 132, 135, 141, 143, 148, 152, 181, 183, 200, 206] assume i have hashmap like this where key=cp2 value is{ } and key2 =m2 and value is Set now i am performing the retainAll(); operation by iterating the loop and my hashmap values are changed ....like this cp2=[4, 110, 132, 152], m2=[4, 110, 132, 152], cmlab=[4, 110, 132, 152], m3=[4, 110, 132, 152],} finally the set becomes empty.. –  Sep 24 '18 at 00:45
  • @naveen The Map is not changed as it doesn't have and cannot have in Java a Set in it, it has a ***reference*** to a Set in it. So when you change that one and only one copy of the Set, it appears to change in the Map, but the Map is not altered in any way. What you need to do instead is take a copy of the Set before modifying the copy. This was the copy in the Map won't change. – Peter Lawrey Sep 24 '18 at 06:27
  • its really helpful that's it its a right solution and what i did is i created the same copy of it but a bit modification was done i converted the keys into String[ ] and values into ArrayList[ ] and performed comparisons on it...then i've got the required result... but the above idea has helped as i also created a copy of it and performed operations... but i need a suggestion whether the space complexity is increased by doing like that...like splitting and comparing .. –  Sep 25 '18 at 15:42