All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sghd.common.utils.array.ArrayHelper Maven / Gradle / Ivy

The newest version!
package com.sghd.common.utils.array;

import java.util.*;

@SuppressWarnings("unchecked")
public abstract class ArrayHelper {
	static public interface Matcher {
		public boolean match(T record);
	}

	public static int getRange(int current, Integer[] ranges) {
		int range = 1;
		for (Integer value : ranges) {
			if (current >= value) {
				range++;
			} else {
				break;
			}
		}
		return range;
	}

	public static int getRange(double current, Integer[] ranges) {
		int range = 0;
		for (Integer value : ranges) {
			if (current > value) {
				range++;
			} else {
				break;
			}
		}
		return range;
	}
	
	public static void main(String[] args) {
		Integer[] arr = new Integer[]{4,10,16};
		System.out.println(getRange(0, arr));
		System.out.println(getRange(1, arr));
		System.out.println(getRange(4, arr));
		System.out.println(getRange(5, arr));
		System.out.println(getRange(10, arr));
		System.out.println(getRange(11, arr));
		System.out.println(getRange(16, arr));
	}

	public static  List remove(T[] list, T obj) {
		List result = new ArrayList<>(list.length);
		for (T t : list) {
			if (obj.equals(t)) {
				continue;
			}
			result.add(t);
		}
		return result;
	}

	// ///////////////集合空检查////////////////
	public static  boolean isEmpty(Collection list) {
		if (list == null || list.isEmpty()) {
			return true;
		}
		return false;
	}

	public static  boolean isEmpty(T[] list) {
		if (list == null || list.length == 0) {
			return true;
		}
		return false;
	}

	public static  boolean isNotEmpty(Collection list) {
		return !isEmpty(list);
	}

	public static  boolean isNotEmpty(T[] list) {
		return !isEmpty(list);
	}

	// /////////////////切割集合////////////////////////
	public static  List pageQuery(T[] list, int page, int limit) {
		final int start = page * limit;
		final int end = start + limit;
		return subList(list, start, end);
	}

	public static  List pageQuery(List list, int page, int limit) {
		final int start = page * limit;
		final int end = start + limit;
		return subList(list, start, end);
	}

	public static  List subList(T[] list, int start, int end) {
		List result = new LinkedList<>();
		if (start < 0) {
			return result;
		}

		int len = list.length;
		for (int i = start; i < len && i < end; i++) {
			result.add(list[i]);
		}
		return result;
	}

	public static  List subList(List list, int start, int end) {
		List result = new LinkedList<>();
		if (start < 0) {
			return result;
		}

		int len = list.size();
		for (int i = start; i < len && i < end; i++) {
			result.add(list.get(i));
		}
		return result;
	}

	/**
	 * 是否交集
	 */
	public static  boolean contains(T[] A, T[] B) {
		for (T a : A) {
			if (contains(B, a)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 是否交集
	 */
	public static  boolean contains(Collection A, Collection B) {
		for (T a : A) {
			if (B.contains(a)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * B集合是否完全包含A集合
	 */
	public static  boolean containsAll(Collection A, Collection B) {
		return containsAll(A.toArray(), B.toArray());
	}

	/**
	 * B集合是否完全包含A集合
	 */
	public static  boolean containsAll(Collection A, T[] B) {
		return containsAll(A.toArray(), B);
	}

	/**
	 * B集合是否完全包含A集合
	 */
	public static  boolean containsAll(T[] A, T[] B) {
		// if (A.length != B.length) {
		// return false;
		// }
		for (T a : A) {
			boolean isRight = false;
			for (T b : B) {
				if (a.equals(b)) {
					isRight = true;
					break;
				}
			}
			if (!isRight) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 是否在集合内
	 */
	public static  boolean contains(T[] list, T value) {
		for (T t : list) {
			if (t.equals(value)) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 是否在集合内
	 */
	public static  boolean contains(Collection list, Matcher matcher) {

		for (T a : list) {
			if (matcher.match(a)) {
				return true;
			}
		}
		return false;
	}

	// 集合添加
	public static  void addAll(Collection list, T... data) {
		for (T t : data) {
			list.add(t);
		}
	}

	public static  void addAll(Collection list, Collection data) {
		list.addAll(data);
	}

	// 下面是集合合并

	public static  Collection mapValueMergerList(Map map) {
		Collection result = new LinkedList<>();
		for (T[] list : map.values()) {
			_merger(result, list);
		}
		return result;
	}

	public static  Collection mergerSet(T[]... list) {
		Collection result = new HashSet<>();
		_merger(result, list);
		return result;
	}

	public static  Collection mergerSet(Collection... list) {
		Collection result = new HashSet<>();
		_merger(result, list);
		return result;
	}

	public static  Collection mergerList(T[]... list) {
		Collection result = new LinkedList<>();
		_merger(result, list);
		return result;
	}

	public static  Collection mergerList(Collection... list) {
		Collection result = new LinkedList<>();
		_merger(result, list);
		return result;
	}

	private static  void _merger(Collection result, Collection... list) {
		for (Collection l : list) {
			for (T t : l) {
				result.add(t);
			}
		}
	}

	private static  void _merger(Collection result, T[]... list) {
		for (T[] l : list) {
			for (T t : l) {
				result.add(t);
			}
		}
	}

	public static Integer getMaxIndex(Collection list) {
		int index = 0;
		int pre = Integer.MIN_VALUE;
		int i = 0;
		for (Integer size : list) {
			if (pre < size) {
				index = i;
				pre = size;
			}
			i++;
		}

		return index;
	}

	public static int sumList(Collection list) {
		if (isEmpty(list)) {
			return 0;
		}
		int count = 0;
		for (Integer value : list) {
			if (value == null) {
				continue;
			}
			count += value;
		}
		return count;
	}

	public static int sumList(Integer... list) {
		if (isEmpty(list)) {
			return 0;
		}
		int count = 0;
		for (Integer value : list) {
			if (value == null) {
				continue;
			}
			count += value;
		}
		return count;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy