In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the common operation of JAVA language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the common operations of JAVA language.
Summation maximum and minimum average group weight removal list.stream (). MapToDouble (User::getHeight). Sum () / and list.stream (). MapToDouble (User::getHeight). Max () / / maximum list.stream (). MapToDouble (User::getHeight). Min () / / minimum list.stream (). MapToDouble (User::getHeight). Average () / / average / / List in ID grouping MapMap groupBy = appleList.stream (). Collect (Collectors.groupingBy (Apple::getId)) System.err.println ("groupBy:" + groupBy); {1 = [Apple {id=1, name=' Apple 1x, money=3.25, num=10}, Apple {id=1, name=' Apple 2mm, money=1.35, num=20}], 2 = [Apple {id=2, name=' Banana', money=2.89, num=30}], 3 = [Apple {id=3, name=' Litchi, money=9.99, num=40}]} import static java.util.Comparator.comparingLong;import static java.util.stream.Collectors.collectingAndThen;import static java.util.stream.Collectors.toCollection / / deduplication according to id List unique = appleList.stream (). Collect (toCollection (()-> new TreeSet (comparingLong (Apple::getId)), ArrayList::new)); SpringBoot- reads files under classpath / / method 1: get files or streams this.getClass (). GetResource ("/") + fileName;this.getClass (). GetResourceAsStream (failName) / / method 2: get the file File file = org.springframework.util.ResourceUtils.getFile ("classpath:test.txt"); / / method 3: get the file or stream ClassPathResource classPathResource = new ClassPathResource ("test.txt"); classPathResource .getFile (); classPathResource .getInputStream (); / / > the following method can read the file under the jar package assuming that there is a test.txt file in the resources directory, first get the current class loader, and read the file through the class loader. / / method 1InputStream io = Thread.currentThread (). GetContextClassLoader (). GetResourceAsStream ("test.txt"); / / method 2InputStream io = getClass (). GetClassLoader (). GetResourceAsStream ("test.txt");-copyright notice: this article is the original article of the CSDN blogger "Tokyo Yileng" and complies with the CC 4.0 BY-SA copyright Agreement. Please attach the original source link and this notice for reprint. Original link: the https://blog.csdn.net/weixin_38229356/article/details/80783142SpringBoot project packages the project into a jar package, using an absolute path when using ClassPathResource Directly calling the getFile () method will report FileNotFoundExceptionval resource = ClassPathResource ("my.keystore") val temp = Files.createTempFile ("my.keystore", "tmp") Files.copy (resource.inputStream, temp, StandardCopyOption.REPLACE_EXISTING)-copyright notice: this article is the original article of CSDN blogger "bannerXu" and complies with the CC 4.0BY-SA copyright Agreement. Please attach the original source link and this statement for reprint. Original link: https://blog.csdn.net/qq_31724483/article/details/89291739
Notes commonly used in bean validation (https://www.cnblogs.com/mr-yang-localhost/p/7812038.html)
Constraint @ Null annotated elements built into Bean Validation must be null @ NotNull annotated elements must not be null @ AssertTrue annotated elements must be true @ AssertFalse annotated elements must be false @ Min (value) annotated elements must be a number Its value must be greater than or equal to the specified minimum value @ Max (value) the annotated element must be a number, its value must be less than or equal to the specified maximum value @ DecimalMin (value) the annotated element must be a number, its value must be greater than or equal to the specified minimum value @ DecimalMax (value) the annotated element must be a number Its value must be less than or equal to the specified maximum value @ Size (max=, min=) the size of the annotated element must be within the specified range @ Digits (integer, fraction) the annotated element must be a number Its value must be within the acceptable range @ Past annotated element must be a past date @ Future annotated element must be a future date @ Pattern (regex=,flag=) annotated element must conform to the specified regular expression Hibernate Validator appended constraint @ NotBlank (message =) validation string non-null And the length must be greater than 0 @ Email the annotated element must be an email address @ Length (min=,max=) the size of the commented string must be within the specified range @ NotEmpty the commented string must be non-empty @ Range (min=,max=,message=) the annotated element must be within the appropriate range bigDecimal to string representation:
This paper introduces the differences of three toString methods of BigDecimal.
The BigDecimal class has three toString methods, which are toEngineeringString, toPlainString, and toString
You can see the difference between these three methods from the comments in BigDecimal:
ToEngineeringString: use the engineering counting method if necessary. Engineering notation is a method of recording numbers, which is often used in engineering calculation, which is similar to the scientific and technological method, but requires that the power of 10 must be a multiple of 3.
ToPlainString: do not use any index
ToString: use scientific counting if necessary
Engineering notation 27002.7 × 10 / 2 / 2 / 270002.7 / 10 / 27 / 10 / 2700002.7 / 10 / 27 / 10 / 2700002.7 / 10 / 27 / 10 / 27000002.7 / 10 / 2. 7 × 10 / 10 import java.math.BigDecimal;public class BigDecimalDemo {public static void main (String [] args) {BigDecimal bg = new BigDecimal ("1E11"); System.out.println (bg.toEngineeringString ()); System.out.println (bg.toPlainString ()) System.out.println (bg.toString ());}} / / output 100E+91000000000001E+11 calculates the number of days difference between two LocalDateTime types by:
Initially, using the Period.between () method, you can only calculate the number of days in the same month:
/ / can only calculate the number of days between the same month int daysNum = Period.between (O.getStartTime () .toLocalDate (), O.getEndTime () .toLocalDate ()) .getDays ()
After optimization, the toEpochDay () method is used:
Int daysNum= (int) (o.getEndTime (). ToLocalDate (). ToEpochDay ()-o.getStartTime (). ToLocalDate (). ToEpochDay ())
Date conversion:
/ / java.util.Date-- > java.time.LocalDateTimepublic void UDateToLocalDateTime () {java.util.Date date = new java.util.Date (); Instant instant = date.toInstant (); ZoneId zone = ZoneId.systemDefault (); LocalDateTime localDateTime = LocalDateTime.ofInstant (instant, zone);} / / 02. Java.util.Date-- > java.time.LocalDatepublic void UDateToLocalDate () {java.util.Date date = new java.util.Date (); Instant instant = date.toInstant (); ZoneId zone = ZoneId.systemDefault (); LocalDateTime localDateTime = LocalDateTime.ofInstant (instant, zone); LocalDate localDate = localDateTime.toLocalDate ();} / / 03. Java.util.Date-- > java.time.LocalTimepublic void UDateToLocalTime () {java.util.Date date = new java.util.Date (); Instant instant = date.toInstant (); ZoneId zone = ZoneId.systemDefault (); LocalDateTime localDateTime = LocalDateTime.ofInstant (instant, zone); LocalTime localTime = localDateTime.toLocalTime ();} / / 04. Java.time.LocalDateTime-- > java.util.Datepublic void LocalDateTimeToUdate () {LocalDateTime localDateTime = LocalDateTime.now (); ZoneId zone = ZoneId.systemDefault (); Instant instant = localDateTime.atZone (zone). ToInstant (); java.util.Date date = Date.from (instant);} / / 05. Java.time.LocalDate-- > java.util.Datepublic void LocalDateToUdate () {LocalDate localDate = LocalDate.now (); ZoneId zone = ZoneId.systemDefault (); Instant instant = localDate.atStartOfDay (). AtZone (zone). ToInstant (); java.util.Date date = Date.from (instant);} / / 06. Java.time.LocalTime-- > java.util.Datepublic void LocalTimeToUdate () {LocalTime localTime = LocalTime.now (); LocalDate localDate = LocalDate.now (); LocalDateTime localDateTime = LocalDateTime.of (localDate, localTime); ZoneId zone = ZoneId.systemDefault (); Instant instant = localDateTime.atZone (zone). ToInstant (); java.util.Date date = Date.from (instant);}
The list of objects is grouped by attribute:
/ / List is grouped in ID MapMap groupBy = appleList.stream () .collect (Collectors.groupingBy (Apple::getId))
List to Map:
/ * List-> Map * it should be noted that: * toMap if the collection object has a duplicate key, it will report an error Duplicate key. * the id of apple1,apple12 is 1. * you can set it with (K1Query K2)-> K1. If there is a duplicate key, keep key1 and discard key2 * / Map appleMap = appleList.stream () .collect (Collectors.toMap (Apple::getId, a-> a, (K1PowerK2)-> K1)
Filter:
List filterList = appleList.stream () .filter (a-> a.getName () .equals ("banana")) .equals (Collectors.toList ())
Summation:
BigDecimal totalMoney = appleList.stream () .map (Apple::getMoney) .reduce (BigDecimal.ZERO, BigDecimal::add)
Find the maximum and minimum value in the stream
Collectors.maxBy and Collectors.minBy to calculate the maximum or minimum values in the flow.
Optional maxDish = Dish.menu.stream (). Collect (Collectors.maxBy (Comparator.comparing (Dish::getCalories); maxDish.ifPresent (System.out::println); Optional minDish = Dish.menu.stream (). Collect (Collectors.minBy (Comparator.comparing (Dish::getCalories); minDish.ifPresent (System.out::println)
Deweighting
List unique = appleList.stream () .collect (collectingAndThen (toCollection (()-> new TreeSet (comparingLong (Apple::getId), ArrayList::new)
Array to List
Using the Collector collector in Stream
String [] arrays = new String [] {"a", "b", "c"}; List listStrings = Stream.of (arrays) .requests (Collectors.toList ())
Use the asList () method in the java.util.Arrays utility class (this is not new in Java8):
String [] arrays = new String [] {"a", "b", "c"}; List listStrings = Arrays.asList (arrays)
Convert List to an array
Use Stream:
String [] ss = listStrings.stream () .toArray (String []:: new)
Use the toArray () method in List
String [] sss = listStrings.toArray (new String [listStrings.size ()])
Obtain the local private network address:
/ * * Native IP list * / public static List getLocalIps () {List ips = new ArrayList (); try {Enumeration enumeration = NetworkInterface.getNetworkInterfaces (); while (enumeration.hasMoreElements ()) {NetworkInterface iface = enumeration.nextElement (); / filters out 127.0.0.1 and inactive interfaces if (iface.isLoopback () | |! iface.isUp ()) continue Enumeration inetAddresses = iface.getInetAddresses (); while (inetAddresses.hasMoreElements ()) {String ip = inetAddresses.nextElement () .getHostAddress (); / / exclude loopback IP/ipv6 address if (ip.contains (":)) continue; if (StringUtils.isNotBlank (ip)) ips.add (ip) } catch (SocketException E1) {e1.printStackTrace ();} return ips;} / * * obtain private network IP * / public static String getLocalIntranetIp () {List ips = getLocalIps (); for (String ip: ips) {if (isIntranetIp (ip)) return ip;} return "" } / * * determine whether the private network IP * tcp/ip protocol reserves three IP address areas as private addresses The address range is as follows: * 10.0.0.0swap 8: 10.0.0.0Universe 10.255.255.255 * 172.16.0.0 ash 12: 172.16.0.0" 172.31.255.255 * 192.168.0.0 public static boolean isIntranetIp 16: 192.168.0.0 * 192.168.255.255 * / public static boolean isIntranetIp (String ip) {try {if ("10. ") | | ip.startsWith (" 192.168. ")) Return true; / / 172.16.x.x~172.31.x.x String [] ns = ip.split ("\\."); int ipSub = Integer.valueOf (ns [0] + ns [1]); if (ipSub > = 17216 & & ipSub
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.