HashSet vs. ArrayList

When its comes to the behavior of ArrayList and HashSet they are completely different classes. ArrayList ArrayList Does not validate duplicates. get() is O(1) contains() is O(n) but you have fully control over the order of the entries. get add contains next remove(0) iterator.remove ArrayList O(1) O(1) O(n) O(1) O(1) O(1) Not thread safe and to make it thread safe you have to use Collections.synchronizedList(…) HashSet … Read more

Why do I get an UnsupportedOperationException when trying to remove an element from a List?

Quite a few problems with your code: On Arrays.asList returning a fixed-size list From the API: Arrays.asList: Returns a fixed-size list backed by the specified array. You can’t add to it; you can’t remove from it. You can’t structurally modify the List. Fix Create a LinkedList, which supports faster remove. On split taking regex From the API: String.split(String regex): Splits this string around matches of the given regular expression. … Read more

Print ArrayList

I have an ArrayList that contains Address objects. How do I print the values of this ArrayList, meaning I am printing out the contents of the Array, in this case numbers. I can only get it to print out the actual memory address of the array with this code:

Java ArrayList copy

Yes, assignment will just copy the value of l1 (which is a reference) to l2. They will both refer to the same object. Creating a shallow copy is pretty easy though: (Just as one example.)