Right now I can only implement the Cartesian product of two collections, here is the code: This code works fine in IntelliJ, but not in Eclipse. Both with compiler compliance level of 1.8: Here is Pair.java: How to fix this error? Is there an elegant way to implement the Cartesian product of several collections? Suppose we have class tuple. Answer
Tag: collections
Declaring initial capaity for a list in java is a bad technique?
I have multiple ArrayLists with the same capacity. I fill these lists by reading a file. I know that one difference between an Array and an ArrayList is that the Array have a fixed capacity while the ArrayList have a variable capacity. You should explicitly specify the array length when you declare it, but the ArrayList re-size itself when gets
Idiomatic way to create a Stream from a Nullable object
What is the best/idiomatic way of performing a null-check before generating a stream? I have a method that receives a List that might be null. So I can’t just call stream() on the value that is passed. Is there some static helper in that would give me an empty stream if a value is null? Answer I agree with Stuart
Any way to stream a map like “(k,v)” instead of working with (entry)?
Basically I look for a way to avoid working with and similar to what Map.forEach() does. If only I could get a way to work as map.stream().filter((k,v) -> )… and so on It seems the interface is called BiConsumer. Perhaps with a converter to BiConsumer or a Stream.generate() someway Answer Since this is a repeating question, I will throw a
How do I keep the iteration order of a List when using Collections.toMap() on a stream?
I am creating a Map from a List as follows: I want to keep the same iteration order as was in the List. How can I create a LinkedHashMap using the Collectors.toMap() methods? Answer The 2-parameter version of Collectors.toMap() uses a HashMap: To use the 4-parameter version, you can replace: with: Or to make it a bit cleaner, write a
Perform operation on n random distinct elements from Collection using Streams API
I’m attempting to retrieve n unique random elements for further processing from a Collection using the Streams API in Java 8, however, without much or any luck. More precisely I’d want something like this: I want to do it as efficiently as possible. Can this be done? edit: My second attempt — although not exactly what I was aiming for:
How to convert String into Hashmap in java
How can I convert a String into a HashMap? into Where the keys are first_name, last_name and gender and the values are naresh, kumar, male. Note: Keys can be any thing like city = hyderabad. I am looking for a generic approach. Answer This is one solution. If you want to make it more generic, you can use the StringUtils
What is difference between Collection.stream().forEach() and Collection.forEach()?
I understand that with .stream(), I can use chain operations like .filter() or use parallel stream. But what is difference between them if I need to execute small operations (for example, printing the elements of the list)? Answer For simple cases such as the one illustrated, they are mostly the same. However, there are a number of subtle differences that
Java collections faster than c++ containers?
I was reading the comments on this answer and I saw this quote. Object instantiation and object-oriented features are blazing fast to use (faster than C++ in many cases) because they’re designed in from the beginning. and Collections are fast. Standard Java beats standard C/C++ in this area, even for most optimized C code. One user (with really high rep
How to copy a java.util.List into another java.util.List
I have a List<SomeBean> that is populated from a Web Service. I want to copy/clone the contents of that list into an empty list of the same type. A Google search for copying a list suggested me to use Collections.copy() method. In all the examples I saw, the destination list was supposed to contain the exact number of items for