集合工具类,提供数组转LIST、数组转SET、合并集合、计算笛卡儿积等方法。
源码如下:(点击下载 - CollectionUtil.java、ArrayUtil.java、commons-lang-2.6.jar、commons-collections4-4.0.jar)
1 import java.util.ArrayList; 2 import java.util.Arrays; 3 import java.util.Collection; 4 import java.util.LinkedHashSet; 5 import java.util.List; 6 import java.util.Set; 7 import org.apache.commons.collections4.CollectionUtils; 8 import org.apache.commons.lang.ArrayUtils; 9 10 /** 11 * 集合工具类 12 * 13 */ 14 public class CollectionUtil { 15 16 private static transient final String DEFAULT_SPLIT_STR = ","; 17 18 /** 19 * 数组转列表 20 * 21 * @param arr 22 * an array of T objects. 23 * @param <T> 24 * a T object. 25 * @return a {@link java.util.List} object. 26 */ 27 public static final <T> List<T> array2List(T[] arr) { 28 if (ArrayUtils.isEmpty(arr)) { 29 return null; 30 } 31 return Arrays.asList(arr); 32 } 33 34 /** 35 * 数组转SET 36 * 37 * @param arr 38 * an array of T objects. 39 * @param <T> 40 * a T object. 41 * @return a {@link java.util.Set} object. 42 */ 43 public static final <T> Set<T> array2Set(T[] arr) { 44 if (ArrayUtils.isEmpty(arr)) { 45 return null; 46 } 47 return new LinkedHashSet<T>(Arrays.asList(arr)); 48 } 49 50 /** 51 * 合并集合 52 * 53 * @param collections 54 * a {@link java.util.Collection} object. 55 * @param <T> 56 * a T object. 57 * @return a {@link java.util.List} object. 58 */ 59 @SuppressWarnings("unchecked") 60 public static final <T> List<T> collections2List( 61 Collection<T>... collections) { 62 if (ArrayUtils.isEmpty(collections)) { 63 return null; 64 } 65 66 final List<T> li = new ArrayList<T>(); 67 for (Collection<T> foo : collections) { 68 if (CollectionUtils.isNotEmpty(foo)) { 69 li.addAll(foo); 70 } 71 } 72 return li; 73 } 74 75 /** 76 * 合并集合 77 * 78 * @param collections 79 * a {@link java.util.Collection} object. 80 * @param <T> 81 * a T object. 82 * @return a {@link java.util.Set} object. 83 */ 84 @SuppressWarnings("unchecked") 85 public static final <T> Set<T> collections2Set(Collection<T>... collections) { 86 if (ArrayUtils.isEmpty(collections)) { 87 return null; 88 } 89 final Set<T> set = new LinkedHashSet<T>(); 90 for (Collection<T> foo : collections) { 91 if (CollectionUtils.isNotEmpty(foo)) { 92 set.addAll(foo); 93 } 94 } 95 return set; 96 } 97 98 /** 99 * 拼接集合字符串 100 * 101 * @param collection 102 * a {@link java.util.Collection} object. 103 * @param joinStr 104 * a {@link java.lang.String} object. 105 * @return a {@link java.lang.String} object. 106 */ 107 @SuppressWarnings({ "unchecked", "rawtypes" }) 108 public static final String join(final Collection collection, final String joinStr) { 109 if (CollectionUtils.isEmpty(collection)) { 110 return ""; 111 } 112 if (joinStr == null) { 113 throw new IllegalArgumentException("join string is null."); 114 } 115 116 Object[] arr = ArrayUtil.toArray(collection, Object.class); 117 return ArrayUtil.join(arr, joinStr); 118 } 119 120 /** 121 * 拼接集合字符串 122 * 123 * @param collection 124 * a {@link java.util.Collection} object. 125 * @return a {@link java.lang.String} object. 126 */ 127 @SuppressWarnings("rawtypes") 128 public static final String join(final Collection collection) { 129 return join(collection, DEFAULT_SPLIT_STR); 130 } 131 132 /** 133 * 计算笛卡儿积 134 * 135 * @param crossArgs 136 * a {@link java.util.List} object. 137 * @param <T> 138 * a T object. 139 * @return a {@link java.util.List} object. 140 */ 141 public static <T> List<List<T>> decartes(List<List<T>> crossArgs) { 142 143 // 计算出笛卡尔积行数 144 int rows = crossArgs.size() > 0 ? 1 : 0; 145 146 for (List<T> data : crossArgs) { 147 rows *= data.size(); 148 } 149 150 // 笛卡尔积索引记录 151 int[] record = new int[crossArgs.size()]; 152 153 List<List<T>> results = new ArrayList<List<T>>(); 154 155 // 产生笛卡尔积 156 for (int i = 0; i < rows; i++) { 157 List<T> row = new ArrayList<T>(); 158 159 // 生成笛卡尔积的每组数据 160 for (int index = 0; index < record.length; index++) { 161 row.add(crossArgs.get(index).get(record[index])); 162 } 163 164 results.add(row); 165 crossRecord(crossArgs, record, crossArgs.size() - 1); 166 } 167 168 return results; 169 } 170 171 /** 172 * @param sourceArgs 173 * @param record 174 * @param level 175 */ 176 private static <T> void crossRecord(List<List<T>> sourceArgs, int[] record, int level) { 177 record[level] = record[level] + 1; 178 179 if (record[level] >= sourceArgs.get(level).size() && level > 0) { 180 record[level] = 0; 181 crossRecord(sourceArgs, record, level - 1); 182 } 183 } 184 185 /** 186 * 从list中取得某段数据 187 * 188 * @param <T> 189 * @param datas 190 * @param page 191 * @param PAGESIZE 192 * @return 193 */ 194 public static <T> List<T> getLimit(List<T> datas, int begin, int end) { 195 List<T> objects = new ArrayList<T>(); 196 if (datas.size() <= begin) { 197 return objects; 198 } 199 int temp = (datas.size() > end) ? end : datas.size(); 200 for (int i = begin; i < temp; i++) { 201 objects.add(datas.get(i)); 202 } 203 return objects; 204 } 205 206 }