33import org .junit .jupiter .api .RepeatedTest ;
44import org .junit .jupiter .api .Test ;
55
6+ import java .lang .management .ManagementFactory ;
67import java .lang .reflect .InvocationTargetException ;
78import java .lang .reflect .Method ;
89import java .math .BigDecimal ;
910import java .math .RoundingMode ;
11+ import java .net .InetAddress ;
12+ import java .net .NetworkInterface ;
13+ import java .net .SocketException ;
14+ import java .net .UnknownHostException ;
1015import java .time .Duration ;
1116import java .time .LocalDateTime ;
1217import java .time .LocalTime ;
@@ -367,13 +372,13 @@ private static boolean t221() {
367372 System .out .println ("第一个boolean" );
368373 return true ;
369374 }
375+
370376 private static boolean t222 () {
371377 System .out .println ("第二个boolean" );
372378 return true ;
373379 }
374380
375381
376-
377382 @ Test
378383 void t23 () {
379384
@@ -384,11 +389,11 @@ void t23() {
384389 list .add ("4" );
385390
386391 List <String > collect = list .stream ().map (item -> {
387- if (item .equals ("2" )) {
388- return null ;
389- }
390- return item + "1" ;
391- })
392+ if (item .equals ("2" )) {
393+ return null ;
394+ }
395+ return item + "1" ;
396+ })
392397 .filter (Objects ::nonNull )
393398 .collect (Collectors .toList ());
394399
@@ -397,7 +402,6 @@ void t23() {
397402 }
398403
399404
400-
401405 @ Test
402406 void t24 () throws InterruptedException {
403407
@@ -410,7 +414,6 @@ void t24() throws InterruptedException {
410414 });
411415
412416
413-
414417 TimeUnit .SECONDS .sleep (5 );
415418 }
416419
@@ -424,8 +427,44 @@ void t25() {
424427 }
425428
426429 @ Test
427- void t26 () {
430+ void t26 () throws UnknownHostException , SocketException {
428431
432+ // 获取本机IP
433+ InetAddress ip = InetAddress .getLocalHost ();
434+
435+ System .out .println (ip );
436+
437+ // 获取网卡
438+ NetworkInterface networkInterface = NetworkInterface .getByInetAddress (ip );
439+
440+ System .out .println (networkInterface );
441+
442+ if (networkInterface == null ) {
443+ return ;
444+ }
445+
446+ // 获取mac地址
447+ byte [] mac = networkInterface .getHardwareAddress ();
448+
449+ System .out .println (mac );
450+
451+ if (mac == null ) {
452+ return ;
453+ }
454+ // mac地址处理
455+ long id = (255L & (long ) mac [mac .length - 2 ] | 65280L & (long ) mac [mac .length - 1 ] << 8 ) >> 6 ;
456+
457+ System .out .println (id );
458+
459+ }
460+
461+ @ Test
462+ void t27 () {
463+ // 获取进程id
464+ String name = ManagementFactory .getRuntimeMXBean ().getName ();
465+ System .out .println (name ); // 格式 12345@localhost
466+ String pid = name .split ("@" )[0 ];
467+ System .out .println (pid );
429468 }
430469
431470}
0 commit comments