Is there an accepted Java equivalent to Python's zip()? [duplicate] - java

This question already has answers here:
How to most elegantly iterate through parallel collections?
(8 answers)
Closed 6 years ago.
I've got two List objects and I want to pair them up, just like the zip() function in Python. I'm pretty sure this isn't available in the JDK, but is there something like this in a fairly widespread library, similar to Apache Commons Collections? Thanks.

Functional Java has zip, zipWith and zipIndex the way you would expect from Haskell or Scala. (Indeed, the authors are pretty much all Haskell programmers.)

Related

Java method().method().method(); How to do this? [duplicate]

This question already has answers here:
How to achieve method chaining in Java?
(4 answers)
Closed 3 years ago.
For example how do I do this?
getServer().getManager().registerNewUser(arguments);
and
getServer().getStats().user(arguments);
Stuff like this, I've tried searching the web but I cant find any tutorials on this,so I'm asking here.
In OOP, in general, this concept is called class composition more on this you can find it here https://en.wikipedia.org/wiki/Object_composition
Also for java and the difference between Composition and inheritance https://www.baeldung.com/java-inheritance-composition

What's the point of Interfaces? [duplicate]

This question already has answers here:
When should one use interfaces?
(19 answers)
Closed 5 years ago.
I'm having trouble understanding why I should use interfaces, and how I should integrate them into my current project. I use a lot of polymorphism already, and I usually see polymorphism and interfaces side by side in other projects of my peers.
Why do you use interfaces? What are the real benefits?
This question has already been answered in:
When should one use interfaces?
What is an interface in Java?
Java Interfaces?
To get more information about OOP design you can also refer to books like Head First: Object oriented analysis and design. There are also some on-line courses on platforms like Coursera and Edx that you can use.

Why is there no Stream.flatMap(Collection) method? [duplicate]

This question already has an answer here:
Why can't Stream.flatMap accept a collection?
(1 answer)
Closed 5 years ago.
Currently, to convert a List<List<Foo>> to a Stream<Foo>, you have to use the following:
Stream<Foo> stream = list.stream().flatMap(fs -> fs.stream());
//or
Stream<Foo> stream = list.stream().flatMap(Collection::stream);
I think this is exactly what the method references were designed for, and it does improve readability quite a bit. Now consider this:
Stream<Bar> stream = list.stream().flatMap(fs -> fs.getBarList().stream());
Having two chained method calls, no method reference is possible, and I've had this happen to me a few times. While it is not a big issue, it seems to drift away from the method-reference brevity.
Having worked with JavaFX 8 a bit, I noticed that a constant of their API's is the convenience methods. Java is a very verbose language, and it seemed to me that simple method overloads were a big selling point for JavaFX.
So my question is, I wonder why there is no convenience method Stream.flatMap(Collection) that could be called like:
Stream<Bar> stream = list.stream().flatMap(Foo::getBarList);
Is this an intentional omission by the folks at Oracle? Or could this cause any confusion?
Note: I'm aware of the "no-opinion-based-questions policy," and I'm not looking for opinions, I'm just wondering if there is a reason that such a method is not implemented.
Because Stream is already a pretty big interface and there's resistance to making it bigger.
Because there's also the workaround list.stream().map(Foo::getBarList).flatMap(List::stream).
You can also see the original discussion at http://mail.openjdk.java.net/pipermail/lambda-libs-spec-observers/2013-April/001690.html ; I'm not seeing that option specifically discussed; it may have been discarded already at that point?

Purely Object Oriented Languages [duplicate]

This question already has answers here:
Is java Purely Object Oriented?
(12 answers)
Closed 6 years ago.
What does the term Purely Object Oriented mean in case of Programming Languages? Is Java a purely Object Oriented and which languages come under this category? I've often read that Purely Object Oriented languages are one in which everything comes in form of objects and so can anyone clarify the confusion.
Java still has the primitives like short , int , long , float and double
these are not considered objects. However, in ruby for example everything,
including the numbers are objects.

Why NOT to use wildcard imports like java.util.*? [duplicate]

This question already has answers here:
Why is using a wild card with a Java import statement bad?
(18 answers)
Closed 9 years ago.
I am a student, and a couple of the books I have been reading (Java for Dummies, for one) has said using the wildcard import statement is bad programming practice and encourage the reader to avoid using it. Whereas, in class, we are encouraged to use it. Can somebody please explain why it is poor programming practice?
If so, what adverse affects does it have on the program performance? For example, slow it down.
The more you insert, the higher the change that you will get a naming collision where two classes have the same class name:
http://en.wikipedia.org/wiki/Name_collision
The first example i can find within the java API are:
http://docs.oracle.com/javase/6/docs/api/javax/naming/Binding.html
http://docs.oracle.com/javase/6/docs/api/org/omg/CosNaming/Binding.html

Categories