# Hutool之集合工具——CollectionUtil

2014/05/09 17:30

#### 集合工具 `CollectionUtil`

##### 1. `join` 方法

``````String[] col= new String[]{a,b,c,d,e};
String str = CollectionUtil.join(col, "#"); //str -> a#b#c#d#e
``````
##### 2. `sortPageAll`、`sortPageAll2`方法

``````//Integer比较器
Comparator<Integer> comparator = new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
};

//新建三个列表，CollectionUtil.newArrayList方法表示新建ArrayList并填充元素
List<Integer> list1 = CollectionUtil.newArrayList(1, 2, 3);
List<Integer> list2 = CollectionUtil.newArrayList(4, 5, 6);
List<Integer> list3 = CollectionUtil.newArrayList(7, 8, 9);

//参数表示把list1,list2,list3合并并按照从小到大排序后，取0~2个（包括第0个，不包括第2个），结果是[1,2]
@SuppressWarnings("unchecked")
List<Integer> result = CollectionUtil.sortPageAll(0, 2, comparator, list1, list2, list3);
System.out.println(result);     //输出 [1,2]
``````

##### 5.`newHashMap`、`newHashSet`、`newArrayList`方法

``````HashMap<String, String> map = CollectionUtil.newHashMap();
HashSet<String> set = CollectionUtil.newHashSet();
ArrayList<String> list = CollectionUtil.newArrayList();
``````

##### 9. `range`方法

``````int[] a1 = CollectionUtil.range(6);       //[0,1,2,3,4,5]
int[] a2 = CollectionUtil.range(4, 7);    //[4,5,6]
int[] a3 = CollectionUtil.range(4, 9, 2); //[4,6,8]
``````

##### 12. `zip`方法

``````String[] keys = new String[]{"a", "b", "c"};
Integer[] values = new Integer[]{1, 2, 3};
Map<String, Integer> map = CollectionUtil.zip(keys,values);
System.out.println(map);    // {b=2, c=3, a=1}

String a = "a,b,c";
String b = "1,2,3";
Map<String, String> map2 = CollectionUtil.zip(a,b, ",");
System.out.println(map2);   // {b=2, c=3, a=1}
``````

0
13 收藏

0 评论
13 收藏
0