MOOC Part 10
Since I last posted I completed part 10 of the MOOC course and 3/4’s through part 11.
I’ve found them very useful for learning new topics I haven’t touched before (streams) and for reinforcing what I learned in college.
Below are the topics I covered in part 10 I’ve added a link to the relevant MOOC part at each section.
Handling Collections using streams
In java method of stream() which can be used to convert a collection to a stream. With streams you can define a sequence of events to be executed for each value in a collection.
They aren’t efficient as using a normal loop, but they scale better as they can easily be used for parallel operations. They are also can reduce down the amount of lines of code written which can make it easier to read.
Even if you don’t want or need to use them someone else with use them in their code so understanding them might be very helpful!
Terminal and Intermediate Operations
Operations that can be applied to streams are split up into terminal and intermediate.
Terminal operations are ones which are used to return data or values and intermediate return a new stream.
All the methods that can used with stream can be found in the Java Docs.
Examples of Intermediate operations:
- .filter() returns a new stream of values which pass the requirements you are filtering by.
- .mapToInt() returns a new stream which is an IntStream.
Example of Terminal operations:
- .count() which returns the number of values in the stream.
- .max() which returns the max value in a stream for the given comparison.
Below is an example of a Java method using streams. It takes in an array of numbers which are then filtered out if they are divisible by 2,3,5 the rest of the numbers are then added to a new collection another array list and returned.
public static ArrayList<Integer> divisible(ArrayList<Integer> numbers) {
ArrayList<Integer> divisibleNumbers = numbers.stream()
.filter(number -> number % 2 == 0 || number % 3 == 0 || number % 5 == 0)
.collect(Collectors.toCollection(ArrayList::new));
return divisibleNumbers;
}
Interface comparable
The comparable interface is what defines the compareTo method which can be used to compare objects. If you implement Comparable in one of your classes you can then override the compareTo method to compare the values you want to compare. In the example below I compare maxSpeed this can be used to sort cars in a list by their maxSpeed.
public class Car implements Comparable<Car> {
.
.
.
.
@Override
public int compareTo(Car car) {
if (this.maxSpeed == car.getMaxSpeed()) {
return 0;
} else if (this.maxSpeed > car.getMaxSpeed()) {
return 1;
} else {
return -1;
}
}
}
StringBuilder
StringBuilder is only really useful if space and efficiency are crucial. In the book I’m reading CleanCode they recommended against using them unless you must as they can cause unclear code.
Regular Expressions
Regular Expressions are very useful for checking if a string is in the form you want. They can be used to match chars in very a very condensed way.
In the code below a regular expression is used to check if a student number is valid. Checks that it starts with X01 and then have 7{7} numbers after which are between 0 and 9 inclusive[0-9].
System.out.print("Student Number: ");
String number = scanner.nextLine();
if (number.matches("X01[0-9]{7}")) {
System.out.println("Valid format.");
} else {
System.out.println("Invalid format.");
}
Enumerated type
Enumerated types are useful for when you have a case where you know the possible values of a variable in advance. Like in the case of directions in a game.
After implementing the code below you would be able to call Direction.NORTH which returns 1.
The advantage of this is you don’t end up with magic numbers hanging around in your code instead you see what 1 means in context.
public enum Direction {
NORTH(1),
SOUTH(-1),
EAST(-1),
WEST(1);
private int code;
private Direction(int direction) {
this.direction = direction;
}
public int getDirection() {
return this.direction;
}
}
Final Project
Right now I’m in the struggle of trying to come up with a good project to do for my final year college project wish me luck!
Have a nice day good luck, happy coding!