java unit 4
java unit 4
Advantages-
1) Reusable Code -framework provides data structures and save
developers time and efforts.
Collection
Collections
names.add("Alice");
names.add("Bob");
Or
ArrayList :
Eg :
ArrayList<String> colors = new ArrayList<>();
// Adding elements
colors.add("Red");
colors.add("Green");
colors.add("Blue");
// Accessing elements
LinkedList :
Eg;
// Adding elements
animals.add("Dog");
animals.add("Cat");
animals.add("Horse");
// Accessing elements
Vector :
Eg:
// Adding elements
cities.add("Delhi");
cities.add("Mumbai");
cities.add("Chennai");
// Accessing elements
Stack :
Eg;
import java.util.Stack;
stack.push(20);
stack.push(30);
Queue Interface:
1. FIFO ordering : elemnts are inserted at the end of the queue and
remove from the front.
2. Adding and removing elmnts : you can add items to the
end(using add()) and remove them from the front (using remove()).
3. Peeking :you can look at the item at the front without removing it
(using peek() ).
4. Size and empty check : method s to check the size of queue
(size() ) and wheter it is empty (isEmpty()).
Set Interface :
HashSet :
Key Features :
LinkedhashSet:
Key Features :
SortedSet interface :
Key Characteristics
TreeSet
It is a java class that stores unique elements in a sorted order.
It uses red black tree to keep elemnts sorted.
Useful for situations wher you need a sorted collection of unique
elements.
Charecteristics
Map Interface
Charecteristics ;
I. Key Value pairs : maps store data as pairs of keys and values.
II. Unique Keys: each key must be in the map.
III. Efficient retrieval : maps allow quick access to values using their
keys.
IV. No ordering : Maps not keep their elements in any specific order.