• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 22821
132
Eclipse IDE Spring 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.
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic