Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查列表中两个对象中的字段_Java_List_Filter - Fatal编程技术网

Java 检查列表中两个对象中的字段

Java 检查列表中两个对象中的字段,java,list,filter,Java,List,Filter,我有一份物品清单: { "rn": "14", "time": "2020-06-16 16:47:55", "id": 42332392988, "termname": "S1160AFB", "type": "100", "trancode": "110&quo

我有一份物品清单:

{
  "rn": "14",
  "time": "2020-06-16 16:47:55",
  "id": 42332392988,
  "termname": "S1160AFB",
  "type": "100",
  "trancode": "110",
  "pan": "4790729949581839",
  "amount": "360.96",
  "approvalcode": "44444"
},
{
  "rn": "15",
  "time": "2020-06-16 16:48:55",
  "id": 42330308379,
  "termname": "S1160AFB",
  "type": "100",
  "trancode": "110",
  "pan": "4149510064617121",
  "amount": "360.96",
  "approvalcode": "55555"
},
{
  "rn": "13",
  "time": "2020-06-16 16:49:11",
  "id": 42332530886,
  "termname": "S1160AFB",
  "type": "420",
  "trancode": "110",
  "pan": "4790729949581839",
  "amount": "360.96",
  "approvalcode": "44444"
}
如果出现以下情况,我需要从列表对象中删除:

  • 它们具有相同的字段批准代码。(approvalcode-唯一。不能超过两个相同)
  • 在具有相同approvalcode的两个对象中,您需要检查其中日期更大的对象,并且如果在该对象类型中,则需要检查日期是否为420。从列表中删除两个对象
  • 我决定这样做:

  • 我按批准代码和日期对列表进行排序

     List<Transaction> filteredTrans = transResponse.getTransaction().stream().sorted(Comparator.comparing(Transaction::approvalcode).thenComparing(Transaction::getTime))
     .collect(Collectors.toList());
    
    List filteredTrans=transResponse.getTransaction().stream().sorted(Comparator.comparing(Transaction::approvalcode)。然后比较(Transaction::getTime))
    .collect(Collectors.toList());
    
  • 排序后,我比较两个最近的对象

      for (int i=0; i<filteredTrans.size()-1; i++) {
      if (filteredTrans.get(i).getApprovalcode().equals(filteredTrans.get(j).getApprovalcode()) && filteredTrans.get(j).getType().equals("420")) {
          filteredTrans.remove(i);
          filteredTrans.remove(j);
          j++;
      }
    }
    

    对于(inti=0;i您应该使用映射而不是排序。下面是如何继续此操作

    import java.time.LocalDateTime;
    导入java.util.ArrayList;
    导入java.util.List;
    导入java.util.function.function;
    导入java.util.stream.collector;
    公开课考试{
    静态类事务{
    字符串批准码;
    本地日期时间;
    int型;
    公共事务(字符串approvalCode、LocalDateTime、int类型){
    this.approvalCode=approvalCode;
    这个时间=时间;
    this.type=type;
    }
    公共int getType(){
    返回类型;
    }
    公共LocalDateTime getTime(){
    返回时间;
    }
    公共字符串getApprovalCode(){
    返回批准代码;
    }
    @凌驾
    公共字符串toString(){
    返回“事务{”+
    “approvalCode=”+approvalCode+'\''+
    “,time=“+时间+
    “,type=“+type”+
    '}';
    }
    }
    公共静态void main(字符串[]args){
    列表事务=新建ArrayList();
    添加(新事务(“1”,LocalDateTime.now(),420));
    添加(新事务(“1”,LocalDateTime.now(),11));
    添加(新事务(“1”,LocalDateTime.now(),12));
    添加(新事务(“2”,LocalDateTime.now(),13));
    添加(新事务(“2”,LocalDateTime.now(),14));
    添加(新事务(“2”,LocalDateTime.now(),420));
    System.out.println(
    transactions.stream()
    .收集(
    汤姆(
    事务::getApprovalCode,
    Function.identity(),
    (u,v)->u.getTime().compareTo(v.getTime())>0?u:v)
    .values()
    .stream()
    .filter(v->v.getType()!=420)
    .collect(Collectors.toList());
    }
    }
    
    迭代列表时,不要从同一列表中删除。这可能会导致并发修改异常。相反,请将所有必需元素复制到新列表/使用流过滤器获取所有帮助。 此代码帮助我:

    List<Transaction> filteredTrans = transResponse.getTransaction().stream()
        .collect(Collectors.groupingBy(Transaction::getApprovalcode))
        .values()
        .stream()
        .filter(l -> {
            if (l.size() == 1) return true;
            Transaction maxTimeTr = l.stream().max(Comparator.comparing(Transaction::getTime)).orElseThrow();
            return !maxTimeTr.getType().equals("420");
        })
        .flatMap(Collection::stream)
        .collect(Collectors.toList());
    
    List filteredTrans=transResponse.getTransaction().stream()
    .collect(收集器.groupingBy(事务::getApprovalcode))
    .values()
    .stream()
    .过滤器(l->{
    如果(l.size()==1)返回true;
    事务maxTimeTr=l.stream().max(Comparator.comparing(Transaction::getTime)).orElseThrow();
    return!maxTimeTr.getType()等于(“420”);
    })
    .flatMap(集合::流)
    .collect(Collectors.toList());
    
    Thx获取答案,但如果等于approvalCode,并且在其中日期为greate type=420,则需要删除这两个对象。例如:List transactions=new ArrayList();transactions.add(new Transaction(“1”,LocalDateTime.now(),420));transactions.add(new Transaction(“1”,LocalDateTime.now(),12));transactions.add(新事务(“2”,LocalDateTime.now(),13));从中应该保留approvalCode==2的事务。结果:[事务{approvalCode='1',time=2020-06-29T02:48:15.828,type=12}]这将删除approvalCode=2的事务。这不是你的意思吗?approvalCode=2必须保留。很抱歉,如果我对问题的表述不正确,请检查具有相同approvalCode的两个对象中日期更大的一个,如果该对象类型为420。从列表中删除这两个对象。使用给定条件回答s、 是的,但具有相同approvalcode的对象的最大值可能为2。抱歉,没有立即编写