如何只在List中保留唯一值? - java

时间:2018-05-24 21:52:34

标签: java

基本上,我有一个存储两个值int keyScoreList<Integer> moves的模型。在主类中,我有一个从计算方法生成的模型列表。

我想做的是:

  1. 如果keyScore等于
  2. ,则连接List<Integer>会移动
  3. 删除重复项
  4. 我尝试在HashSet移动时使用List<Integer>,当我找到相同的keyScore时,但我最终得到了我的模型的副本。

    private class HeuristicResult {
            private int keyResult;
            private List<Integer> moves;
    
            private HeuristicResult(int keyResult, List<Integer> moves) {
                this.keyResult = keyResult;
                this.moves = moves;
            }
    
            private int getKeyResult(){
                return this.keyResult;
            }
    
            private List<Integer> getMoves(){
                return this.moves;
            }
    
            private void setMoves(List<Integer> moves){
                this.moves = moves;
            }
    
            @Override
            public String toString() {
                return String.format("%s : %s", this.keyResult, this.moves);
            }
        }
    private List<HeuristicResult> concatHeuristicResults(List<HeuristicResult> heuristicResultsList){
                List<HeuristicResult> heuristicResults = heuristicResultsList;
                for(int i =0; i<heuristicResults.size()-2; i++){
                    int score = heuristicResults.get(i).getKeyResult();
                    for(int j = 0; j<heuristicResults.size()-1;j++){
                        if(score == heuristicResults.get(j).getKeyResult()){
                            heuristicResults.get(i).getMoves().addAll(heuristicResults.get(j).getMoves());
                            Set<Integer> temp = new HashSet<>(heuristicResults.get(i).getMoves());
                            heuristicResults.get(i).setMoves(new ArrayList<>(temp));
                        }
                    }
                }
                return heuristicResults;
            }  
    

    当我尝试连接时,这是我得到的输出:

    1 : [0, 1]
    0 : [0, 1, 3, 6, 7, 8]
    0 : [0, 1, 3, 6, 7, 8]
    -10 : [3]
    0 : [0, 1, 3, 6, 7, 8]
    0 : [0, 1, 3, 6, 7, 8]
    0 : [0, 1, 3, 6, 7, 8]
    0 : [0, 1, 3, 6, 7, 8]
    0 : [0, 1, 3, 6, 7, 8]
    -1 : [0, 1, 7, 8]
    0 : [0, 1, 3, 6, 7, 8]
    0 : [0, 1, 3, 6, 7, 8]
    0 : [0, 1, 3, 6, 7, 8]
    -1 : [0, 1, 7, 8]
    0 : [6]
    0 : [6]
    

1 个答案:

答案 0 :(得分:2)

试试这个:

class Upcoming_Training extends Component {
  constructor(props) {
    super(props);
    this.state = {
      inputValue: '',
    }
  }
  componentDidMount() {}
  showDate(date) {
    // Creates a more readable date
    if (date) {
      date = date.substring(0, date.indexOf("T"));
      let dateArr = date.split('-');
      return dateArr[1] + '/' + dateArr[2] + '/' + dateArr[0];
    }
  }
  filterCourses() {
    let courseRows = [];
    if (this.props.upcoming_training != []) {
      if (this.showDate) {
        let courseRows = this.props.upcoming_training.map(function (
          course, index) {
          return <tr>
                   <th><button className='btn btn-sm btn-primary'> More Info </button></th> {/*Will make this button do something later*/}
                   <td>{course.Course}</td>
                   <td> {(course.Name.includes("OL -") ? course.Name.slice(5) : course.Name)}</td>
                   <td>{this.showDate(course.Start)}</td>
                   <td>{this.showDate(course.End)}</td>
                   <td>{(course.IsOnline ? "Online" : "On-Site")}</td>
                 </tr>
        })
      }
      return courseRows;
    }
    return [];
  }

您正在尝试按层次合并。首先,您需要唯一的static Collection<HeuristicResult> concatenate(List<HeuristicResult> list) { HashMap<Integer, HeuristicResult> keys = new HashMap<>(); for (HeuristicResult x: list) { HeuristicResult hr = keys.get(x.keyResult); if (hr != null) { // Merge hr and x. Set<Integer> moves = new HashSet<>(); moves.addAll(hr.getMoves()); moves.addAll(x.getMoves()); hr.moves.clear(); hr.moves.addAll(moves); } else { // Create a new entry into our keys map if it doesn't exist. keys.put(x.keyResult, x); } } return keys.values(); } ,并且要为这些唯一的keyResult合并keyResult。这是两个合并级别。

movesHashMap - &gt; keyResult)仅保留唯一的HeuristicResult,并将其映射到列表中看到的第一个keyResult。然后在迭代期间,如果它再次找到相同的HeuristicResult,它将从地图中提取keyResult并在迭代中找到并合并它们。合并的moves将被放回列表中(首先清除它)。