Spaces:
Running
Running
List, boxed()
Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/14A. Creating List, convert to ArrayList+++
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Creating a list with predefined initial values is made easier,
|
| 2 |
+
for example, with the static method 'of' from the List class:
|
| 3 |
+
|
| 4 |
+
List<Integer> numbers = List.of(1, 3, 2, 4, 3, 6, 4, 3, 2, 1);
|
| 5 |
+
System.out.println(numbers);
|
| 6 |
+
|
| 7 |
+
List<String> names = List.of("Kai", "Jack", "Kirsi", "Joanna");
|
| 8 |
+
names.stream().forEach(name -> System.out.println(name));
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
Ohjelma tulostaa:
|
| 12 |
+
[1, 3, 2, 4, 3, 6, 4, 3, 2, 1]
|
| 13 |
+
Kai
|
| 14 |
+
Jack
|
| 15 |
+
Kirsi
|
| 16 |
+
Joanna
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
Note that the method returns a list of type 'List' - not 'ArrayList'.
|
| 23 |
+
The difference is that 'List' is IMMUTABLE (i.e., you cannot add elements to it, for example).
|
| 24 |
+
|
| 25 |
+
If a mutable list is needed, the List-type object can be given as a parameter to the ArrayList class constructor:
|
| 26 |
+
|
| 27 |
+
List<Integer> numbers = List.of(1, 3, 2, 4, 3, 6, 4, 3, 2, 1);
|
| 28 |
+
ArrayList<Integer> mutableList = new ArrayList<>(numbers);
|
| 29 |
+
|
| 30 |
+
// this can be normally modified
|
| 31 |
+
mutableList.add(23);
|
| 32 |
+
System.out.println(mutableList);
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
// Initialization can also be conveniently done on one line
|
| 36 |
+
ArrayList<Double> measurements = new ArrayList<>(List.of(1.0, 2.0, 3.0, 2.5, 5.25));
|
| 37 |
+
measurements.add(10.75);
|
| 38 |
+
System.out.println(measurements);
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
=================
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
You can conveniently create an 'ArrayList' from an array with the 'Array' class method 'asList',
|
| 46 |
+
if it's an object type:
|
| 47 |
+
|
| 48 |
+
String[] np = {"Huey", "Dewey", "Louie"};
|
| 49 |
+
ArrayList<String> nephews = new ArrayList<>(Arrays.asList(np));
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
=================
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
However, forming an Integer type ArrayList from an int type array does not work this way.
|
| 58 |
+
This can be circumvented (though a bit clumsily) for example by using a stream.
|
| 59 |
+
|
| 60 |
+
A stream can be created from an array with the 'Arrays' class method 'stream',
|
| 61 |
+
and as we saw earlier,
|
| 62 |
+
the boxed() method converts the stream from a primitive type to an object type.
|
| 63 |
+
|
| 64 |
+
int[] luvut = {10, 20, 30, 40, 50, 60, 70};
|
| 65 |
+
int[] numbers = {10, 20, 30, 40, 50, 60, 70};
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
// ArrayList<Integer> => ArrayList of 'Integer' objects
|
| 69 |
+
// ArrayList can only hold objects (eg Integer), not primitives (eg int)
|
| 70 |
+
// .boxed() -> convert each 'int' value to 'Integer' object
|
| 71 |
+
ArrayList<Integer> numberList = Arrays.stream(numbers).
|
| 72 |
+
// after boxed() --we get--> Stream<Integer>
|
| 73 |
+
boxed().
|
| 74 |
+
collect(Collectors.toCollection(ArrayList::new));
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|