• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java algorithm for add two collection in a smooth way

 
Alex Miel
Greenhorn
Posts: 9
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new in a Java world and I wonder if there is possible add two collection and save result to third without using loop. I want to achieve something like in c++ code below. I was thinking about new Java 8 features like lambdas and java.util.function.



EDIT: After executing this code, in vec3 there are {5, 7, 9}.
 
Rob Spoor
Sheriff
Pie
Posts: 20555
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Although you will not be able to do this without loops, you can hide those loops. java.util.Collection has method addAll, which takes another collection and adds all of its elements to the current one. Most Collection implementations also have a constructor that takes another collection, so you can be done with two statements: one for creating the union with the contents of the first collection, and one for adding all elements of the second.

In Java 8, you can even do this in one line:
 
Alex Miel
Greenhorn
Posts: 9
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for quick respond. I probably didn't specify exactly my problem. I have two containers:

vec1 = [1 2 3]
vec2 = [4 5 6]

After adding this two I want to get:

vec3 = [5 7 9]

This is exactly what code from my previous post does .

 
Piet Souris
Rancher
Posts: 1297
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried this myself, in the way that Scala does it.
In Scala, you would simply do something like
List<Int> sum = List1<Int> zip List2<Int> map ((a, b) => a + b)

(or even shorter versions).

Now, the only thing I could come up with was to define a


and creating a method

Then you could use
List sum = zip(list1, list2).stream().map(some function).collect(Collectors.toList());

Haven't tried it out, yet,

Greetz,
Piet
 
Piet Souris
Rancher
Posts: 1297
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just tried it out. Beware: this forum's code formatter leaves out a lot of underscores.
But I reckon that anyone is capable of seeing where the underscores are missing.

As said, this is the closest I could come up with. If someone has
a better way, I'd love to hear it.

Greetz,
Piet
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sadly, Java 8 has no zip() and zipWith() methods. I rolled my own Stream class which I can use to easily add my own functors to streams. I can then do the following:
This prints the following:
[(1,a), (2,b), (3,c)]
[a, c, d, e, f, g, h, i, j, k]


The zip methods look like this:
Filename: MyStream.java
Description:
File size: 8 Kbytes
[Download MyStream.java] Download Attachment
Filename: Pair.java
Description:
File size: 959 bytes
[Download Pair.java] Download Attachment
 
Piet Souris
Rancher
Posts: 1297
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Goodness Gracious!

This level of coding is way above my current level, but it gives me something to strive for.
Thanks for the downloads. Having read the tutorials on lambda's and bulk operations,
I'm now struggling with the API's about Streams, Spliterators, IntConsumers, BiFunctions,
Collectors, and tons of type parameters. So I think it will gonna take me a while, to put it mildly.
Maybe I should invest in a good book.

Greetz,
Piet

 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it took me quite a while to figure it all out as well. The combination of generics, laziness, lambdas and parallelism makes this the most difficult area in Java I've encountered yet, outside of third party libraries. Hang in there. I recommend carefully reading the package documentation of java.util.streams.*;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic