001/* 002 * Copyright 2024-2025, Warm-Flow (290631660@qq.com). 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.dromara.warm.flow.core.utils; 017 018import java.util.*; 019import java.util.stream.Collectors; 020import java.util.stream.IntStream; 021 022/** 023 * 集合工具类 024 * 025 * @author warm 026 * @since 2023/5/18 9:39 027 */ 028public class CollUtil { 029 /** 030 * * 判断一个Collection是否为空, 包含List,Set,Queue 031 * 032 * @param coll 要判断的Collection 033 * @return true:为空 false:非空 034 */ 035 public static boolean isEmpty(Collection<?> coll) { 036 return ObjectUtil.isNull(coll) || coll.isEmpty(); 037 } 038 039 /** 040 * 获取集合的第一个 041 * 042 * @param list 043 */ 044 public static <T> T getOne(List<T> list) { 045 return CollUtil.isEmpty(list) ? null : list.get(0); 046 } 047 048 /** 049 * * 判断一个Collection是否非空,包含List,Set,Queue 050 * 051 * @param coll 要判断的Collection 052 * @return true:非空 false:空 053 */ 054 public static boolean isNotEmpty(Collection<?> coll) { 055 return !isEmpty(coll); 056 } 057 058 /** 059 * 如果集合是空,则返回默认值 060 * 061 * @param list 集合 062 * @param defaultList 默认值 063 * @return 结果 064 */ 065 public static <T> List<T> emptyDefault(List<T> list, List<T> defaultList) { 066 return isEmpty(list) ? defaultList : list; 067 } 068 069 /** 070 * 判断给定的collection列表中是否包含数组array 判断给定的数组array中是否包含给定的元素value 071 * 072 * @param collection 给定的集合 073 * @param array 给定的数组 074 * @return boolean 结果 075 */ 076 public static boolean containsAny(Collection<String> collection, String... array) { 077 if (isEmpty(collection) || ArrayUtil.isEmpty(array)) { 078 return false; 079 } else { 080 for (String str : array) { 081 if (collection.contains(str)) { 082 return true; 083 } 084 } 085 return false; 086 } 087 } 088 089 /** 090 * 判断给定的collection1列表中是否包含collection2 判断给定的collection2中是否包含给定的元素value 091 * 092 * @param collection1 给定的集合1 093 * @param collection2 给定的集合2 094 * @return boolean 结果 095 */ 096 public static boolean containsAny(Collection<String> collection1, Collection<String> collection2) { 097 if (isEmpty(collection1) || isEmpty(collection2)) { 098 return false; 099 } else { 100 for (String str : collection2) { 101 if (collection1.contains(str)) { 102 return true; 103 } 104 } 105 return false; 106 } 107 } 108 109 /** 110 * 判断给定的collection1列表中是否包含collection2 判断给定的collection2中是否完全不包含给定的元素value 111 * 112 * @param collection1 给定的集合1 113 * @param collection2 给定的集合2 114 * @return boolean 结果 115 */ 116 public static boolean notContainsAny(Collection<String> collection1, Collection<String> collection2) { 117 return !containsAny(collection1, collection2); 118 } 119 120 /** 121 * 字符串转数组 122 * 123 * @param str 字符串 124 * @param sep 分隔符 125 * @return 126 */ 127 public static List<String> strToColl(String str, String sep) { 128 return StringUtils.isEmpty(str) ? null : Arrays.asList(str.split(sep)); 129 } 130 131 /** 132 * 集合add新的对象,返回新的集合 133 * 134 * @param list 集合 135 * @param t 对象 136 * @return 137 */ 138 public static <T> List<T> listAddToNew(List<T> list, T t) { 139 return listAddToNew(list, Collections.singletonList(t)); 140 } 141 142 /** 143 * 集合add新的对象,返回新的集合 144 * 145 * @param list 集合 146 * @param listA 对象 147 * @return 148 */ 149 public static <T> List<T> listAddToNew(List<T> list, List<T> listA) { 150 List<T> newList = new ArrayList<>(); 151 newList.addAll(listA); 152 newList.addAll(list); 153 return newList; 154 } 155 156 /** 157 * 几个元素生成一个集合 158 * 159 * @param paramArr 对象数组 160 * @param <T> 泛型 161 * @author xiar 162 * @since 2024/5/10 15:45 163 */ 164 public static <T> List<T> toList(T... paramArr) { 165 if (ArrayUtil.isEmpty(paramArr)) { 166 return new ArrayList<>(); 167 } 168 List<T> arrayList = new ArrayList<>(paramArr.length); 169 arrayList.addAll(Arrays.asList(paramArr)); 170 return arrayList; 171 } 172 173 /** 174 * 将collection转化为List集合,其中一个List集合中包含多个集合<br> 175 * <B>{@code Collection<T>和Collection<Collection<T>> ------> List<T> } </B> 176 * 177 * @param list 需要合并得集合 178 * @param lists 需要合并得包含多个集合得集合 179 * @param <T> List中的泛型 180 * @return List<T> 181 * @author xiarg 182 * @since 2024/5/10 15:45 183 */ 184 public static <T> List<T> listAddListsToNew(List<T> list, List<List<T>> lists) { 185 List<T> newList = new ArrayList<>(); 186 if (isNotEmpty(lists)) { 187 for (List<T> ts : lists) { 188 if (isNotEmpty(ts)) { 189 newList.addAll(ts); 190 } 191 } 192 } 193 if (isNotEmpty(list)) { 194 newList.addAll(list); 195 } 196 return newList; 197 } 198 199 /** 200 * 字符串集合拼接字符串 201 * 202 * @param list 字符串集合 203 * @param sep 分隔符 204 * @return String 205 * @author xiar 206 * @since 2024/5/10 15:45 207 */ 208 public static String strListToStr(List<String> list, String sep) { 209 StringBuilder sb = new StringBuilder(); 210 if (isNotEmpty(list)) { 211 for (String str : list) { 212 sb.append(str).append(sep); 213 } 214 sb.deleteCharAt(sb.length() - 1); 215 } 216 return sb.toString(); 217 } 218 219 /** 220 * 按照<p>batchSize</p>分割源集合,微批概念 221 * 222 * @param list 源集合 223 * @param batchSize 分批大小 224 * @param <T> obj 225 * @return 指定<p>batchSize</p>的全部list 226 */ 227 public static <T> List<List<T>> split(List<T> list, int batchSize) { 228 final int N = (int) Math.ceil((double) list.size() / batchSize); 229 return IntStream.range(0, N).boxed() 230 .map(i -> list.subList(i * batchSize, Math.min((i + 1) * batchSize, list.size()))) 231 .collect(Collectors.toList()); 232 } 233}