Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

please help me to resolve the issue given below in the Java code . Need to check if the 2 keys same

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Master Rancher
Posts: 4081
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code has no comments to help us find the problem.

Are you asking how to compare the contents of two variables (keys) to see if they are the same?
What are the names of the variables?
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies for no code comments but what I need is (key1,key2)->((key1==key2)) does not allow me to compile but I need to get the minimum values of the common keys of the hashmap . So How to i get those here ?

Map<Character, Integer> leastRepeatingCharmap = Stream.concat(l.get(0).entrySet().stream(), l.get(1).entrySet().stream())
              .collect(Collectors.toMap(
                      entry -> entry.getKey().charValue(),
                      (key1,key2)->((key1==key2)),

                      entry -> entry.getValue(), // The value
                      // The "merger"
                      (frequency1, frequency2) ->Math.min(frequency1,frequency2)
                      )
              );
 
Norm Radder
Master Rancher
Posts: 4081
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

does not allow me to compile


Please copy the full text of the error message and paste it here. It has important info about the error.

Also please edit the code and wrap it in code tags: select the code and press the Code button
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Error message is shown below :
Operator'==' Cannot be applied to 'java.lang.Object' ','<lamdaparemeter>'
 
Marshal
Posts: 26283
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to compare whether two objects are equal in Java, use the equals() method. The == operator will compare to see if they are both the same object, which usually isn't what you want.
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks but even equals method is not helping to compile .
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please note that all i need is that I need to pick up the value for common keys and that is why I have put that condition
 
Paul Clapham
Marshal
Posts: 26283
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately the Collectors.toMap methods are extremely complex and your comments so far don't come anywhere near explaining which of the three versions you planned to use, and why. So please start by doing that for us; just saying you need the code to compile is useless.
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please specify what do you mean by versions Iam using Java 1.8.

restating my problem in plain english .,
If I have 2 Compare 2 hashmaps  where hashmap1 has key value pairs as  a=1,b=2,c=3,d=4,e=5 and hashmap2 which has a=6,b=7,c=8,d=9,e=10,f=11 then f is the uncommon key , I need to remove this using Java Java streams  plus  compare hashmap1 and hashmap2 for values of common keys  and chose the minimum values and store the entire result of this comparison and computation in another hashmap .

So in this case the result hashmap will have a=1,b=2,c=3,d=4 and e=5 and since f is uncommon key it should be removed .
Hope this is clear now .
 
Marshal
Posts: 7933
548
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that messy code is hard to locate to which exact area you refering to, but I suggest you start decomposing your code to some smaller pieces, so you could copy/paste a singular small method to us, so it could be inspected.

It is sort of clear what you want, but then again, where those requirements in advance come from, have to be streams... etc.
 
Liutauras Vilda
Marshal
Posts: 7933
548
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And there are various ways to solve that. You could extract keys from hashmaps and put to sets, then use retainAll() which would identify the common ones.
Then by those common ones you could get values from both maps m1 and m2 and compare their values, and do whatever you have to do afterwards.
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but I have given you the statement of the problem very clearly appreciate if you can solve that showing how to use retain all.

For now I have resolved it as shown below .
package com.company;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class adfg
   {

//Method Get Frequency of Counts for each word in the string which returns a hashmap  
        public Map<Character, Integer> Mapofcharfreq(String w)
       {
       System.out.println("String received is" + w);

       Map<Character, Integer> KeyFrequency = new HashMap<>();
       Map<Character, Integer> charcount = new HashMap<>();
       int k = 0;
       int[] freq = new int[w.length()];
       for (int m = 0; m < w.length(); m++) {
                if (charcount.containsKey(w.trim().charAt(m)))
           {
               System.out.println("value of freq before increment for "+w.charAt(m)+"is"+freq[k]);
               freq[k]++;
               System.out.println("value of freq after increment for "+w.charAt(m)+"is"+freq[k]);
               KeyFrequency.put(w.charAt(m), freq[k]);

           } else {
                    //Need to mark the position so that we need to increment here
               freq[m] = 1;
               k=m;
               System.out.println("since "+w.charAt(m)+"is not existing the frequncy is made as "+freq[m]);
               KeyFrequency.put(w.charAt(m), 1);
               charcount.put(w.charAt(m), 1);
           }
       }
       System.out.println("Outside for loop ");
       return KeyFrequency;

       }



//Method to show uncommon key in the  hashmap  which returns a character array
       public Character[] showUnCommonkey(Map<Character, Integer> m1, Map<Character, Integer> m2)
       {

           if (!(m1.keySet().equals(m2.keySet())))
           {
               HashSet<Character> unionKeys = new HashSet<>(m1.keySet());
               unionKeys.addAll(m2.keySet());
               unionKeys.removeAll(m1.keySet());
               Character[] array = new Character[unionKeys.size()];
               unionKeys.toArray(array);
               return array;
           }
           else
           {
               return null;
           }
       }

   public static void main(String[] args) throws IOException
   {
       //String str = "aabccd aaabcccdd aaaabccdd";
       String str = "gghhhiiij gghhhiiijj ghhijk";
       adfg a = new adfg();
       String word[] = str.split(" ");
       List<Map<Character, Integer>> l = new ArrayList();

       for (int z = 0; z <= word.length - 1; z++)
       {
           l.add(a.Mapofcharfreq(word[z]));
           System.out.println("Arrays as list************" + Arrays.asList(l.get(z)));
       }

//Take all the hashmaps stored in the list and get the minimum values using Java streams

                 Map<Character, Integer> leastRepeatingCharmap = Stream.concat(l.get(0).entrySet().stream(), l.get(1).entrySet().stream())
               .collect(Collectors.toMap(
                       entry -> entry.getKey().charValue(),
                           entry -> entry.getValue(), // The value
                       // The "merger"
                       (frequency1, frequency2) ->Math.min(frequency1,frequency2)
                       )
               );


       Map<Character, Integer> leastRepeatingCharmap1 = Stream.concat(l.get(2).entrySet().stream(), leastRepeatingCharmap.entrySet().stream())
               .collect(Collectors.toMap(
                       entry -> entry.getKey(),
                       entry -> entry.getValue(), // The value
                     (frequency1, frequency2) ->Math.min(frequency1,frequency2)
                       )
               );

       //Remove the uncommon key
       Character[] s1=a.showUnCommonkey(l.get(1), l.get(2));
       for (Character s: s1 )
       {
        System.out.println("..........."+s);
           leastRepeatingCharmap1.remove(s);
       };
       System.out.println("Arrays as list>>>>>>>>>" + Arrays.asList(leastRepeatingCharmap1));

   }
   }
 
Paul Clapham
Marshal
Posts: 26283
80
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

anil deshpandey wrote:Thanks for the reply but I have given you the statement of the problem



You aren't the one who gets to decide whether your problem statement is clear. You can answer the question I asked or not, it's up to you.
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies I will try to explain the problem more clearly otherwise are you suggesting something like map1.keySet().retainAll(map2.keySet()); ?
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also please ignore this in case you are not able to resolve this but its upto you .
 
Marshal
Posts: 71655
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

anil deshpandey wrote:. . . but its upto you .

Not at all; it is your responsibility to ask your questions clearly, so we can understand what you need.

Why are you splitting your text into words and then concatenating Streams to put it back together?
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes agree but I would expect you to confirm on the part which you not understood so that I can give another Attempt .
 
Norm Radder
Master Rancher
Posts: 4081
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

which you not understood  


There are NO comments in the code describing what it it trying to do.  So there is nothing to base an understanding of the code's purpose.
 
anil deshpandey
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies but I totally disagree with you for your statement there are comments in the code .
 
Rototillers convert rich soil into dirt. Please note that this tiny ad is not a rototiller:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic