package com.eeepay.eeepay_shop.utils;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/*
* ================================================
* 描述:集合操作的工具类
* 作者:zhuangzeqin
* 时间: 2024/9/26 17:54
* 邮箱:zzq@eeepay.cn
* 备注:
* ----------------------------------------------------------------
* You never know what you can do until you try !
* _ _ _ _ ____ _ _ _
* / \ _ __ __| |_ __ ___ (_) __| | / ___|| |_ _ _ __| (_) ___
* / _ \ | '_ \ / _` | '__/ _ | |/ _` | ___ | __| | | |/ _` | |/ _ \
* / ___ | | | | (_| | | | (_) | | (_| | ___) | |_| |_| | (_| | | (_) |
* /_/ __| |_|__,_|_| ___/|_|__,_| |____/ __|__,_|__,_|_|___/
*
* 签名:最痛苦的事不是我失败了,而是我本可以.--zzq
* ----------------------------------------------------------------
* ================================================
*/
public final class CollectionTools {
/**
* 删除原集合中不符合条件的元素
* CollectionTools.removeByOption(shopIndexInfoList, item -> Constans.HOMEMENU.MENU_VIP_EXCLUSIVE_CODE.equals(item.getNameCode()));
*
* @param collection 操作的集合
* @param predicate 要移除元素的条件
* @param <T> 泛型操作
*/
public static <T> void removeByOption(Collection<T> collection, option<T> predicate) {
if (collection == null || predicate == null) return;
Iterator<T> iterator = collection.iterator();
while (iterator.hasNext()) {
T next = iterator.next();
if (predicate.byOption(next)) {
iterator.remove();// todo 移除
}
}
}
/**
* 根据满足条件查找集合元素
* RequireItemInfo.BodyBean.PrayerListBean item_yyzzqc = CollectionTools.findByOption(this.prayerListBeanList, item -> BaseCons.Mer_id__yyzzqc.equals(String.valueOf(item.getItem_id())));
*
* @param collection
* @param predicate
* @param <T>
* @return
*/
public static <T> T findByOption(Collection<T> collection, option<T> predicate) {
if (collection == null || predicate == null) return null;
T currentOption = null;
for (T next : collection) {
if (predicate.byOption(next)) {
currentOption = next;
break;
}
}
return currentOption;
}
/**
* 将满足条件目标元素移动到列表的第一位,并返回修改后的列表
* CollectionTools.moveElementToTop(datas,item -> Constans.HOMEMENU.HOMEMENU_enjoy_life.equals(item.getNameCode()));
* @param list 集合
* @param predicate 条件的判断
* @param <T>
* @return
*/
public static <T> List<T> moveElementToTop(List<T> list, option<T> predicate) {
return moveElementToTop(list, predicate, null);
}
/**
* 将满足条件目标元素移动到列表的第一位,并返回修改后的列表
* CollectionTools.moveElementToTop(datas,item -> Constans.HOMEMENU.HOMEMENU_enjoy_life.equals(item.getNameCode()),shopInfo);
* @param list 集合
* @param defaultItem 默认元素
* @param predicate 条件的判断
* @param <T>
* @return
*/
public static <T> List<T> moveElementToTop(List<T> list, option<T> predicate, T defaultItem) {
if (list == null || predicate == null || list.size() == 0) return list;
T firstItem = list.get(0);//获取列表的第一个元素
if (predicate.byOption(firstItem)) {
//如果条件成立;说明第一个元素满足条件,则直接返回
return list;
}
Iterator<T> iterator = list.iterator();
T currentOption = null;
while (iterator.hasNext()) {
T next = iterator.next();
if (predicate.byOption(next)) {
currentOption = next;
iterator.remove();// todo 移除
}
}
//如果有满足条件的元素,则添加到列表最前面
if (currentOption != null) {
list.add(0, currentOption);//todo 添加到列表最前面
return list;
}
//如果有默认元素,则添加默认元素
if (defaultItem != null) {
list.add(0, defaultItem);//todo 添加到列表最前面
return list;
}
return list;
}
public interface option<E> {
boolean byOption(E item);
}
}