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
Related
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 1 year ago.
Improve this question
Is there a more elegant way to achieve by using java 8 or above the eighth version what's below?
List<String> exampleList = List.of("test","test1");
exampleList.stream().filter(s -> s.equals("test")).findFirst();
Thanks in advance
It depends on what exactly you want to do.
If you just want to check if "test" is in one of the elements, you could just use .contains():
List.of("test","test1").contains("test");
If you want to find the first element fitting a condition, you can omit creating the list and directly create a Stream:
Stream.of("test","test1").filter(s->"test".equals(s)).findFirst()
If you want to check if an element fitting the condition exist, you can use anyMatch:
Stream.of("test","test1").anyMatch(s->"test".equals(s))
This is probably the best your going to get.
List<String> exampleList = List.of("test","test1");
exampleList.stream().filter("test"::equals).findFirst();
If its reused you can just make a method out of the 2nd line. Question has also already been answered here
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 3 years ago.
Improve this question
object Test extends App {
val i: Iterable[(String, Long)] = List(("a", 1), ("b", 2))
val sortedMap: SortedMap[String, Long] = i.toList.sortBy(_._2)
}
I don't want to convert Iterable to List/Array etc since it's coming form a jdbc query.
You can't do that. SortedMap sorts by keys, not values.
If you want it sorted by value, you gotta use ListMap, and can't avoid converting to List:
ListMap(i.toList.sortBy(-_._2):_*)
There isn't really too much wrong with converting to list, since you are loading the whole thing in memory anyway. This is faster too, than building a tree one element at a time.
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.
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
Is it possible to search without iterator without using map.
Map<String,Short> map = new HashMap<String,Short>();
map.put("String2", (short)2);
map.put("String1", (short)1);
map.put("String3", (short)4);
I am looking for a way to get the value based on the key (Return 2 for value String2). Is Map the right one to use in this scenario.
Thanks !
Returning values based on a key is precisely what maps are for.
To retrieve your value you can simply do:
short returnVal = map.get("String2");
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()