I know how to find the first element of a list by predicate: Find first element by predicate Is there an easy way to get the index of that element? Answer If I understood correctly, that’s the classic case where you need IntStream; but that would only apply for a List obviously.
Tag: collections
How to pretty print a complex Java object (e.g. with fields that are collections of objects)? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed 5 years ago. The community reviewed whether to reopen this question 4 months ago and left it
detect last foreach loop iteration
Supposing that I have some foreach loop like this: Is there a way to check inside foreach that the actual name is the last one in Set without a counter? I didn’t found here some question like this. Answer There isn’t, take a look at How does the Java ‘for each’ loop work? You must change your loop to use
Java Vowel count using Collection
I am asked to write a code to count and display vowels in a String using Sets. Any suggestions as to why the code below gives wrong output? Switch seemed like the obvious approach, I also tried with an if-statemen, but neither work properly. Answer I’m pretty sure it’s partially because you’re counting ‘Y’ and ‘W’ as vowels. Also, when
List.of() or Collections.emptyList()
As an special case of List.of(…) or Collections.unmodifiableList() – what is the preferred Java 9 way of pointing to an empty and immutable list? Keep writing or switch to Answer What is the preferred Java 9 way of pointing to an empty and immutable list? The difference is rather subtle so “preferred” depends on what you want to achieve. Some
Merge Map<String, List Java 8 Stream
I would like to merge two Map with JAVA 8 Stream: I try to use this implementation: However, this implementation only create a result like: Map<String, List<Object>> If one key is not contained in the mapGlobal, it would be added as a new key with the corresponding List of String. If the key is duplicated in mapGlobal and mapAdded, both
Java sorting is not the same with MySQL sorting
I need to check the sorting in the table and table content is given by MySQL. I’m trying the following: Collections.sort(sorted, String.CASE_INSENSITIVE_ORDER); And get the following result: tes3@test.com test4@test.com test5@test.com test@test.com test_user@mail.com user-og@driver.com And this is what I get from MySQL by query: SELECT ’email’ FROM ‘user’ WHERE 1 ORDER BY ‘user’.’email’ ASC : tes3@test.com test_user@mail.com test@test.com test4@test.com test5@test.com user-og@driver.com
Java collection like c# KeyedColllection
Is there a Java collection that has the same behavior as the c# abstract KeyedCollection class (that is items can be retrieved by both key and index)? I have looked around but can’t find anything similar. Thanks, Nick Answer I think you can develop Your own class by extending a HashMap
How to count number of custom objects in list which have same value for one of its attribute
I am programming in java. Say I have an custom object Item I have list of Items. Now, What is best way in java to know, list of Items contain some Items with same value for itemNumber. Answer To search for a specific item with some item number: To get lists of items for all item numbers: You can use
Convert a for loop to concat String into a lambda expression
I have the following for loop which iterates through a list of strings and stores the first character of each word in a StringBuilder. I would like to know how can I transform this to a lambda expression Answer Assuming you call toString() on the StringBuilder afterwards, I think you’re just looking for Collectors.joining(), after mapping each string to a