This lesson is describing creating and using classes which allows to pass a type as a parameter. In fact, maps are not a data structure, but for this lesson we will assume that they are.
Lesson 2
This lesson is describing creating and using classes which allows to pass a type as a parameter. In fact, maps are not a data structure, but for this lesson we will assume that they are.
Consider a class that contains String or Integer object.
class StringWrapper {
String value;
}
class IntegerWrapper {
Integer value;
}
Those classes are very similar, so we would like to create something more universal by using a diamond:
class Wrapper<T> {
T value;
}
Wrapper<String> stringWrapper;
Wrapper<Integer> integerWrapper;
Create a class which contains a list of a specific type. Its size will be passed as the argument in a constructor. Don't use java's list, create your own.
Array | List | Map |
---|---|---|
The fastest | Slower (depends on implementation) | |
You have to provide a size | Size is optional | |
Size is fixed | Size is changing dynamic | |
Index of an object is an integer | Index is of any type | |
No support for a diamond | One parameter type required | Two parameter types required (index, value) |
// Creating a list of a specific type
List<Integer> list = new ArrayList<>();
// Adding an element to the list
list.add(5);
// Getting an element from the list (index as an argument)
list.get(0);
// Cheking if the list contains an element given as an argument
list.contains(5);
// Overriding the element with an index 0 with a new value 10
list.set(0, 10);
// Deleting an element with an index 0
list.remove(0);
// Deleting all elements from the list
list.clear();
Task 1
Create a program reading numbers via Scanner class from standard input. If the program receives 0, it will display:
// Creating a mapping from String to String
Map<String, String> map = new HashMap<>();
// Putting a value “Warsaw” for a key “Poland”
map.put("Poland", "Warsaw");
// Getting a value for a key "Poland"
map.get("Poland");
// Checking if a key “Japan” exists
map.containsKey("Japan");
// Getting a set of all keys
map.keySet();
// Deleting a key (so its value)
map.remove("Poland");
// Deleting all keys (and values)
map.clear();
There is a file with content:
Poland 2
Spain 3
Poland 1
Germany 10
Italy 123
China 40
China 78
Your task is to aggregate those informations. You need to display total value according to a country.
Task 3
Write a program generating a structure like: university has many groups, there are many students in any group.
University, groups and students are classess with custom fields, which data should be randomized during constructing the object.
At the very end you should write whole content to .csv file.