Sorting Values of Set

Use a SortedSet (TreeSet is the default one): SortedSet<String> set=new TreeSet<String>(); set.add(“12”); set.add(“15”); set.add(“5”); List<String> list=new ArrayList<String>(set); No extra sorting code needed. Oh, I see you want a different sort order. Supply a Comparator to the TreeSet: new TreeSet<String>(Comparator.comparing(Integer::valueOf)); Now your TreeSet will sort Strings in numeric order (which implies that it will throw exceptions … Read more

What is the difference between find and firstOrNull?

In this thread from 2014, Kotlin community members and JetBrains staff discuss the merits of the different methods find and firstOrNull: https://youtrack.jetbrains.com/issue/KT-5185 While not an official statement, JetBrains’ employee Ilya Ryzhenkov describes it as: I think we can undeprecate find and make it an alias to firstOrNull. Much like indexOf has well-known semantics, find is … Read more

What’s the max items in a List?

List<T> will be limited to the max of an array, which is 2GB (even in x64). If that isn’t enough, you’re using the wrong type of data storage. You can save a lot of overhead by starting it the right size, though – by passing an int to the constructor. Re your edit – with … Read more

Is there a standard Java List implementation that doesn’t allow adding null to it?

Watch out — several answers here are claiming to solve your problem by wrapping a list and checking in add and addAll, but they’re forgetting you can also add to a List via its listIterator. It’s challenging to get a constrained list right, which is why Guava has Constraints.constrainedList to do it for you. But … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)