Skip to content

Instantly share code, notes, and snippets.

View agiri23's full-sized avatar

Abhishek Giri agiri23

  • DataPebbles
  • Leiden
View GitHub Profile
@agiri23
agiri23 / gist:881770456208b2e831eeb22386e43931
Created December 16, 2021 08:55
switch case with pattern matching
public static void onShow(Country country) {
switch (country) {
case NETHERLANDS, POLAND, GERMANY -> System.out.println("European Country");
case INDIA, BHUTAN, NEPAL -> System.out.println("Asian Country");
default -> System.out.println("It's from Wakanda");
}
}
@agiri23
agiri23 / gist:4f073f0bf266c8d276d448c371109c38
Created December 16, 2021 20:35
pattern matching switch case with yield
public static void onShowWithYield(Country country) {
String output = switch (country) {
case NETHERLANDS, POLAND, GERMANY -> {
System.out.println("European Country" + country);
yield "Country belongs to Europe Continent";
}
case INDIA, BHUTAN, NEPAL -> {
System.out.println("Asian Country" + country);
yield "Country belongs to Asian Continent";
}
@agiri23
agiri23 / gist:4662096021f52d5a6e73aaf8db7dfc3f
Created December 16, 2021 20:36
pattern matching switch case with return value
public static void onShowWithReturnValue(Country country) {
String output = switch (country) {
case NETHERLANDS, POLAND, GERMANY -> "European Country";
case INDIA, BHUTAN, NEPAL -> "Asian Country";
default -> "It's from Wakanda";
};
System.out.println(output);
}
record Country(String name,String capital){}
@agiri23
agiri23 / gist:441e0815b7f9fd2384c665b760a473b2
Last active December 23, 2021 20:29
Sample record class app
public class SampleRecordClass {
record Country(String name,String capital){}
public static void main(String[] args) {
Country firstCountry = new Country("Netherlands","Amsterdam");
Country secondCountry = new Country("Germany","Berlin");
System.out.println("First country object is " + firstCountry);
System.out.println("Second country object is " + secondCountry);
System.out.println("Check if both objects equal" + firstCountry.equals(secondCountry));
// Copying object using attribute method of existing records object
Country countryClone = new Country(firstCountry.name,firstCountry.capital);
@agiri23
agiri23 / gist:bc62a2af50fc11feca83077efc249de8
Last active December 16, 2021 21:26
pattern matching instanceOf
private static void patternMatchingInstanceOf() {
Object o = new CountryData("Netherlands","Amsterdam","Europe");
if (o instanceof CountryData countryData) {
System.out.println("This capital of Netherlands is " + countryData.capital());
}
}
@agiri23
agiri23 / gist:178dc649f408cffe0378cabaf3e6083d
Last active December 16, 2021 21:25
pattern matching instanceOf scope
private static void patternMatchingInstanceOfScope() {
Object o = new CountryData("Netherlands","Amsterdam","Europe");
if (o instanceof CountryData countryData && countryData.continent().equals("Europe")) {
System.out.println("This continent of Netherlands is " + countryData.continent());
}
}
@agiri23
agiri23 / gist:f4150958a7cceb883b8b33eba0985920
Last active December 23, 2021 20:18
compact number format long
NumberFormat numberFormatLong = NumberFormat
.getCompactNumberInstance(Locale.forLanguageTag("NL"), NumberFormat.Style.LONG);
System.out.println(numberFormatLong.format(2000));
System.out.println(numberFormatLong.format(20000));
System.out.println(numberFormatLong.format(200000));
@agiri23
agiri23 / gist:0654a96ab36e1d223f2446d728e62767
Created December 16, 2021 21:08
output compact number format long format
2 duizend
20 duizend
200 duizend
@agiri23
agiri23 / gist:bf40cb993e8c009fb4f5624d1e9b4f4b
Last active December 23, 2021 20:17
compact number format short
NumberFormat numberFormatShort = NumberFormat
.getCompactNumberInstance(Locale.forLanguageTag("NL"), NumberFormat.Style.SHORT);
System.out.println(numberFormatShort.format(2000));
System.out.println(numberFormatShort.format(20000));
System.out.println(numberFormatShort.format(200000));