package com.zy.crm.common.utils; import com.zy.crm.common.entity.RouteCollectCountType; import java.util.ArrayList; import java.util.List; import static java.util.stream.Collectors.toList; /** * Created by Monkey D. Luffy on 2023/7/18 */ public class SetOfUtils { // 正序 public static final List TRACK_POSITION_POSITIVE_SEQUENCE = new ArrayList() {{ add(1);add(2);add(3);add(4);add(5);add(6);add(7);add(8);add(9);add(10);add(11);add(12); }}; // 反序 public static final List TRACK_POSITION_REVERSE_SEQUENCE = new ArrayList() {{ add(12);add(11);add(10);add(9);add(8);add(7);add(6);add(5);add(4);add(3);add(2);add(1); }}; public static String zerofill(String msg, Integer count){ if (msg.length() == count){ return msg; } else if (msg.length() > count){ return msg.substring(0, 16); } else { StringBuilder msgBuilder = new StringBuilder(msg); for (int i = 0; i getRoute(Integer groupStart,Integer groupEnd){ boolean sign = groupStart < groupEnd; List result = new ArrayList<>(); List groupRoute = null; if (sign){ groupRoute = TRACK_POSITION_POSITIVE_SEQUENCE; }else { groupRoute = TRACK_POSITION_REVERSE_SEQUENCE; } if (groupRoute.contains(groupStart) && groupRoute.contains(groupEnd)) { sign = false; for (Integer route : groupRoute) { if (route.equals(groupStart)){ sign=true; } if (route.equals(groupEnd)){ result.add(route); break; } if (sign){ result.add(route); } } }else { return null; } return result; } //是否有交集 public static boolean getRouteBoolean(List groupCurrent,List groupOther){ for (Integer positionCurrent : groupCurrent){ for (Integer positionOther : groupOther){ if (positionCurrent.equals(positionOther)){ return true; } } } return false; } //集合运算 public static List getRouteIntersection(List groupCurrent, List groupOther, RouteCollectCountType routeCollectCountType){ switch (routeCollectCountType){ case INTERSECTION: //交集 return groupCurrent.stream().filter(item -> groupOther.contains(item)).collect(toList()); case DIFFERENCESET: //差集 return groupCurrent.stream().filter(item -> !groupOther.contains(item)).collect(toList()); case UNION: //并集 groupCurrent.addAll(groupOther); return groupCurrent; case DEDUPLICATIONUNION: //去重并集 groupCurrent.addAll(groupOther); return groupCurrent.stream().distinct().collect(toList()); default: return null; } } //集合运算 public static List getRouteIntersectionString(List groupCurrent, List groupOther, RouteCollectCountType routeCollectCountType){ switch (routeCollectCountType){ case INTERSECTION: //交集 List intersection = groupCurrent.stream().filter(item -> groupOther.contains(item)).collect(toList()); return intersection; case DIFFERENCESET: //差集 List reduce1 = groupCurrent.stream().filter(item -> !groupOther.contains(item)).collect(toList()); return reduce1; case UNION: //并集 List listAll = groupCurrent.parallelStream().collect(toList()); List listAll2 = groupOther.parallelStream().collect(toList()); listAll.addAll(listAll2); return listAll; case DEDUPLICATIONUNION: //去重并集 List listAll1 = groupCurrent.parallelStream().collect(toList()); List listAll21 = groupOther.parallelStream().collect(toList()); listAll1.addAll(listAll21); List listAllDistinct = listAll1.stream().distinct().collect(toList()); return listAllDistinct; default: return null; } } public static void main(String[] arge){ List routeCurrent = getRoute(2, 9); //获取当前小车路径 List routeOther = getRoute(12, 5); //获取其它小车路径 System.out.println("当前小车路径:\t"+routeCurrent); System.out.println("其它小车路径:\t"+routeOther); boolean routeBoolean = getRouteBoolean(routeCurrent, routeOther); //是否有交集 System.out.println("是否有交集:\t"+routeBoolean); List routeIntersection = getRouteIntersection(routeCurrent, routeOther, RouteCollectCountType.INTERSECTION);//交集 System.out.println("路径交集:\t"+routeIntersection); List routeIntersection1 = getRouteIntersection(routeCurrent, routeOther, RouteCollectCountType.DIFFERENCESET);//差集 System.out.println("路径差集:\t"+routeIntersection1); List routeIntersection2 = getRouteIntersection(routeCurrent, routeOther, RouteCollectCountType.UNION);//并集 System.out.println("路径并集:\t"+routeIntersection2); List routeIntersection3 = getRouteIntersection(routeCurrent, routeOther, RouteCollectCountType.DEDUPLICATIONUNION);//去重并集 System.out.println("路径去重并集:\t"+routeIntersection3); } }