Java Coding Tips

本文介绍了Java中打印Map和List的方法,展示了如何使用正则表达式进行模式匹配,包括整数、小数的验证,并演示了如何将List转换为Map及构建请求URL等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值