• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

How to improve algorithm using streams

 
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. Given sentence for example: ala ma kota, kot koduje w Javie kota I should to every letter occurring in the sentence I should map every string that this letter contains. So the output should be:


I have written algorithm for it, but I think that there is a better way do to this using streams (or somehow else).
Code;


And these two methods which are placed in other class:



Thanks a lot.
 
Saloon Keeper
Posts: 3462
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Must,

I don't know if the following is an improvement, but it uses streams:

 
must Janik
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that using streams is bad idea here. So with no talking about streams, is there any way to improve it and make it efficient?
 
must Janik
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without fetching letters in advance.



Is it better than previous? Can I improve this somehow?
 
Piet Souris
Saloon Keeper
Posts: 3462
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it can.

The result of 'map.computeIfAbsent(k, x -> new V)'is either a new V, or the existing V. So, you can do this:
 
Piet Souris
Saloon Keeper
Posts: 3462
149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words: I don't know a better algorithm
 
must Janik
Ranch Hand
Posts: 103
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even this small improvement (2 lines into 1) is much better. Thanks.
 
Marshal
Posts: 65467
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

must Janik wrote:. . . 2 lines into 1 . . .

Fred Rosenberger told me off for simply writing fewer lines once. Yes, quality code is often shorter than poor code, but simply counting lines can tempt people to format their code badly.
Piet, please explain the compute if absent code a bit more.
 
Sheriff
Posts: 21788
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
computeIfAbsent takes a key and a function. If there is already a non-null value in the map for the key, it is returned. Otherwise, the function is called with the key as argument. The result of the function call, if not null, will be inserted into the map. It will also be returned (even if it's null).

In this case, the function will return a new set. The result will therefore be a non-null set - either the existing non-null set, or the newly created one. By calling add on this return value you save one get call on the map, making it slightly more efficient.
 
please buy this thing and then I get a fat cut of the action:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!