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...
loading...
Related posts:
- 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... - Brainteaser: Overridable methods
Consider the following case of inheritance: public class Parent { public Parent() { getValue(); } public void getValue() { } } public class... - 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.... - 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... - 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...
