Browse Source

feat: new object with 'of' method

easepan 5 years ago
parent
commit
7588535cac
1 changed files with 15 additions and 0 deletions
  1. 15 0
      hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java

+ 15 - 0
hutool-core/src/main/java/cn/hutool/core/collection/ListUtil.java

@@ -474,4 +474,19 @@ public class ListUtil {
 	public static <T> List<T> empty() {
 		return Collections.emptyList();
 	}
+
+	/**
+	 * 像java11一样获取一个List
+	 * @param ts 对象
+	 * @param <T> 对象类型
+	 * @return 不可修改List
+	 */
+	public static <T> List<T> of(T... ts) {
+		if (ArrayUtil.isEmpty(ts)) {
+			return Collections.emptyList();
+		}
+		List<T> unmodifiableList = new ArrayList<>(ts.length);
+		Collections.addAll(unmodifiableList, ts);
+		return Collections.unmodifiableList(unmodifiableList);
+	}
 }