How to move specific item in array list to the first item

What you want is a very expensive operation in an ArrayList. It requires shifting every element between the beginning of the list and the location of C down by one. However, if you really want to do it: int index = url.indexOf(itemToMove); url.remove(index); url.add(0, itemToMove); If this is a frequent operation for you, and random … Read more

why HashMap Values are not cast in List?

TL;DR List<V> al = new ArrayList<V>(hashMapVar.values()); Explanation Because HashMap#values() returns a java.util.Collection<V> and you can’t cast a Collection into an ArrayList, thus you get ClassCastException. I’d suggest using ArrayList(Collection<? extends V>) constructor. This constructor accepts an object which implements Collection<? extends V> as an argument. You won’t get ClassCastException when you pass the result of … Read more

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

Arrays.asList returns a List implementation, but it’s not a java.util.ArrayList. It happens to have a classname of ArrayList, but that’s a nested class within Arrays – a completely different type from java.util.ArrayList. If you need a java.util.ArrayList, you can just create a copy: ArrayList<Foo> list = new ArrayList<>(Arrays.asList(sos1.getValue()); If you don’t need an ArrayList just … Read more