What are the two ways to iterate the elements of a collection?
[Link]
What is the difference between ArrayList and LinkedList classes in collection framework?
[Link]
What is the difference between ArrayList and Vector classes in collection framework?
[Link]
What is the difference between HashSet and HashMap classes in collection framework?
[Link]
What is the difference between HashMap and Hashtable class?
[Link]
What is the difference between Iterator and Enumeration interface in collection framework?
[Link]
How can we sort the elements of an object? What is the difference between Comparable and
Comparator interfaces?
To sort elements of an object in Java:
1. **Using `Comparable`**:
- Implement the `Comparable` interface in your class.
- Override the `compareTo` method.
```java
class Person implements Comparable<Person> {
String name;
int age;
@Override
public int compareTo(Person other) {
return [Link]([Link]); // Sorting by name
[Link](listOfPersons);
```
2. **Using `Comparator`**:
- Create a `Comparator` and override the `compare` method.
```java
Comparator<Person> byAge = new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return [Link]([Link], [Link]); // Sorting by age
};
[Link](listOfPersons, byAge);
```
3. **For arrays**:
- Use `[Link]()`.
```java
[Link](arrayOfPersons);
```
[Link]
What does the hashcode() method?
[Link]
What is the difference between Java collection and Java collections?
[Link]