• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Arithmetic operation on multidimensional array using Streams

 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this thread https://coderanch.com/t/679348/java/Java-Streams-Convert-multidiamentional-array which gives a nice example of converting a multidimensional array to a nested List. I am interested in a similar application but rather applying mathematical operations to arrays of higher dimension.

Here is my best shot. The idea is just to double each entry of a 2D array using streams.

import java.util.Arrays;



Error is "bad return type in lambda expression". Thanks for any help.
 
Marshal
Posts: 80140
418
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remembering that Java® doesn's actually support 2D arrays; it supports arrays of arrays, which are better . . .
Don't know, but have a few hints:-
  • 1: Indent Streams code differently. Every time you call a method on a Stream after the first call, push the enter key and indent the code so the dots align vertically.
  • 2: Use an IDE. If you hover your mouse over the method call you will get a popup with its return type shown e.g. Stream<Foo>. At least that is what Eclipse does, so I expect NetBeans and IntelliJ will have similar functionality.
  • 3: Consider not using arrays of primitives; maybe use Double[]s instead.
  • 4: I am 99% sure that flatMap takes a Stream<Foo[]> and creates a Stream<Foo>, so it is probably doing the exact opposite of what you want.
  • 5: Rather than assigning the arrays in lines 4 and 6, consider printing them with System.out.println(Arrays.deepToString(...
  • 6: Use the peek() method to view the contents of your Streams whilst in development for debugging.
  • In your line 4, you are creating a DoubleStream, which has a no-args toArray() method returning a double[]. If you had a Double[] you would be better off the Stream#toArray() method with a parameter: myStream.toArray(Double[]::new)

    What is wrong with writing a method which takes a double[] as its parameter and returns a new double[] with changed values? Call that method with a λ from the map() method.

    Actually, by going through by hand and working out the types, there is quite a simple solution and you are not that far off. Here it is I think a few misspellings have been introduced in copying but you can retain about half of your line 6 and change the other half.

    Addition:-

    java LambdaTest
    [2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 0.0]
    [[1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]
    [[2.0, 4.0, 6.0], [2.0, 4.0, 6.0]]

     
    Eric Barnhill
    Rancher
    Posts: 241
    Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much for the tips. Use of Double[] was key, then this works:



    Although as you also suggested, calling a 1D method within the stream will make more design sense most of the time.
     
    Campbell Ritchie
    Marshal
    Posts: 80140
    418
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done

    This is my solution minus the “spelling errors”, and I shall let you work out what mistake I made.
     
    Author
    Posts: 161
    31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would suggest this:



    Edit: Oops! Sorry, I should have refresh the page before posting my answer, which i the same as yours!

    Edit2: Not exactly the same in fact.
     
    Campbell Ritchie
    Marshal
    Posts: 80140
    418
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That picks up my mistake; because I forgot to write double[][]::new in line 16, I got an Object[] instead of a double[][].

    double[] array1D = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

    You realise you are allowed to miss out new double[] because you are declaring and initialising the array on the same line?
     
    Pierre-Yves Saumont
    Author
    Posts: 161
    31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Not sure to understand what you mean. Where should "new double[]" go?
     
    Campbell Ritchie
    Marshal
    Posts: 80140
    418
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
  • 1: I missed out double[][]::new in my last toArray() call, so I got an Object[] rather than a double[][].
  • 2: In OP's original code it says double[] array1D = new double[]{...}; Because the declaration and initialisation are on the same line that can be shortened to double[] array1D = {...};
  • Sorry, I have confused you by not being clear that second part was directed at the OP.
     
    F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic