CollUtil

Introduction

This tool mainly adds operations for arrays and set classes.

join Method

Converts a collection to a string. This method is quite common and the opposite of StrUtil.split. This method supports collections of various types of objects, and for each object, it calls its toString() method when connecting. Example:

String[] col= new String[]{"a","b","c","d","e"};
List<String> colList = CollUtil.newArrayList(col);

String str = CollUtil.join(colList, "#"); //str -> a#b#c#d#e

sortPageAllsortPageAll2 Method

This method is actually a combination method that functions by putting multiple collections into a list (List), sorting them based on a given Comparator object, and then paging through the data. This method is similar to sorting and paging through multiple tables in a database query, and it is the purpose of this method to do so. The functionality of sortPageAll2 is the same as sortPageAll, but sortPageAll2 uses the BoundedPriorityQueue class to store the combined list, unsure if either performs better, they were both retained. Example usage:

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

//Three lists are created, CollUtil.newArrayList indicates creating a new ArrayList and populating it with elements
List<Integer> list1 = CollUtil.newArrayList(1, 2, 3);
List<Integer> list2 = CollUtil.newArrayList(4, 5, 6);
List<Integer> list3 = CollUtil.newArrayList(7, 8, 9);

//Parameters indicate combining lists list1, list2, and list3, sorting them in ascending order, and taking items 0-2 (including 0 but not 2), the result is [1,2]
@SuppressWarnings("unchecked")
List<Integer> result = CollUtil.sortPageAll(0, 2, comparator, list1, list2, list3);
System.out.println(result);     //Output [1,2]

sortEntrySetToList Method

This method mainly sorts Entry<Long, Long> according to the value, but has limited usage as I have forgotten where it was used…

popPart Method

This method takes a stack as input and pops a specified number of elements from it. Popping refers to the pop() method that removes elements from the original stack.

append Method

This method adds one element to the end of a given array. This is similar to the List.add() method, but exists for adding a small number of elements as it internally uses System.arraycopy, which involves copying the array each time it’s called. It’s used in situations where only arrays can be used to avoid converting to a List, adding an element, and then converting back to an array again.

7. resize Method

Resizes the data by truncating it if the new size is smaller than the original size or leaving empty slots if the new size is larger. (This is similar to how List expands when resized.)

addAll Method

Merges multiple datasets into one array.

sub Method

This method slices a collection, converts other types of collections into a List, encapsulates the List.subList() method to automatically correct any overflow issues and completely avoid IndexOutOfBoundsException. Example usage:

Collection<String> keys = CollUtil.newArrayList("a", "b", "c", "d");
Collection<Integer> values = CollUtil.newArrayList(1, 2, 3, 4);

// {a=1,b=2,c=3,d=4}
Map<String, Integer> map = CollUtil.zip(keys, values);