Math
是一个帮助我们用于进行数学计算的工具类
私有化构造方法,所有的方法都是静态的
方法名
说明
public static int abs(int a)
返回参数绝对值
public static double ceil(double a)
向上取整
public static double floor(double a)
向下取整
public static int round(float a)
四舍五入
public static int max(int a,int b)
获取两个int值中的较大值
public static int min(int a,int b)
获取两个int值中的较小值
public static double pow (double a,double b)
返回a的b次幂
public static double random()
返回一个[0.0,1.0)的double随机值
**注意: **
int范围:-2147483648~2147483647,-2147483648没有相反数
System 提供了一些与系统相关的方法
方法名
说明
public static void exit(int status)
终止当前运行的Java虚拟机
public static long currentTimeMillis()
返回当前系统的时间毫秒值形式(1970.1.1 8:00)
public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数)
数组拷贝
**注意: **
如果数据源数组和目的地数组都是基本数据类型,那么两者类型必须一致
拷贝时需考虑数组长度
如果数据源数组和目的地数组都是引用数据类型,那么子类类型可以赋值给父类类型
Runtime 表示当前虚拟机的运行环境
方法名
说明
public static Runtime getRuntime()
当前系统的运行环境对象
public void exit(int status)
停止虚拟机
public int availableProcessors()
获得CPU的线程数
public long maxMemory()
JVM能从系统中获取总内存大小(单位byte)
public long totalMemory()
JVM已经从系统中获取总内存大小(单位byte)
public long freeMemory()
JVM剩余内存大小(单位byte)
public Process exec(String command)
运行cmd命令
Object Object是Java中的顶级父类。所有的类都直接或间接继承与Object类
Object类中的方法可以被所有子类访问
方法名
说明
public String toString()
返回对象的字符串表示形式(地址值)
public boolean equals(Object obj)
比较两个对象是否相等
protected Object clone(int a)
对象克隆
**对象克隆: **把A对象的属性值完全拷贝给B对象,也叫对象拷贝,对象复制
浅克隆:不管对象内部属性是基本数据类型还是引用数据类型,都完全拷贝过来
深克隆:
基本数据类型拷贝过来
字符串复用
引用数据类型会重新创建新的
Objects Objects是一个工具类,提供了一些方法去完成一些功能
方法名
说明
public static boolean equals(Object a, Object b)
先做非空判断,然后比较两个对象是否相等
public static boolean isNull(Object obj)
判断对象是否为null,为null则返回true,反之
public static boolean nonNull(Object obj)
判断对象是否为null,和isNull的结果相反
BigInteger
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 package com.itheima.a06bigintegerdemo;import java.math.BigInteger;public class BigIntegerDemo1 { public static void main (String[] args) { BigInteger bd4 = new BigInteger ("123" , 2 ); System.out.println(bd4); BigInteger bd5 = BigInteger.valueOf(16 ); BigInteger bd6 = BigInteger.valueOf(16 ); System.out.println(bd5 == bd6); BigInteger bd7 = BigInteger.valueOf(17 ); BigInteger bd8 = BigInteger.valueOf(17 ); System.out.println(bd7 == bd8); BigInteger bd9 = BigInteger.valueOf(1 ); BigInteger bd10 = BigInteger.valueOf(2 ); BigInteger result=bd9.add(bd10); System.out.println(result); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 package com.itheima.a06bigintegerdemo;import java.math.BigInteger;public class BigIntegerDemo2 { public static void main (String[] args) { BigInteger bd1 = BigInteger.valueOf(10 ); BigInteger bd2 = BigInteger.valueOf(5 ); BigInteger bd3 = bd1.add(bd2); System.out.println(bd3); BigInteger[] arr = bd1.divideAndRemainder(bd2); System.out.println(arr[0 ]); System.out.println(arr[1 ]); boolean result = bd1.equals(bd2); System.out.println(result); BigInteger bd4 = bd1.pow(2 ); System.out.println(bd4); BigInteger bd5 = bd1.max(bd2); BigInteger bd6 = BigInteger.valueOf(200 ); double v = bd6.doubleValue(); System.out.println(v); } }
底层存储方式
存储上限
BigDecimal
构造方法
说明
public BigDecimal(double val)
不推荐使用:不精确
public BigDecimal(String val)
精确
public static BigDecimal valueOf(double val)
通过静态方法获取对象
小数位数: .scale()
去掉末尾0: stripTrailingZeros()
比较:
compareTo()
:比较大小
equals()
:比较大小和 scale()
存储方式
正则表达式 作用:
校验字符串是否满足规则
在一段文本中查找满足要求的内容
表达式
总结:
爬虫 本地爬虫 核心代码:
1 2 3 4 5 6 Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); while (m.find()) { String s = m.group(); System.out.println(s); }
Pattern
类:定义正则表达式
Matchaer
类:文本匹配器,按照正则表达式的规则去读取字符串,从头开始读取
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 package com.itheima.a08regexdemo;import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegexDemo6 { public static void main (String[] args) { String str = "Java自从95年问世以来,经历了很多版本,目前企业中用的最多的是Java8和Java11," + "因为这两个是长期支持版本,下一个长期支持版本是Java17,相信在未来不久Java17也会逐渐登上历史舞台" ; Pattern p = Pattern.compile("Java\\d{0,2}" ); Matcher m = p.matcher(str); while (m.find()) { String s = m.group(); System.out.println(s); } } private static void method1 (String str) { Pattern p = Pattern.compile("Java\\d{0,2}" ); Matcher m = p.matcher(str); boolean b = m.find(); String s1 = m.group(); System.out.println(s1); b = m.find(); String s2 = m.group(); System.out.println(s2); } }
网络爬虫 例子:爬取身份证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class RegexDemo7 { public static void main (String[] args) throws IOException { URL url = new URL ("https://m.sengzan.com/jiaoyu/29104.html?ivk sa=1025883i" ); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader (new InputStreamReader (conn.getInputStream())); String line; String regex = "[1-9]\\d{17}" ; Pattern pattern = Pattern.compile(regex); while ((line = br.readLine()) != null ) { Matcher matcher = pattern.matcher(line); while (matcher.find()) { System.out.println(matcher.group()); } } br.close(); } }
条件爬取 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class RegexDemo9 { public static void main (String[] args) { String s = "Java自从95年问世以来,经历了很多版本,目前企业中用的最多的是Java8和Java11," + "因为这两个是长期支持版本,下一个长期支持版本是Java17,相信在未来不久Java17也会逐渐登上历史舞台" ; String regex1 = "((?i)Java)(?=8|11|17)" ; String regex2 = "((?i)Java)(8|11|17)" ; String regex3 = "((?i)Java)(?:8|11|17)" ; String regex4 = "((?i)Java)(?!8|11|17)" ; Pattern p = Pattern.compile(regex4); Matcher m = p.matcher(s); while (m.find()) { System.out.println(m.group()); } } }
贪婪爬取、非贪婪爬取 1 2 3 4 5 6 7 8 9 10 只写+和表示贪婪匹配,如果在+和后面加问号表示非贪婪爬取 +? 非贪婪匹配 *? 非贪婪匹配 贪婪爬取:在爬取数据的时候尽可能的多获取数据 非贪婪爬取:在爬取数据的时候尽可能的少获取数据 举例: 如果获取数据:ab+ 贪婪爬取获取结果:abbbbbbbbbbbb 非贪婪爬取获取结果:ab
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class RegexDemo10 { public static void main (String[] args) { String s = "Java自从95年问世以来,abbbbbbbbbbbbaaaaaaaaaaaaaaaaaa" + "经历了很多版木,目前企业中用的最多的是]ava8和]ava11,因为这两个是长期支持版木。" + "下一个长期支持版本是Java17,相信在未来不久Java17也会逐渐登上历史舞台" ; String regex = "ab+" ; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(s); while (m.find()) { System.out.println(m.group()); } } }
字符串应用
分组
捕获分组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 String regex1 = "(.).+\\1" ;System.out.println("a123a" .matches(regex1)); System.out.println("b456b" .matches(regex1)); System.out.println("17891" .matches(regex1)); System.out.println("&abc&" .matches(regex1)); System.out.println("a123b" .matches(regex1)); System.out.println("--------------------------" ); String regex2 = "(.+).+\\1" ;System.out.println("abc123abc" .matches(regex2)); System.out.println("b456b" .matches(regex2)); System.out.println("123789123" .matches(regex2)); System.out.println("&!@abc&!@" .matches(regex2)); System.out.println("abc123abd" .matches(regex2)); System.out.println("---------------------" ); String regex3 = "((.)\\2*).+\\1" ;System.out.println("aaa123aaa" .matches(regex3)); System.out.println("bbb456bbb" .matches(regex3)); System.out.println("111789111" .matches(regex3)); System.out.println("&&abc&&" .matches(regex3)); System.out.println("aaa123aab" .matches(regex3));
非捕获分组 1 2 3 4 5 6 7 8 9 10 11 12 String regex2 = "[1-9]\\d{16}(\\d Xx)\\1" ;System.out.println("41080119930228457x" .matches(regex2));
Date(JDK7)
tips: 由于中国处于东八区(GMT+08:00)是比世界协调时间/格林尼治时间(GMT)快8小时的时区,当格林尼治标准时间为0:00时,东八区的标准时间为08:00。
1 2 3 4 5 6 7 8 9 10 import java.util.Date;public class Demo01Date { public static void main (String[] args) { System.out.println(new Date ()); System.out.println(new Date (0L )); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package com.itheima.a01jdk7datedemo;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class A03_SimpleDateFormatDemo1 { public static void main (String[] args) throws ParseException { String str = "2023-11-11 11:11:11" ; SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" ); Date date = sdf.parse(str); System.out.println(date.getTime()); } private static void method1 () { SimpleDateFormat sdf1 = new SimpleDateFormat (); Date d1 = new Date (0L ); String str1 = sdf1.format(d1); System.out.println(str1); SimpleDateFormat sdf2 = new SimpleDateFormat ("yyyy年MM月dd日HH:mm:ss" ); String str2 = sdf2.format(d1); System.out.println(str2); } }
Calendar(JDk7)
方法名
说明
public int get(int field)
获取某个字段的值。field参数表示获取哪个字段的值, 可以使用Calender中定义的常量来表示: Calendar.YEAR : 年 Calendar.MONTH :月 Calendar.DAY_OF_MONTH:月中的日期 Calendar.HOUR:小时 Calendar.MINUTE:分钟 Calendar.SECOND:秒 Calendar.DAY_OF_WEEK:星期
JDK8 时间相关类
ZoneId
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Set<String> zoneIds = ZoneId.getAvailableZoneIds(); System.out.println(zoneIds.size()); System.out.println(zoneIds); ZoneId zoneId = ZoneId.systemDefault();System.out.println(zoneId); ZoneId zoneId1 = ZoneId.of("Asia/Pontianak" );System.out.println(zoneId1);
Instant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 Instant now = Instant.now();System.out.println(now); Instant instant1 = Instant.ofEpochMilli(0L );System.out.println(instant1); Instant instant2 = Instant.ofEpochSecond(1L );System.out.println(instant2); Instant instant3 = Instant.ofEpochSecond(1L , 1000000000L );System.out.println(instant3); ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai" ));System.out.println(time); Instant instant4=Instant.ofEpochMilli(0L ); Instant instant5 = Instant.ofEpochMilli(1000L );boolean result1=instant4.isBefore(instant5);System.out.println(result1); boolean result2 = instant4.isAfter(instant5);System.out.println(result2); Instant instant6 = Instant.ofEpochMilli(3000L );System.out.println(instant6); Instant instant7 = instant6.minusSeconds(1 );System.out.println(instant7);
ZoneDateTime
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ZonedDateTime now = ZonedDateTime.now();System.out.println(now); ZonedDateTime time1 = ZonedDateTime.of(2023 , 10 , 1 , 11 , 12 , 12 , 0 , ZoneId.of("Asia/Shanghai" )); System.out.println(time1); Instant instant = Instant.ofEpochMilli(0L );ZoneId zoneId = ZoneId.of("Asia/Shanghai" );ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);System.out.println(time2); ZonedDateTime time3 = time2.withYear(2000 );System.out.println(time3); ZonedDateTime time4 = time3.minusYears(1 );System.out.println(time4); ZonedDateTime time5 = time4.plusYears(1 );System.out.println(time5);
1 2 3 4 5 6 7 8 9 10 11 ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai" ));DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm;ss EE a" ); System.out.println(dtf1.format(time));
LocalDate LocalTime LocalDateTime
**LocalDate: **
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 LocalDate nowDate = LocalDate.now();LocalDate ldDate = LocalDate.of(2023 , 1 , 1 );System.out.println("指定日期:" + ldDate); System.out.println("=============================" ); int year = ldDate.getYear();System.out.println("year: " + year); Month m = ldDate.getMonth();System.out.println(m); System.out.println(m.getValue()); int month = ldDate.getMonthValue();System.out.println("month: " + month); int day = ldDate.getDayOfMonth();System.out.println("day:" + day); int dayofYear = ldDate.getDayOfYear();System.out.println("dayOfYear:" + dayofYear); DayOfWeek dayOfWeek = ldDate.getDayOfWeek();System.out.println(dayOfWeek); System.out.println(dayOfWeek.getValue()); System.out.println(ldDate.isBefore(ldDate)); System.out.println(ldDate.isAfter(ldDate)); LocalDate withLocalDate = ldDate.withYear(2000 );System.out.println(withLocalDate); LocalDate minusLocalDate = ldDate.minusYears(1 );System.out.println(minusLocalDate); LocalDate plusLocalDate = ldDate.plusDays(1 );System.out.println(plusLocalDate); LocalDate birDate = LocalDate.of(2000 , 1 , 1 );LocalDate nowDate1 = LocalDate.now();MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());MonthDay nowMd = MonthDay.from(nowDate1);System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));
**LocalTime: **
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 LocalTime nowTime = LocalTime.now();System.out.println("今天的时间:" + nowTime); int hour = nowTime.getHour();System.out.println("hour: " + hour); int minute = nowTime.getMinute();System.out.println("minute: " + minute); int second = nowTime.getSecond();System.out.println("second:" + second); int nano = nowTime.getNano();System.out.println("nano:" + nano); System.out.println("------------------------------------" ); System.out.println(LocalTime.of(8 , 20 )); System.out.println(LocalTime.of(8 , 20 , 30 )); System.out.println(LocalTime.of(8 , 20 , 30 , 150 )); LocalTime mTime = LocalTime.of(8 , 20 , 30 , 150 );System.out.println(nowTime.isBefore(mTime)); System.out.println(nowTime.isAfter(mTime)); System.out.println(nowTime.withHour(10 )); System.out.println(nowTime.plusHours(10 ));
**LocalDateTime: **
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 LocalDateTime nowDateTime = LocalDateTime.now();System.out.println("今天是:" + nowDateTime); System.out.println(nowDateTime.getYear()); System.out.println(nowDateTime.getMonthValue()); System.out.println(nowDateTime.getDayOfMonth()); System.out.println(nowDateTime.getHour()); System.out.println(nowDateTime.getMinute()); System.out.println(nowDateTime.getSecond()); System.out.println(nowDateTime.getNano()); System.out.println("dayofYear:" + nowDateTime.getDayOfYear()); System.out.println(nowDateTime.getDayOfWeek()); System.out.println(nowDateTime.getDayOfWeek().getValue()); System.out.println(nowDateTime.getMonth()); System.out.println(nowDateTime.getMonth().getValue()); LocalDate ld = nowDateTime.toLocalDate();System.out.println(ld); LocalTime lt = nowDateTime.toLocalTime();System.out.println(lt.getHour()); System.out.println(lt.getMinute()); System.out.println(lt.getSecond());
Duration Period ChronoUnit Duration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 LocalDateTime today = LocalDateTime.now();System.out.println(today); LocalDateTime birthDate = LocalDateTime.of(2000 , 1 , 1 , 0 , 0 , 0 );System.out.println(birthDate); Duration duration = Duration.between(birthDate, today);System.out.println("相差的时间间隔对象:" + duration); System.out.println("============================================" ); System.out.println(duration.toDays()); System.out.println(duration.toHours()); System.out.println(duration.toMinutes()); System.out.println(duration.toMillis()); System.out.println(duration.toNanos());
Period
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 LocalDate today = LocalDate.now();System.out.println(today); LocalDate birthDate = LocalDate.of(2000 , 1 , 1 );System.out.println(birthDate); Period period = Period.between(birthDate, today);System.out.println("相差的时间间隔对象:" + period); System.out.println(period.getYears()); System.out.println(period.getMonths()); System.out.println(period.getDays()); System.out.println(period.toTotalMonths());
ChronoUnit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 LocalDateTime today = LocalDateTime.now();System.out.println(today); LocalDateTime birthDate = LocalDateTime.of(2000 , 1 , 1 ,0 , 0 , 0 );System.out.println(birthDate); System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today)); System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today)); System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today)); System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today)); System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today)); System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today)); System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today)); System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today)); System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today)); System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today)); System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today)); System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today)); System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today)); System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today)); System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));
包装类 包装类:基本数据类型对应的引用类型(对象)
基本类型
对应的包装类(位于java.lang包中)
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
Integer 构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Integer i1 = new Integer (100 );System.out.println(i1); Integer i2 = new Integer ("100" );System.out.println(i2); System.out.println("--------" ); Integer i3 = Integer.valueOf(100 );System.out.println(i3); Integer i4 = Integer.valueOf("100" );System.out.println(i4);
方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 String str1 = Integer.toBinaryString(100 );System.out.println(str1); String str2 = Integer.toOctalString(100 );System.out.println(str2); String str3 = Integer.toHexString(100 );System.out.println(str3); int i = Integer.parseInt("123" );System.out.println(i); System.out.println(i + 1 ); String str = "true" ;boolean b = Boolean.parseBoolean(str);System.out.println(b);
装箱和拆箱
装箱 :从基本类型转换为对应的包装类对象
拆箱 :从包装类对象转换为对应的基本类型
1 2 3 Integer i = new Integer (4 );Integer iii = Integer.valueOf(4 );int num = i.intValue();
**自动装箱 自动拆箱: **JDK5开始,装箱拆箱自动完成
1 2 3 Integer i = 4 ;i = i + 5 ;
Arrays 操作数组的工具类
Lambda 函数式编程
格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 public class Main { public static void main (String[] args) { method(new Swim () { @Override public void swimming () { System.out.println("正在游泳~~~" ); } }); method( () -> { System.out.println("正在游泳~~~" ); } ); } public static void method (Swim s) { s.swimming(); } } @FunctionalInterface interface Swim { public abstract void swimming () ; }
省略写法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 import java.util.Arrays;import java.util.Comparator;public class Main { public static void main (String[] args) { Integer[] arr = {2 , 3 , 1 , 5 , 6 , 7 , 8 , 4 , 9 }; Arrays.sort(arr, new Comparator <Integer>() { @Override public int compare (Integer o1, Integer o2) { return o2 - o1; } }); Arrays.sort(arr, (Integer o1, Integer o2) -> { return o2 - o1; } ); Arrays.sort(arr, (o1, o2) -> o2 - o1); } }