Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

how can I subtract two collection ?

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here the reference type of s1 is  Map<Character, Integer>. I need to subtract values .For example if c1 = [1,3] c2 = [3,6] I need c =[2,3] and alos i want to convert it into array . Please suggest me .

 
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried this? https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeAll-java.util.Collection-
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Have you tried this? https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#removeAll-java.util.Collection-



I am going through this article, but still i couldn't figure out . Kindly let me know if there is any solution you are aware of .  
 
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:here the reference type of s1 is  Map<Character, Integer>. I need to subtract values .
For example,
if                      c1 = [1,3]
                        c2 = [3,6]

               I need c =[2,3] and alos i want to convert it into array . Please suggest me .



I don't get it. I don't understand how "[1,3]" has anything to do with a Map<Character, Integer>. I don't see any Character objects there and there are two Integers. So maybe you could try for a better description of your problem.
 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I was just looking at the subject. Looking at the details you posted, I agree with Paul, please give a clearer description of what you're trying to do. That Collections.removeAll() method doesn't look like it's what you need.
 
Saloon Keeper
Posts: 3893
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, just use a for loop, like:

assuming c1 has the same size as c2.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Sorry, I was just looking at the subject. Looking at the details you posted, I agree with Paul, please give a clearer description of what you're trying to do. That Collections.removeAll() method doesn't look like it's what you need.



I edited the question, now can you please lemme know about it? thank you
 
Marshal
Posts: 68899
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:. . . I edited the question . . .

Don't edit questions after they have been replied to. I have reverted the changes so the replies actually reflect the question. Please post your changes in a new post.
 
Campbell Ritchie
Marshal
Posts: 68899
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Have you tried . . . removeAll[?]

Maps aren't Collections, so I am not convinced that will work easily. I would have thought it is easier to iterate the key set or the entry set for Map1 and remove the mapping:-There is bound to be an easy way to write a Stream:-Not tried it. Don't know whether it will work. The functionality of my two code blocks is different.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i wanted to subtract , i didn't wanted to remove
 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you a solution,  just read my reply. Note that a Collection does not have a get method, so you must use two iterators.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:I gave you a solution,  just read my reply. Note that a Collection does not have a get method, so you must use two iterators.


Thanks I just tried the code :


but I am getting null pointer exception. Here the problem is: map keys are based on character. that is why , we can not loop through the map with int i. That's why I am getting null pointer exception
 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you don't need casting. Note that in your opening post s1 was the Map, and c1 the Collection of the values in that Map.

If you get an NPE in your code snippet, then very likely s1 is null, or s2, Can you check that they have been initialized?

And a Collection does not have the 'get' method, as I wrongly wrote. You must use an iterator to traverse the two Collections, like in:

and likewise for s2.

Then you can enter a loop:

Again, the two Collections should have the same size.

If you get your code up and running, can you explain the idea behind the subtraction of the values?


Edit: the NPE that you get might also come from the fact that (s1 being the Map)  s1.get(i) gives you null as result, since i (probably 0) is not present as key in s1.
 
Campbell Ritchie
Marshal
Posts: 68899
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:actually i wanted to subtract , i didn't wanted to remove

Afraid that wasn't at all clear from the thre‍ad title.
 
Campbell Ritchie
Marshal
Posts: 68899
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets make the problem simplified.    



Here,  s1 and s2 are 2 Map reference variable which I got from calculating countChar() method. For better  understanding, lemme show you what  my s1 and s2 is, once I print :
s2 map:  {a=3, d=1, u=2, e=1, y=1, z=2, n=2}
s1 map: {a=1, d=1, u=1, e=2, y=1, z=1, n=1}

if  I want to subtract values of  s2 from s1, based on key. Basically what i want: values of s2 -  values of s1   based on key. Or in another way I have to subtract the values of one map  from that of another map based on keys.  
However,  the result I want is:    

int[] result = [2, 0 , 1, -1, 0 , 1, 1 ]  // value of  s2 - value of s1 based on key , then the result is in an array form

Please let me know if I need to provide more details. Thank you all.
 
Rancher
Posts: 3714
34
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the API doc for Map and find a method that returns all the keys used in that map.  Then use that list of keys to access the two Maps.
 
Sheriff
Posts: 7051
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also look for the "for each" type loop (sometimes called the enhanced for) which will be helpful here:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html

Look near the end of the page.
 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware that the two maps may differ in the keys.

And to add to Knute's forEach: you can do the exercise in one line, using the Map.merge() method.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:
If you get your code up and running, can you explain the idea behind the subtraction of the values?



I was trying to implement a function :


Here, I have to check:
# of times char 'a' occurs in first string of list1
# of times char  'a' occurs in first string of list2
--------------------------------------------------
--------------------------------------------------
until # of times char 'z' occurs in first string of list1
# of times char  'z' occurs in first string of list2

if all differences are less than a certain number like  3 for all digits, then both the string are similar, otherwise it is not.
later on, I  have to compare   all of the strings for both the list based on character.
In order to find out number of times a character occurs, I used HashMap .


Still I am getting null pointer exception in the code below:





thank you
 
Paul Clapham
Marshal
Posts: 25436
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yeah. Look at line 2. And then of course you're going to get a NPE at line 7.
 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with that method is that it does not take the keys into account. For instance, if List A contains the letter 'a', and List B does not, then you see you cannot just take the differences of the frequencies.
In your opening post, you talked about the frequencymaps of both Lists, and that is the way to go. You have been given some tips of how to get the correct differences. Once you have that difference map, you can see if all values are in the range [-3, 3]. Do you need an array, once you have that Mao of differences?
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, yeah. Look at line 2. And then of course you're going to get a NPE at line 7.



I didnt get it. Actually, I already populated  both the list with string  data. ultimate array should have string data according to line 7
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Beware that the two maps may differ in the keys.

And to add to Knute's forEach: you can do the exercise in one line, using the Map.merge() method.



for the sake of simplicity , i considered two list are same in size. I am facing one more problem. I am getting null pointer exception even though I populated the list with data.



thank you
 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Paul explained, in line 2 you set your String[] ultimate to null. Next you use that same null array in line 7, and that gives you this NPE. Save is to set the String[] to an initial size of list1.size(), like in:

Does the method 'compareMapping' return a String?
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:As Paul explained, in line 2 you set your String[] ultimate to null. Next you use that same null array in line 7, and that gives you this NPE. Save is to set the String[] to an initial size of list1.size(), like in:

Does the method 'compareMapping' return a String?


it worked. yeah, it returns string // here, signature of the method:  String compareMapping(String, String)
I declared the string as null, then I tried to access it. lol. Sometimes I do  mistake. Good thing is that i am learning from my mistake. thanks all of you for correction.
 
Bartender
Posts: 7065
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:Lets make the problem simplified.    
Here,  s1 and s2 are 2 Map reference variable which I got from calculating countChar() method. For better  understanding, lemme show you what  my s1 and s2 is, once I print :
s2 map:  {a=3, d=1, u=2, e=1, y=1, z=2, n=2}
s1 map: {a=1, d=1, u=1, e=2, y=1, z=1, n=1}

if  I want to subtract values of  s2 from s1, based on key. Basically what i want: values of s2 -  values of s1   based on key. Or in another way I have to subtract the values of one map  from that of another map based on keys.  
However,  the result I want is:    
int[] result = [2, 0 , 1, -1, 0 , 1, 1 ]  // value of  s2 - value of s1 based on key , then the result is in an array form


Using a Map for a character histogram makes sense to me, more than arrays without keys.

 
Md Zuanyeed Kamal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:



Initally, I was trying count the frequency of characters in a string. because i was trying to compare two list if string. My input argument was (List<String> list1, List<String>list2)then I have to comapre string by string from both the list based on character frequency. If you can fit in List<String>, then it might work.But I couldn't do that way.
 
Piet Souris
Saloon Keeper
Posts: 3893
154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a method to turn a List<String> into a Map<Character, Integer> (one of the occasions where old fashioned for loops are much simpler than modern streams)

Now you can apply Careys method, but be aware that that method only takes common characters into account.
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic