aspose file tools*
The moose likes Features new in Java 8 and the fly likes Java algorithm for add two collection in a smooth way Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "Java algorithm for add two collection in a smooth way" Watch "Java algorithm for add two collection in a smooth way" New topic
Author

Java algorithm for add two collection in a smooth way

Alex Miel
Greenhorn

Joined: Jul 30, 2014
Posts: 8

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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alex Miel
Greenhorn

Joined: Jul 30, 2014
Posts: 8

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
Ranch Hand

Joined: Mar 08, 2009
Posts: 691
    
  11
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
Ranch Hand

Joined: Mar 08, 2009
Posts: 691
    
  11
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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:


[Download MyStream.java] Download

[Download Pair.java] Download

Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 691
    
  11
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.*;
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Java algorithm for add two collection in a smooth way