arraylist
casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
For me (using Java 1.6.0_26), the first snippet gives the same exception as the second one. The reason is that the Arrays.asList(..) method does only return a List, not necessarily an ArrayList. Because you don’t really know what kind (or implementation of) of List that method returns, your cast to ArrayList<String> is not safe. The … Read more
Why isn’t ArrayList marked [Obsolete]?
I think it should be considered effectively obsolete for new code, but there’s no compelling reason to mark it obsolete and create warnings in all code which was written before 2.0 was released. In my experience, most of the types and members which have been marked obsolete by Microsoft are actively dangerous in some respect, … Read more
Arrays.asList() of an array
Let’s consider the following simplified example: public class Example { public static void main(String[] args) { int[] factors = {1, 2, 3}; ArrayList<Integer> f = new ArrayList(Arrays.asList(factors)); System.out.println(f); } } At the println line this prints something like “[[I@190d11]” which means that you have actually constructed an ArrayList that contains int arrays. Your IDE and … Read more
Java 8 lambda create list of Strings from list of custom class objects
You need to collect your Stream into a List: List<String> adresses = users.stream() .map(User::getAdress) .collect(Collectors.toList()); For more information on the different Collectors visit the documentation. User::getAdress is just another form of writing (User user) -> user.getAdress() which could as well be written as user -> user.getAdress() (because the type User will be inferred by the … Read more
When to use ArrayList over array[] in c#?
Arrays are strongly typed, and work well as parameters. If you know the length of your collection and it is fixed, you should use an array. ArrayLists are not strongly typed, every Insertion or Retrial will need a cast to get back to your original type. If you need a method to take a list … Read more
Java ArrayList of ArrayList
You are adding a reference to the same inner ArrayList twice to the outer list. Therefore, when you are changing the inner list (by adding 300), you see it in “both” inner lists (when actually there’s just one inner list for which two references are stored in the outer list). To get your desired result, … Read more
How does memory allocation of an ArrayList work?
If JVM is not able to allocate requested amount of memory it’ll throw OutOfMemoryError That’s it. Actually JVM memory allocation has only two possible outcomes: Application is given requested amount of memory. JVM throws OutOfMemoryError. There is no intermediate options, like some amount of memory is allocated. It has nothing to do with ArrayList, it’s … Read more
Why do we need to use iterator on ArrayList in Java? [duplicate]
As you have stated iterator is used when you want to remove stuff whilst you iterate over the array contents. If you don’t use an iterator but simply have a for loop and inside it use the remove method you will get exceptions because the contents of the array changes while you iterate through. e.g: … Read more
Vector vs Collections.synchronizedList(ArrayList)
Synchronized collections are a waste of time and dangerous. A trivial example why they are bad is to consider two threads running a loop at the same time on the same collection: int i = 0; while (i < list.size()) { if (testSomeCondition(list.get())) { list.remove(i); else i++; } Our list could be synchronized (e.g. a … Read more