• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections and exceptions

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic