# Java
自带的好用的工具类
# List
集合拼接成以逗号分隔的字符串
1 2 3 4 5 6 7 8
| List<String> list = Arrays.asList("a", "b", "c");
String join = list.stream().collect(Collectors.joining(",")); System.out.println(join);
String join = String.join(",", list); System.out.println(join);
|
# 比较两个字符串是否相等,忽略大小写
1 2 3
| if (strA.equalsIgnoreCase(strB)) { System.out.println("相等"); }
|
# 比较两个对象是否相等
当我们用 equals
比较两个对象是否相等的时候,还需要对左边的对象进行判空,不然可能会报空指针异常,我们可以用 java.util
包下 Objects
封装好的比较是否相等的方法。
1 2 3 4 5
| Objects.equals(strA, strB);
public static boolean equals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); }
|
# 两个 List
取交集
1 2 3 4 5 6 7 8 9 10 11
| 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);
|
# 使用 apache commons
工具类库
# 字符串判空
传参 CharSequence
类型是 String、StringBuilder、StringBuffer
的父类,都可以直接下面方法判空,以下是源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; } public static boolean isNotEmpty(final CharSequence cs) { return !isEmpty(cs); }
public static boolean isBlank(final CharSequence cs) { final int strLen = length(cs); if (strLen == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; } public static boolean isNotBlank(final CharSequence cs) { return !isBlank(cs); }
|
# 首字母转成大写
1 2 3
| String str = "yideng"; String capitalize = StringUtils.capitalize(str); System.out.println(capitalize);
|
# 重复拼接字符串
1 2
| String str = StringUtils.repeat("ab", 2); System.out.println(str);
|
# 格式化日期
1 2 3 4 5 6 7 8 9
| String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); System.out.println(date);
Date date = DateUtils.parseDate("2021-05-01 01:01:01", "yyyy-MM-dd HH:mm:ss");
Date date = DateUtils.addHours(new Date(), 1);
|
# 包装临时对象
当一个方法需要返回两个及以上字段时,我们一般会封装成一个临时对象返回,现在有了 Pair
和 Triple
就不需要了。
1 2 3 4 5 6
| ImmutablePair<Integer, String> pair = ImmutablePair.of(1, "yideng"); System.out.println(pair.getLeft() + "," + pair.getRight());
ImmutableTriple<Integer, String, Date> triple = ImmutableTriple.of(1, "yideng", new Date()); System.out.println(triple.getLeft() + "," + triple.getMiddle() + "," + triple.getRight());
|
# commons-collections
集合工具类
1 2 3 4 5
| <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.4</version> </dependency>
|
# 集合判空
封装了集合判空的方法,以下是源码:
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static boolean isEmpty(final Collection<?> coll) { return coll == null || coll.isEmpty(); } public static boolean isNotEmpty(final Collection<?> coll) { return !isEmpty(coll); }
Collection<String> collection = CollectionUtils.retainAll(listA, listB);
Collection<String> collection = CollectionUtils.union(listA, listB);
Collection<String> collection = CollectionUtils.subtract(listA, listB);
|
# common-beanutils
操作对象
1 2 3 4 5
| <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.9.4</version> </dependency>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class User { private Integer id; private String name; }
User user = new User(); BeanUtils.setProperty(user, "id", 1); BeanUtils.setProperty(user, "name", "yideng"); System.out.println(BeanUtils.getProperty(user, "name")); System.out.println(user);
Map<String, String> map = BeanUtils.describe(user); System.out.println(map);
User newUser = new User(); BeanUtils.populate(newUser, map); System.out.println(newUser);
|
# commons-io
文件流处理
1 2 3 4 5
| <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.8.0</version> </dependency>
|
文件处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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); ``` # ``Google Guava`` 工具类库
```xml <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1.1-jre</version> </dependency>
|
# 创建集合
1 2 3 4 5 6 7 8 9 10
| List<String> list = Lists.newArrayList(); List<Integer> list = Lists.newArrayList(1, 2, 3);
List<Integer> reverse = Lists.reverse(list); System.out.println(reverse);
List<List<Integer>> partition = Lists.partition(list, 10); Map<String, String> map = Maps.newHashMap(); Set<String> set = Sets.newHashSet();
|
# Multimap
一个 key
可以映射多个 value
的 HashMap
1 2 3 4 5 6 7 8
| Multimap<String, Integer> map = ArrayListMultimap.create(); map.put("key", 1); map.put("key", 2); Collection<Integer> values = map.get("key"); System.out.println(map);
Map<String, Collection<Integer>> collectionMap = map.asMap();
|
多省事,多简洁,省得你再创建 Map<String, List>
# BiMap
一种连 value
也不能重复的 HashMap
1 2 3 4 5 6 7
| BiMap<String, String> biMap = HashBiMap.create();
biMap.put("key","value"); System.out.println(biMap);
BiMap<String, String> inverse = biMap.inverse(); System.out.println(inverse);
|
这其实是双向映射,在某些场景还是很实用的。
# Table
一种有两个 key
的 HashMap
1 2 3 4 5 6 7 8 9 10 11
| Table<Integer, String, String> table = HashBasedTable.create(); table.put(18, "男", "yideng"); table.put(18, "女", "Lily"); System.out.println(table.get(18, "男"));
Map<String, String> row = table.row(18); System.out.println(row);
Map<Integer, String> column = table.column("男"); System.out.println(column);
|
# Multiset
一种用来计数的 Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| Multiset<String> multiset = HashMultiset.create(); multiset.add("apple"); multiset.add("apple"); multiset.add("orange"); System.out.println(multiset.count("apple"));
Set<String> set = multiset.elementSet(); System.out.println(set);
Iterator<String> iterator = multiset.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); }
multiset.setCount("apple", 5);
|