Thursday, October 19, 2017

URLify in java 8 style functional flavor


 Write a method to replace all spaces in a string with '%20'


Instead of using str.replaceAll("\\s", "%20"), here is a function. This exercise is to help learning.



public String URLifyJava8(String str) {
return str.chars()
.mapToObj(c -> (char) c)
.flatMap(c -> Character.isWhitespace(c)
? "%20".chars().mapToObj(ch -> (char) ch) : Stream.of(c))
.collect(Collector
.of(StringBuilder::new, StringBuilder::append,
(r1, r2) -> {
r1.append(r2);
return r1;
},
StringBuilder::toString, Collector.Characteristics.CONCURRENT));
}

7 comments:

  1. Excellent read, I just passed this onto a colleague who was doing a little research on that. And he just bought me lunch because I found it for him smile Thus let me rephrase that: Thank you for lunch!
    software development outsourcing company
 

    ReplyDelete
  2. Don't know, haven't faced such an issue before, but hope that solution of your works well under any circumstances. Literally, I'm not into these things that much, just new to java (entirely new-new, to be frank), so I'm just hanging around with this soft, trying to understand how things work manually. I had no practice on that before, but my knowledge of fundamentals are clear, so I have filled all the gaps with this resource on learn java explainjava.com/category/java/java-core/ so I have some solid confidence on that. But when I've wandered in this thread and seen what things you guys are doing here, it's pretty impressive

    ReplyDelete
  3. I like this post,And I guess that they having fun to read this post,they shall take a good site to make a information,thanks for sharing it to me.
    software development company in delhi

    ReplyDelete
  4. bike racks could really help you secure your bike when you leave it ,. stiply.nl elektronische handtekening

    ReplyDelete
  5. I just couldn’t depart your site prior to suggesting that I extremely enjoyed the standard information an individual provide for your visitors? Is gonna be back frequently in order to inspect new posts sd wan technology

    ReplyDelete