• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort String data which is separated by new line and comma

 
Ranch Hand
Posts: 138
1
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The question was to sort the data of a string which is separated by new line character, filter it with certain criteria as well.
I have written the below code as a solution using Java8.
Want to understand the solution is well written or not.
I know there is lots of improvement needed, just want to understand the things.

The code is as below:



Thanks,
Atul
 
gunslinger & author
Posts: 169
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll have to play with this a bit more, but I noticed a couple of things right away:

- you don't have to do the map to "new String(elem)", since the split method already returns an array of String that you turned into a stream
- you don't need the collect at the end if all you plan to do is print them -- you can replace collect(Collectors.toList()) with the forEach(System.out::println) call
- you don't need the null check because the split method will return an empty array but not a null one (and if you want a null check filter(Objects::nonNull) is convenient)
- You don't need the cast; the collect(Collectors.toList()) will already return a List
- The return type is actually List<Document>, so you don't need the ? either

Your thinking process is good, though. It's just a pipeline of maps and filters with a sorted at the end, followed by a terminal expression.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your splitting actually work? You are using "\\n" as a regex; does that actually match the newline character?
I think you shou‍ld only use literal \n and \r characters when you have been told by somebody else that they require CR CR‑LF or LF. Otherwise use String.format("...%n..."); which gives you the correct line end for your operating system. Unfortunately I haven't found a ready‑made regex for line ends; try [\r|\n]+ and see whether that works. More details about line terminators in the Pattern class' documentation.
 
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • getData() is a poor name. Data is a very vague word that doesn't describe what kind of data you're getting, and the method isn't actually getting anything. Rather, it should be called something like parseAndPrintDocuments(), but you can already tell from the name that it does too much. Rather, you should have one method that parses the documents, and print them in another (or just in your main method).
  • Instead of Stream.of(input.split("\\n")), pre-compile a Pattern and use the splitAsStream() method.
  • It's easier to first parse a document descriptor from each line, and then filter the stream of documents.
  • Your Document class should probably be immutable.
  • Don't use String for things other than input, output and identifiers. Use strong types for paths, dates, etc.
  • Your constructor is not performing parameter checking.

  • Here's another way you could do this:
     
    Atul More
    Ranch Hand
    Posts: 138
    1
    jQuery Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Stephan,

    Thanks for your valuable inputs and code.
    Whatever code I mentioned in the post was just a sample program which I created. The actual implementation is far different from what I mentioned. Not similar to yours but upto certain level it matches with your solution.
    I am in process of learning Java8 and its basics.
    From every post which I post here, I am trying to learn something form it, I know the way I described the things are not up to the mark but still I am trying and learn something from the inputs.

    Thanks once gain for your knowledge sharing with us.


    Thanks,
    Atul
     
    Atul More
    Ranch Hand
    Posts: 138
    1
    jQuery Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks to Kenneth and Campbell as well.
     
    I'm still in control here. LOOK at this tiny ad!
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic