Exercises 4 1
Exercises 4 1
md 2023-11-23
Description: Write a Java program that creates an ArrayList of integers, adds some elements to it,
and prints the contents of the list.
1 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.ArrayList;
2 / 22
exercises-1.md 2023-11-23
Description: Write a Java program that creates an immutable List of integers using List.of(), and
prints the contents of the list.
3 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.List;
4 / 22
exercises-1.md 2023-11-23
Description: Create a HashSet of strings. Add several duplicate values to the set and observe how
duplicates are handled. Print the final set.
5 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.HashSet;
// Adding duplicates
fruits.add("apple");
fruits.add("grape");
6 / 22
exercises-1.md 2023-11-23
Description: Write a program that creates an immutable HashMap where the keys are names
(strings) and the values are ages (integers) using Map.of(). Add several key-value pairs and print the
contents of the map.
7 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.Map;
8 / 22
exercises-1.md 2023-11-23
Description: Create an ArrayList of Integer objects and demonstrate autoboxing and unboxing by
first adding integers and then adding all values in the list.
150
9 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.ArrayList;
import java.util.List;
int sum = 0;
for (int n : numbers) {
sum += n;
}
System.out.println(sum);
}
}
10 / 22
exercises-1.md 2023-11-23
Description: Write a function that takes an ArrayList of strings as input and prints each element in
reverse order, one per line. Each word should be reversed as well as the list.
oof
olleh
dlrow
11 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
printReversed(words);
}
12 / 22
exercises-1.md 2023-11-23
Description: Create an ArrayList of objects representing products with names and prices. Implement
a comparator to sort the list by price in ascending order.
Sorted Products:
Product[name=Monitor, price=200.0]
Product[name=Phone, price=500.0]
Product[name=Laptop, price=800.0]
13 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.ArrayList;
import java.util.List;
System.out.println("Sorted Products:");
for (Product product : products) {
System.out.println(product);
}
}
}
14 / 22
exercises-1.md 2023-11-23
Description: Create a taxBase map that represents a Swedish tax database, where each person is
associated with their unique Swedish personal number (personnummer). The taxBase map should
have personal numbers (in the format "YYYYMMDD-XXXX") as keys and Person objects as values.
Each Person object should be a record with fields pnumber (personal number as a string), name, and
income. Populate the taxBase map using Map.of with four Person objects.
1. Fetch a person using an existing Swedish personal number and display their information if
found.
2. Attempt to fetch a person using a non-existent Swedish personal number and handle the case
when the person is not found using a null check.
15 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.Map;
16 / 22
exercises-1.md 2023-11-23
Description: Create a LinkedHashSet of characters and add some characters to it. Demonstrate how
the order of insertion is preserved when iterating over the set.
LinkedHashSet: [A, B, C, D, E]
17 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.LinkedHashSet;
import java.util.Set;
18 / 22
exercises-1.md 2023-11-23
Exercise 9: TreeMap
Description: Create a TreeMap where the keys are integers and the values are strings. Add key-value
pairs and then print the map in ascending order of keys.
TreeMap:
1: One
2: Two
3: Three
4: Four
19 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.Map;
import java.util.TreeMap;
System.out.println("TreeMap:");
for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
20 / 22
exercises-1.md 2023-11-23
Description: Define a record Student with attributes name and roll number. Override the equals and
hashCode methods to only use the roll number when comparing and calculating a hashCode. Create
a HashSet of Student objects and demonstrate how to add, and iterate over the set. Show how the
set will refuse more than one student with a particular roll number.
Students:
Student[name='Alice', rollNumber=101]
Student[name='Bob', rollNumber=102]
Student[name='Carol', rollNumber=103]
21 / 22
exercises-1.md 2023-11-23
Solution:
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@Override
public int hashCode() {
return Objects.hash(rollNumber);
}
}
System.out.println("Students:");
for (Student student : students) {
System.out.println(student);
}
}
}
22 / 22