// 如何把list集合拼接成以逗号分隔的字符串 a,b,c List<String> list = Arrays.asList("a", "b", "c"); // 第一种方法,可以用stream流 String join = list.stream().collect(Collectors.joining(",")); System.out.println(join); // 输出 a,b,c // 第二种方法,其实String也有join方法可以实现这个功能 String join = String.join(",", list); System.out.println(join); // 输出 a,b,c
List<String> list1 = new ArrayList<>(); list1.add("a"); list1.add("b"); list1.add("c"); List<String> list2 = new ArrayList<>(); list2.add("a"); list2.add("b"); list2.add("d"); list1.retainAll(list2); System.out.println(list1); // 输出[a, b]
apache commons是最强大的,也是使用最广泛的工具类库,里面的子库非常多,下面介绍几个最常用的
建议使用commons-lang3,优化了一些api,原来的commons-lang已停止更新
Maven依赖如下:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency>
String str = "yideng"; String capitalize = StringUtils.capitalize(str); System.out.println(capitalize); // 输出Yideng
String str = StringUtils.repeat("ab", 2); System.out.println(str); // 输出abab
// Date类型转String类型 String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); System.out.println(date); // 输出 2021-05-01 01:01:01 // String类型转Date类型 Date date = DateUtils.parseDate("2021-05-01 01:01:01", "yyyy-MM-dd HH:mm:ss"); // 计算一个小时后的日期 Date date = DateUtils.addHours(new Date(), 1);
Maven依赖如下:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency>
/ 两个集合取交集 Collection<String> collection = CollectionUtils.retainAll(listA, listB); // 两个集合取并集 Collection<String> collection = CollectionUtils.union(listA, listB); // 两个集合取差集 Collection<String> collection = CollectionUtils.subtract(listA, listB);
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency>
对象和map互转
// 对象转map Map<String, String> map = BeanUtils.describe(user); System.out.println(map); // 输出 {"id":"1","name":"yideng"} // map转对象 User newUser = new User(); BeanUtils.populate(newUser, map); System.out.println(newUser); // 输出 {"id":1,"name":"yideng"}
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.8.0</version> </dependency>
File file = new File("demo1.txt"); // 读取文件 List<String> lines = FileUtils.readLines(file, Charset.defaultCharset()); // 写入文件 FileUtils.writeLines(new File("demo2.txt"), lines); // 复制文件 FileUtils.copyFile(srcFile, destFile);
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1.1-jre</version> </dependency>
List<String> list = Lists.newArrayList(); List<Integer> list = Lists.newArrayList(1, 2, 3); // 反转list List<Integer> reverse = Lists.reverse(list); System.out.println(reverse); // 输出 [3, 2, 1] // list集合元素太多,可以分成若干个集合,每个集合10个元素 List<List<Integer>> partition = Lists.partition(list, 10); Map<String, String> map = Maps.newHashMap(); Set<String> set = Sets.newHashSet();
Multimap<String, Integer> map = ArrayListMultimap.create(); map.put("key", 1); map.put("key", 2); Collection<Integer> values = map.get("key"); System.out.println(map); // 输出 {"key":[1,2]} // 还能返回你以前使用的臃肿的Map Map<String, Collection<Integer>> collectionMap = map.asMap();
BiMap<String, String> biMap = HashBiMap.create(); // 如果value重复,put方法会抛异常,除非用forcePut方法 biMap.put("key","value"); System.out.println(biMap); // 输出 {"key":"value"} // 既然value不能重复,何不实现个翻转key/value的方法,已经有了 BiMap<String, String> inverse = biMap.inverse(); System.out.println(inverse); // 输出 {"value":"key"}
// 一批用户,同时按年龄和性别分组 Table<Integer, String, String> table = HashBasedTable.create(); table.put(18, "男", "yideng"); table.put(18, "女", "Lily"); System.out.println(table.get(18, "男")); // 输出 yideng // 这其实是一个二维的Map,可以查看行数据 Map<String, String> row = table.row(18); System.out.println(row); // 输出 {"男":"yideng","女":"Lily"} // 查看列数据 Map<Integer, String> column = table.column("男"); System.out.println(column); // 输出 {18:"yideng"}
Multiset<String> multiset = HashMultiset.create();
multiset.add("apple");
multiset.add("apple");
multiset.add("orange");
System.out.println(multiset.count("apple")); // 输出 2
// 查看去重的元素
Set<String> set = multiset.elementSet();
System.out.println(set); // 输出 ["orange","apple"]
// 还能查看没有去重的元素
Iterator<String> iterator = multiset.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 还能手动设置某个元素出现的次数
multiset.setCount("apple", 5);
原文:https://www.cnblogs.com/kc19941205/p/14944042.html