This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections and exceptions

 
p hasini
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic