| Author |
Collections and exceptions
|
p hasini
Ranch Hand
Joined: Oct 24, 2009
Posts: 92
|
|
Please clarify.
1. In what situations we write try/finally without a catch.
2. How can we avoid duplicates in a list
3. How can we make an ArrayLsit synchronized explicitly.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
p hasini wrote:1. In what situations we write try/finally without a catch.
if there in no need to catch exception and need to de-allocate resource like db connection though there is an exception
p hasini wrote:2. How can we avoid duplicates in a list
you cant . if you want to avoid duplicates *use Set*
p hasini wrote:3. How can we make an ArrayLsit synchronized explicitly.
java.util.Collections class has a method for this. but i suggest you to prefer *java.util.concurrent.CopyOnWriteArrayList*
hth
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Seetharaman Venkatasamy wrote:
p hasini wrote:2. How can we avoid duplicates in a list
you cant . if you want to avoid duplicates *use Set*
Well you can, but you'll need to call contains first:
But you can't subclass a List implementing class and add override the add methods to add this check since it breaks the contract for List -- it explicitly allows duplicates. A LinkedHashSet is a good alternative for a collection without duplicates that maintains insertion order.
|
 |
 |
|
|
subject: Collections and exceptions
|
|
|