Initialize list with both a single object and another list of objects

If the order of the elements is not important, you can use: List<MyObject> newList = new List<MyObject>(listOfObjects) { object1 }; This works by using the List<T> constructor which accepts an IEnumerable<T>, then the collection initializer to add the other items. For example, the following: static void Main() { int test = 2; List<int> test2 = … Read more

cast a List to a Collection

List<T> already implements Collection<T> – why would you need to create a new one? Collection<T> collection = myList; The error message is absolutely right – you can’t directly instantiate an interface. If you want to create a copy of the existing list, you could use something like: Collection<T> collection = new ArrayList<T>(myList);

Comparator.nullsLast does not avoid NullPointerException

You should use Comparator.nullsLast twice: list.sort(nullsLast(comparing(Bean::getVal, nullsLast(naturalOrder())))); First nullsLast will handle the cases when the Bean objects are null. Second nullsLast will handle the cases when the return value of Bean::getVal is null. In case you’re sure there aren’t any null values in your list then you can omit the first nullsLast (as noted by … Read more

The best way to transform int[] to List in Java? [duplicate]

There’s probably a built-in method to do it somewhere* (as you note, Arrays.asList won’t work as it expects an Integer[] rather than an int[]). I don’t know the Java libraries well enough to tell you where that is. But writing your own is quite simple: public static List<Integer> createList(int[] array) { List<Integer> list = new … Read more

Java Stream: find an element with a min/max value of an attribute

Stream<String> stringStream = stringList.stream(); String coolest = stringStream.reduce((a,b)-> coolnessIndex(a) > coolnessIndex(b) ? a:b; ).get() if calling coolnessIndex is expensive we can use distinct so it’s called only for distinct elements (assuming that the coolnesIndex is the same for equal elements) Stream<String> stringStream = stringList.stream().distinct(); String coolest = stringStream.reduce((a,b)-> coolnessIndex(a) > coolnessIndex(b) ? a:b; ).get() Stream … Read more

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