Home | About the author | Resume | << Patch for Flex Builder 3 plugin to work with Eclipse 3.4 (Ganymede) | How to set SecurityManager and Java security policy programmatically >>
SMS Bundle - Mobile Marketing Solutions
SMS Bundle is an Australian-based service for sending marketing SMS and MMS

Brainteaser: ArrayList VS TreeSet

When I came across the following example I did not expect the results that the program has printed :) hehe...

Question: What does this program print? Why?
import java.util.*;

public class SetList {

public static void main(String[] args) {

Set<Integer> set =
new TreeSet<Integer>();
List<Integer> list =
new ArrayList<Integer>();

for (int i = -3; i < 3; i++) {
set.add(i);
list.add(i);
}

for (int i = 0; i < 3; i++) {
set.remove(i);
list.remove(i);
}

System.out.println(set + " " + list);
}
}
Looking forward for your answers dear readers

Resources:
Effective Java
Categories : brainteaser, java
Technorati Tags : , ,
Social Bookmarks :  Add this post to Slashdot    Add this post to Digg    Add this post to Reddit    Add this post to Delicious    Add this post to Stumble it    Add this post to Google    Add this post to Technorati    Add this post to Bloglines    Add this post to Facebook    Add this post to Furl    Add this post to Windows Live    Add this post to Yahoo!

Related Posts
Brainteaser Drools: Testing Objects
Brainteaser: Broken case of inheritance
Brainteaser: Overridable methods
How to set SecurityManager and Java security policy programmatically
Hack any Java class using reflection attack
Brainteaser: Broken comparator
Singleton pattern and problem with double checked locking
Brainteaser: Hidden iterators




If you like this post, then consider subscribing to the full feed RSS.



Re: Brainteaser: ArrayList VS TreeSet

Just a guess..

Since list maintain the order of insertion in the collection, remove method in the list class might take the index of the object to be removed.. so list.remove(0) will remove zeroth element..

Where as Tree Set stores elements in sorted order, remove(index) does not make sense.. so remove(0) might remove element whose value is 0 instead of zeroth element..

I did not try.. just guessing.. ofcourse I will try now..

Re: Brainteaser: ArrayList VS TreeSet

List.remove(index) and Set.remove(Object) that should explain it.

Re: Brainteaser: ArrayList VS TreeSet

My guess is that it will display the class names and addresses in the heap...were you looking for the values within the data structure? </smartass>

Re: Brainteaser: ArrayList VS TreeSet

I agree with both of the above comments that List.remove(index) is the method that gets called on the List type, BUT the specification of this method additionally states that as a consequence all elements with a greater index are shifted to the left. Taking this into account: the list should print -2,0,2.

Re: Brainteaser: ArrayList VS TreeSet

Late with my reply :(
My reply would be the same as Martin's
btw, the output would be:
[-2 0 -2] [-3, -2, -1]

Re: Brainteaser: ArrayList VS TreeSet

Thank you for your replies dear readers. Honestly speaking I did not get it right the first time when I came across this example... hehe...

Add a comment    Send a TrackBack