Feed on
 Posts
 Comments
Java Beans dot Asia

Just a few simple tutorials …

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

GD Star Rating
loading...
Brainteaser: ArrayList VS TreeSet, 8.0 out of 10 based on 2 ratings

Related posts:

  1. Brainteaser: Broken Comparator
    Question: The following program returns result “1″, which indicates that first Integer value is greater than the second, why? import java.util.*; public class...
  2. Brainteaser: Overridable methods
    Consider the following case of inheritance: public class Parent { public Parent() { getValue(); } public void getValue() { } } public class...
  3. Brainteaser: Hidden Iterators
    … While locking can prevent iterators from throwing ConcurrentMofdificationException, You have to remember to use locking everywhere a shared collection might be iterated....
  4. Brainteaser: Broken Case of Inheritance
    Consider the following case of inheritance: public class ExtendingHashSet<E> extends HashSet<E> { private int counter = 0; public ExtendingHashSet() { } @Override public...
  5. Java Generics and Reflection
    Hi, the other day I had a situation, where in my code at run time I had to determine the super type of...