|
@@ -2,6 +2,7 @@ package com.diagbot.util;
|
|
|
|
|
|
import com.diagbot.biz.push.entity.Item;
|
|
|
import com.diagbot.biz.push.entity.Lis;
|
|
|
+import com.diagbot.dto.PushBaseDTO;
|
|
|
import com.diagbot.dto.RuleBaseDTO;
|
|
|
import com.diagbot.enums.LexiconEnum;
|
|
|
import com.diagbot.model.entity.BodyPart;
|
|
@@ -161,16 +162,13 @@ public class CoreUtil {
|
|
|
* @return : 父类中的属性值
|
|
|
*/
|
|
|
public static Object getFieldValue(Object object, String fieldName) {
|
|
|
- //根据 对象和属性名通过反射 调用上面的方法获取 Field对象
|
|
|
- Field field = getDeclaredField(object, fieldName);
|
|
|
-
|
|
|
- //抑制Java对其的检查
|
|
|
- field.setAccessible(true);
|
|
|
-
|
|
|
try {
|
|
|
+ //根据 对象和属性名通过反射 调用上面的方法获取 Field对象
|
|
|
+ Field field = getDeclaredField(object, fieldName);
|
|
|
+ //抑制Java对其的检查
|
|
|
+ field.setAccessible(true);
|
|
|
//获取 object 中 field 所代表的属性值
|
|
|
return field.get(object);
|
|
|
-
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -969,36 +967,105 @@ public class CoreUtil {
|
|
|
*
|
|
|
* @param list
|
|
|
*/
|
|
|
- public static List<String> removeRepeat(List<String> list) {
|
|
|
+ public static <T> List<T> removeRepeat(List<T> list) {
|
|
|
if (ListUtil.isNotEmpty(list) && list.size() > 1) {
|
|
|
list = list.stream().distinct().collect(Collectors.toList());
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据属性去除重复元素
|
|
|
+ *
|
|
|
+ * @param source
|
|
|
+ * @param property
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ public static <T> void removeByRepeatProperty(List<T> source, String property) {
|
|
|
+ if (ListUtil.isEmpty(source)) {
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ List<Object> list = Lists.newArrayList();
|
|
|
+ Iterator<T> iterator = source.iterator();
|
|
|
+ T t = null;
|
|
|
+ Object object = null;
|
|
|
+ while(iterator.hasNext()) {
|
|
|
+ t = iterator.next();
|
|
|
+ object = getFieldValue(t, property);
|
|
|
+ if (list.contains(object)) {
|
|
|
+ iterator.remove();
|
|
|
+ } else {
|
|
|
+ list.add(object);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定属性且过滤属性的名称列表,例如:获取对象negative不为null的uniqueName字段值
|
|
|
+ *
|
|
|
+ * @param input
|
|
|
+ * @param propertyName
|
|
|
+ * @param filterProperty
|
|
|
+ * @param <T>
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static <T> List<String> getPropertyWithFilter(List<T> input, String propertyName, String filterProperty) {
|
|
|
+ List<String> res = Lists.newArrayList();
|
|
|
+ if (ListUtil.isNotEmpty(input)) {
|
|
|
+ for (T d : input) {
|
|
|
+ Negative val = (Negative) CoreUtil.getFieldValue(d, filterProperty);
|
|
|
+ if (val != null) {
|
|
|
+ String name = (String) CoreUtil.getFieldValue(d, propertyName);
|
|
|
+ if (StringUtil.isNotBlank(name)) {
|
|
|
+ res.add(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
|
- List<Item> list = new ArrayList<>();
|
|
|
- Item item = new Item();
|
|
|
- item.setUniqueName("test1");
|
|
|
- item.setName("test");
|
|
|
- list.add(item);
|
|
|
-
|
|
|
- Item item2 = new Item();
|
|
|
- item2.setUniqueName("test2");
|
|
|
- item2.setName("test");
|
|
|
- list.add(item2);
|
|
|
-
|
|
|
- Item item3 = new Item();
|
|
|
- item3.setUniqueName("test1");
|
|
|
- item3.setName("test");
|
|
|
- list.add(item3);
|
|
|
-
|
|
|
- Item item4 = new Item();
|
|
|
- item4.setUniqueName("test2");
|
|
|
- item4.setName("test");
|
|
|
- list.add(item4);
|
|
|
- Map<String, List<Item>> map = makeEntityListMap(list, "name", "uniqueName");
|
|
|
-
|
|
|
- System.out.println(map);
|
|
|
+ List<PushBaseDTO> pushBaseDTOList = ListUtil.newArrayList();
|
|
|
+ PushBaseDTO pushBaseDTO = new PushBaseDTO();
|
|
|
+ pushBaseDTO.setName("a1");
|
|
|
+ pushBaseDTOList.add(pushBaseDTO);
|
|
|
+
|
|
|
+ PushBaseDTO pushBaseDTO1 = new PushBaseDTO();
|
|
|
+ pushBaseDTO1.setName("a1");
|
|
|
+ pushBaseDTOList.add(pushBaseDTO1);
|
|
|
+
|
|
|
+ removeByRepeatProperty(pushBaseDTOList, "name");
|
|
|
+ System.out.println(pushBaseDTOList);
|
|
|
+
|
|
|
+ // List<String> list = Lists.newArrayList("a","b","a");
|
|
|
+ // list = removeRepeat(list);
|
|
|
+ // System.out.println(list);
|
|
|
+ // System.out.println(pushBaseDTOList);
|
|
|
+
|
|
|
+
|
|
|
+ // List<Item> list = new ArrayList<>();
|
|
|
+ // Item item = new Item();
|
|
|
+ // item.setUniqueName("test1");
|
|
|
+ // item.setName("test");
|
|
|
+ // list.add(item);
|
|
|
+ //
|
|
|
+ // Item item2 = new Item();
|
|
|
+ // item2.setUniqueName("test2");
|
|
|
+ // item2.setName("test");
|
|
|
+ // list.add(item2);
|
|
|
+ //
|
|
|
+ // Item item3 = new Item();
|
|
|
+ // item3.setUniqueName("test1");
|
|
|
+ // item3.setName("test");
|
|
|
+ // list.add(item3);
|
|
|
+ //
|
|
|
+ // Item item4 = new Item();
|
|
|
+ // item4.setUniqueName("test2");
|
|
|
+ // item4.setName("test");
|
|
|
+ // list.add(item4);
|
|
|
+ // Map<String, List<Item>> map = makeEntityListMap(list, "name", "uniqueName");
|
|
|
+ //
|
|
|
+ // System.out.println(map);
|
|
|
}
|
|
|
}
|