Java Coding Tips

Posted bladestone

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Coding Tips相关的知识,希望对你有一定的参考价值。

Print Map in Java

Arrays.toString(map.entrySet().toArray())

Print List in Java

Arrays.toString(list.toArray())

拼接List的字符串

String.join(‘delimiter’, ListObject)

List<String> list = Arrays.asList("one", "two", "three");
System.out.println("Joined String:" + String.join(",", list));

输出结果为:

System.out.println("Joined String:" + String.join(",", list));

List的数据操作

list.replaceAll(s-> s.toUpperCase())

List<String> list = Arrays.asList("one", "two", "three");
System.out.println("before modification: " + list);
list.replaceAll(s -> s.toUpperCase());
System.out.println("after modification: " + list);

输出结果为:

before modification: [one, two, three]
after modification: [ONE, TWO, THREE]

模式匹配

@Test
public void isPositiveInteger() 
	System.out.println("\\n*** Positive Intger *****");
	String pattern = "^\\\\+?[1-9]\\\\d*"; //? -- 0,1
		
	System.out.println("90:" + "90".matches(pattern));
	System.out.println("9:" + "9".matches(pattern));
	System.out.println("90.1:" + "90.1".matches(pattern));
	System.out.println("-9:" + "-9".matches(pattern));
	System.out.println("00:" + "00".matches(pattern));


@Test
public void isNegativeInteger() 
	System.out.println("\\n*** Negative Intger *****");
    String pattern = "^-[1-9]\\\\d*";
		
	System.out.println("90:" + "90".matches(pattern));
	System.out.println("90.1:" + "90.1".matches(pattern));
	System.out.println("-9:" + "-9".matches(pattern));
	System.out.println("00:" + "00".matches(pattern));
	System.out.println("-0.1:" + "00".matches(pattern));

	
@Test
public void isWholeNumber() 
	System.out.println("\\n ***  is Pure Number ****");
	String numberPattern = "[+-]?0";
		
	System.out.println("+0:" + "+0".matches(numberPattern));
	System.out.println("-0:" + "-0".matches(numberPattern));
	System.out.println("0:" + "0".matches(numberPattern));
		
	//combine three pattern 0, isNegative, isPositive

	
@Test
public void isNegativeDecimal() 
	System.out.println("\\n ***  is Negative Number ****");
	String decimalPattern = "^-[0]\\\\.[1-9]*|^-[1-9]\\\\d*\\\\.\\\\d*";
		
	System.out.println("-12.1:" + "-12.1".matches(decimalPattern));
	System.out.println("-122:" + "-122".matches(decimalPattern));;
	System.out.println("122:" + "122".matches(decimalPattern));;
	System.out.println("0.2:" + "0.2".matches(decimalPattern));;

	
@Test
public void isPositveDecimal() 
	System.out.println("\\n ***  is Positive Number ****");
	String positvePattern = "\\\\+0,1[0]\\\\.[1-9]*|\\\\+0,1[1-9]\\\\d*\\\\.\\\\d*";
		
	System.out.println("-12.1:" + "-12.1".matches(positvePattern));
	System.out.println("-122:" + "-122".matches(positvePattern));;
	System.out.println("122:" + "122".matches(positvePattern));;
	System.out.println("0.2:" + "0.2".matches(positvePattern));;

Map To List

map.values().stream().collect(Collectors.toList());

List to Map:

Map<String, BigDecimal> result = listDevs.stream().collect(Collectors.toMap(Developer :: getName, Developer :: getSal));

Java Project Maven Profile Variable

  • $project.activeProfiles[0].id : 当前活跃的profile的id
  • $project.profiles[0].id : 当前的profile的第一个的id

构建Request URL

String url = "http://localhost:8080/demo";
 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
                .queryParam("msisdn", "msisdn")
                .queryParam("email", "email")
                .queryParam("clientVersion", "clientVersion")
                .queryParam("clientType", "clientType")
                .queryParam("issuerName", "issuerName")
                .queryParam("applicationName", "applicationName");
  log.info("uri:", builder.toUriString());

Object Operation

BeanUtils.copyProperties(Object source, Object target, Class editable, String[] ignoreProperties)
apache common BeanUtils.copyProperties()
org.apache.commons.beanutils.PropertyUtils.copyProperties(Object dest, Object orig)

以上是关于Java Coding Tips的主要内容,如果未能解决你的问题,请参考以下文章

[Tips] python发送邮件

Tips--java

Python TIPS上一道关于人民币金额小写转大写的题

tips(java)

java tips

ref:JAVA代码审计的一些Tips(附脚本)