(Java Programming)
Question 4:
1. What are some considerations for selecting a Collection?
2. What is Garbage Collection and how can you mark an object for Garbage Collection.
Expert Answer
Java collection package contains 4 basic types, namely
- List: Which is basically a variable array. A particular order of the items is maintained and also Item can be added or removed from arbitary position.
- Set: As the name suggests, when you add items to a set, there’s no notion of how many times the item was added, and usually no notion of ordering.
- Map: Stores association between Key and Value pair. In general key cannot be repeated since keys are indexed for optimizd searching
- Queue: Veri similar to queue, but we cannot add or remove items from arbitary position.
2. In Java it is automatically done by the garbage collector(by JVM) so we don’t need to make extra efforts. To call garbage collector we need to
- nullify the reference
e.g. Object o = null; - assign a reference to another
e.g. Object o = new Object(); - By annonymous object.
e.g. new Object();