GeneralUtils

package com.suning.cpm.utils;

import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.beans.BeanCopier;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author 20014258
 * @date 2021/1/19
 */
@Slf4j
public class GeneralUtils {
        private final static AtomicInteger COUNT_ID = new AtomicInteger();
	/**
	 * 多集合(过滤空集合)取交集retain公共方法
	 */
	public static Collection retain(Collection<Collection> c){
		Optional result = c.parallelStream()
				.filter(element -> CollectionUtils.isNotEmpty(element))
				.reduce((m1, m2)->{
			m1.retainAll(m2);
			return m1;
		});
		return (Collection) result.get();
	}

	/**
	 * 多集合(过滤空集合)取交集retain公共方法 参数String
	 */
	public static List<String> retainElementList(List<List<String>> elementLists) {
		Optional<List<String>> result = elementLists.parallelStream()
				.filter(elementList -> CollectionUtils.isNotEmpty(elementList))
				.reduce((a, b) -> {
					a.retainAll(b);
					return a;
				});
		return result.orElse(new ArrayList<>());
	}


	/**
	 * 性能更好的对象拷贝属性
	 * 要比 BeanUtils.copyProperties 快n倍
	 * @param source
	 * @param target
	 */
	public static void copyProperties(Object source, Object target) {
		BeanCopier copier = BeanCopier.create(source.getClass(), target.getClass(), false);
		copier.copy(source, target, null);
	}

        /**
  	 * 统计字符串出现的次数
  	 * @param str
  	 * @param find
  	 * @return
  	 */
  	public static int countString(String str, String find) {
  		int count = 0;
  		while (str.indexOf(find) != -1) {
  			count++;
  			str = str.substring(str.indexOf(find) + find.length());
  		}
  		return count;
  	}

        /**
	 * List转换成IN参数
	 *
	 * @param listString
	 * @return
	 */
	public static String listToIn(List<String> listString) {
		String result = "";
		for (String str : listString) {
			if (StringUtils.isBlank(str)) {
				continue;
			}
			result = result + ",'" + str + "'";
		}
		result = result.replaceFirst(",", "");
		return result;
	}

	/**
	 * 当前时间戳 + 三位自增ID
	 * 202107151936001
	 *
	 * @return
	 */
	public static String getUniqueId() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
		return formatter.format(System.currentTimeMillis()) + String.format("%1$03d", COUNT_ID.incrementAndGet());
	}

    /**	
	 * 获取像个实体类对象中 相同元素属性集合
	 * @return
	 */
     public class Main {
        public static void main(String[] args) {
            List<EntityA> listA = Arrays.asList(new EntityA(1), new EntityA(2), new EntityA(3));
            List<EntityB> listB = Arrays.asList(new EntityB(2), new EntityB(3), new EntityB(4));
     
            List<Integer> commonIds = listA.stream()
                    .map(EntityA::getId)
                    .filter(id -> listB.stream().map(EntityB::getId).collect(Collectors.toList()).contains(id))
                    .collect(Collectors.toList());
     
            commonIds.forEach(System.out::println); // 打印相同的id

        }
    }
    

}

```plaintext
List<Map<String,String>> stockholderList = clientService.qryStockholder(base, highFrequencyInvestorQryForm.getClient_id(), highFrequencyInvestorQryForm.getFund_account());
				// 获取证券账户相同数据集合
				resultDTOList = resultDTOList.stream().filter(it->{
					boolean flag = false;
					for (Map<String, String> map : stockholderList) {
						if (StringUtils.equals(it.getStock_account(), map.get("stock_account"))) {
							flag = true;
						}
					}
                    return flag;
                }).collect(Collectors.toList());
posted @ 2021-05-11 11:49  王岳阳  阅读(99)  评论(0编辑  收藏  举报