Map.containsKey() vs Map.keySet().stream().anyMatch() [closed] - java

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
What do you think is more efficient: HashMap.containsKey(key) or HashMap.keySet().stream().anyMatch(predicate) ?
Thanks

Map is an interface, it does not make sense to speak about efficiency or performance without a specific implementation.
But let's take HashMap as one of the common implementations.
HashMap.containsKey is amortized O(1).
Map.keySet().stream().anyMatch(predicate) is O(N) as you iterate over keys. And we don't even mention all the objects created by this statement.

Related

Java stream alternative to LambdaJ index [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
What is Java stream API alternative to LambdaJ indexing? Let's say I have code like this
List<Product> products = ...
Map<Month, Product> productsOnMonths = Lambda.index(products, Lambda.on(Product.class).getMonth());
Where I know that every product has unique month attribute.
products.stream().collect(Collectors.toMap(Product::getMonth, s -> s));
The difference here is that Collectors.toMap can take a third argument that says how to merge two entries when they are the same; I don't think lambdaj offers that

Why do we have int type in Java? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Why do we have int type in Java? Couldn't the only numeric type be double (and maybe float)? You can keep any integer number in a variable of type double.
For performance reasons and memory saving. Plus , the fact that the numbers are precise with no decimal values is very handy for a lot of cases.

What is the Java keyword that indicates a reference variable does not access an instance? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have tried all sorts of keyword related but I keep getting it wrong.
The only word in Java that precisely meets your vague description is null. If so, it isn't a keyword, it's a literal.

How do I use only one element of an ArrayList? Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If I have an initialized array in Java and I want to do a method to one element in that array I would do
array[n].method();
but how do I do the same thing with an arraylist?
arraylist[n].method() gives me an error.
With Java use get method with ArrayList:
arraylist.get(n).method();

Make a list from the other different type of list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a List<A> of data, and wanna store it to the other list(List<B>).
any suggestion to make it possible?
You can use the Collections.copy(ListA, ListB) method, or even, if you're working with ArrayLists: ListB = ListA.clone()

Categories