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

More from Alexander Zagniotov:

  1. Brainteaser: Hidden Iterators
    … While locking can prevent iterators from throwing ConcurrentMofdificationException, You have to remember to use locking everywhere a shared collection...
  2. Bitwise Operation In Hibernate 3
    Hi all… i encountered a small problem in doing bitwise operations with hibernate. Until now, HIbernate 2 HQL parser has...
  3. Java Generics and Reflection
    Hi, the other day I had a situation, where in my code at run time I had to determine the...