JAVA集合和字符串互转

1、导入StringUtils的依赖包

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.12.0</version>
		</dependency>

2、字符串转集合

//字符串转list
		String test = "金斧头,银斧头,铜斧头";
		//先把字符串变成String[]
		String[] split = test.split(",");
		//再把String[] 转成list
		List<String> asList = Arrays.asList(split);
		asList.forEach(c ->{
			System.out.println(c);
		});

金斧头
银斧头
铜斧头

3、集合转字符串

		List<String> arrayList = new ArrayList<String>();
		arrayList.add("金斧头");
		arrayList.add("银斧头");
		arrayList.add("铜斧头");
		
		String str= StringUtils.join(arrayList,",");
		System.out.println(str);
金斧头,银斧头,铜斧头

注意:这个StringUtils的包别导错了,他是commons.lang的包


版权声明:本文为m0_46584442原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。