Java Concepts Assignment
1. Array vs Collection:
- Array is a fixed-size data structure that stores elements of the same type.
- Collection is part of the Java Collections Framework and provides dynamic storage.
- Arrays can store primitives; Collections store only objects.
- Collections provide utility methods; Arrays do not.
2. ArrayList vs LinkedList:
- ArrayList uses a dynamic array; LinkedList uses a doubly linked list.
- ArrayList offers fast random access; LinkedList is better for frequent insertions/deletions.
- ArrayList shifting makes insertion/removal slower than LinkedList.
3. List vs Set:
- List allows duplicates and maintains insertion order.
- Set does not allow duplicates and may not maintain order (e.g., HashSet).
- Common List: ArrayList, LinkedList; Set: HashSet, TreeSet.
4. Collection vs Collections:
- Collection is an interface (root of List, Set, etc.).
- Collections is a utility class with static methods like sort(), reverse().
5. Comparable vs Comparator:
- Comparable defines natural ordering within the class via compareTo().
- Comparator defines custom sorting logic outside the class via compare().
- Comparable modifies class; Comparator adds flexibility for different sort logics.