What are the two ways to iterate the elements of a collection?
https://www.javatpoint.com/how-to-iterate-list-in-java
What is the difference between ArrayList and LinkedList classes in collection framework?
https://www.javatpoint.com/difference-between-arraylist-and-linkedlist
What is the difference between ArrayList and Vector classes in collection framework?
https://www.javatpoint.com/difference-between-arraylist-and-vector
What is the difference between HashSet and HashMap classes in collection framework?
https://www.javatpoint.com/difference-between-hashset-and-hashmap
What is the difference between HashMap and Hashtable class?
https://www.javatpoint.com/difference-between-hashmap-and-hashtable
What is the difference between Iterator and Enumeration interface in collection framework?
https://www.tutorialspoint.com/difference-between-iterator-and-enumeration-in-java
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 this.name.compareTo(other.name); // Sorting by name
Collections.sort(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 Integer.compare(p1.age, p2.age); // Sorting by age
};
Collections.sort(listOfPersons, byAge);
```
3. **For arrays**:
- Use `Arrays.sort()`.
```java
Arrays.sort(arrayOfPersons);
```
https://www.javatpoint.com/difference-between-comparable-and-comparator
What does the hashcode() method?
https://www.javatpoint.com/java-integer-hashcode-method
What is the difference between Java collection and Java collections?
https://www.geeksforgeeks.org/collection-vs-collections-in-java-with-example/