Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

An unexpected null key in HashSet, the first be added not the rest

I’m learning Activiti 7, I drew a BPMN diagram as below:
enter image description here
When the highlight1 UserTask has been completed but the highlight2 UserTask is still pending, I ran the following code to highlight the completed flow element.

private AjaxResponse highlightHistoricProcess(@RequestParam("instanceId") String instanceId,
      @AuthenticationPrincipal UserInfo userInfo) {
    try {
      // Get the instance from the history table
      HistoricProcessInstance instance = historyService
          .createHistoricProcessInstanceQuery().processInstanceId(instanceId).singleResult();
      BpmnModel bpmnModel = repositoryService.getBpmnModel(instance.getProcessDefinitionId());
      Process process = bpmnModel.getProcesses().get(0);
      // Get all process elements, including sequences, events, activities, etc.
      Collection<FlowElement> flowElements = process.getFlowElements();
      Map<String, String> sequenceFlowMap = Maps.newHashMap();
      flowElements.forEach(e -> {
        
        if (e instanceof SequenceFlow) {
          SequenceFlow sequenceFlow = (SequenceFlow) e;
          String sourceRef = sequenceFlow.getSourceRef();
          String targetRef = sequenceFlow.getTargetRef();
          sequenceFlowMap.put(sourceRef + targetRef, sequenceFlow.getId());
        }
      });

      // Get all historical Activities, i.e. those that have been executed and those that are currently being executed
      List<HistoricActivityInstance> actList = historyService.createHistoricActivityInstanceQuery()
          .processInstanceId(instanceId)
          .list();

      // Each history Activity is combined two by two
      Set<String> actPairSet = new HashSet<>();
      for (HistoricActivityInstance actA : actList) {
        for (HistoricActivityInstance actB : actList) {
          if (actA != actB) {
            actPairSet.add(actA.getActivityId() + actB.getActivityId());
          }
        }
      }

      // Highlight Link ID 
      Set<String> highSequenceSet = Sets.newHashSet();
      actPairSet.forEach(actPair -> {
        logger.info("actPair:{}, seq:{}", actPair, sequenceFlowMap.get(actPair));
        highSequenceSet.add(sequenceFlowMap.get(actPair));
        logger.info("{}",highSequenceSet.toString());
      });

      // Get the completed Activity
      List<HistoricActivityInstance> finishedActList = historyService
          .createHistoricActivityInstanceQuery()
          .processInstanceId(instanceId)
          .finished()
          .list();

      // Highlight the completed Activity
      Set<String> highActSet = Sets.newHashSet();
      finishedActList.forEach(point -> highActSet.add(point.getActivityId()));

      // Get the pending highlighted node, i.e. the currently executing node
      List<HistoricActivityInstance> unfinishedActList = historyService
          .createHistoricActivityInstanceQuery()
          .processInstanceId(instanceId)
          .unfinished()
          .list();
      Set<String> unfinishedPointSet = Sets.newHashSet();
      unfinishedActList.forEach(point -> unfinishedPointSet.add(point.getActivityId()));

      ...
      return AjaxResponse.ajax(ResponseCode.SUCCESS.getCode(),
          ResponseCode.SUCCESS.getDesc(),
          null);
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResponse.ajax(ResponseCode.ERROR.getCode(),
          "highlight failure",
          e.toString());
    }
  }

Please see this piece of code:

      // Highlight Link ID 
      Set<String> highSequenceSet = Sets.newHashSet();
      actPairSet.forEach(actPair -> {
        logger.info("actPair:{}, seq:{}", actPair, sequenceFlowMap.get(actPair));
        highSequenceSet.add(sequenceFlowMap.get(actPair));
        logger.info("{}",highSequenceSet.toString());
      });

It was expected to get 2 elements in the highSequenceSet, but it got 3, with a unexpected null.
The log printed in the console was:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Why is the first null added to the HashSet but not the rest?

>Solution :

Why is the first null added to the HashSet but not the rest?

HashSet implements the Set interface, duplicate values are not allowed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading