aspose file tools*
The moose likes Java in General and the fly likes Collections and exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Collections and exceptions" Watch "Collections and exceptions" New topic
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: 19762
    
  20

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.
 
jQuery in Action, 2nd edition
 
subject: Collections and exceptions