Pattern Matching in Java: 5 Examples for Busy Developers

preview_player
Показать описание
Are you a busy developer who wants to use Pattern Matching but can not find enough time to read through all the details?
Don't worry. We've got you covered.
In this video, I'll show you five examples where you can use Pattern Matching in Java. I'll also cover why you might want to use them, where, and how.

00:00 - Intro
00:26 - Replacing long if-else constructs with switch
04:57 - Using Type patterns with instanceof
08:58 - Checking exhaustive enum values or sealed class subtypes
12:50 - Deconstructing nested records
13:51 - Separation of computation and its side effect

For more information, take a look at:

*Author: Mala Gupta

Join us:

#intelliJIDEA #intelliJ #jetbrains #java #programming
Рекомендации по теме
Комментарии
Автор

Thanks Mala, I like your teaching style.

mustafailikkan
Автор

Great Video! Easy to understand. Thank you very much.

dirkweissenbacher
Автор

Nice vídeo. Very helpful. If I can make a suggestion, please take sometime on The option selected before hit enter. It would let me check the option

lucasguaru
Автор

10:13 If you place the camera view a little higher we can see the whole shortcut line.

BrazenNL
Автор

Great stuff I learnt a lot in short time 🎉😊

perwramdemark
Автор

Which theme are you using in this video? It is nice to look at for a video imo.

Quillraven
Автор

Great Video, even for Kotlin user,
or Just "switch" to Kotlin!😀😀😀

CryptoCodeZone
Автор

If Else, how many time have you written one? 😀 Always

nasamind
Автор

Title is confusing, i thought you will discuss about pattern matching or regular expressions in java😂😂

praveen
Автор

Java is like baroque full of splendor i Preffer kotlin

cyamdirl
Автор

Guys, sorry, I see that you are trying to do a good job,
mostly going over and beyond, but iterating over an Enumeration,
or switch/case, that is not right, guys you are trying to teach
will repeat antipatterns like these. :-(
So to make it right:

@AllArgsConstructor
enum SingleUsePlastic {
BOTTLE("Booth 4: Pick up a glass bottle"),
SPOON("Pantry: Pick up a steel spoon"),
CARRY_BAG("Booth 5: Pick up a cloth bag"),
;

final String replacement;
}

String plastic) {
return plastic.replacement;
}

So the method above will be quite a duplicity itself.

And if needed any sort of semantics, enumerations
can always implement interfaces, the OOP approach could be like this:
public class Sample {

interface Arithmetic {

int apply(int num1, int num2);
}

enum Operator implements Arithmetic {
PLUS('+') {
@Override
public int apply(int n1, int n2) {
return n1 + n2;
}
},
MINUS('-') {
@Override
public int apply(int n1, int n2) {
return n1 - n2;
}
},
CROSS('*') {
@Override
public int apply(int n1, int n2) {
return n1 * n2;
}
},
DIVIDE('/') {
@Override
public int apply(int n1, int n2) {
return n1 / n2;
}
},
;

final char operator;

Operator(char operator) {
this.operator = operator;
}
}

Operator getOperator(char c) {
switch (c) {
case '+':
return PLUS;
case '-':
return MINUS;
case '*':
return CROSS;
default:
return DIVIDE;
}
}
}

pavolvarga