从树集转换为抽象列表

时间:2017-01-19 13:46:16

标签: java sorting

我有一个名为actionEnt的类,它是一个asbtract,它类似于以下内容:

public abstract class ActionEnt implements Serializable {

    protected RaceGroup r;
    protected int t1;
    protected int t2;
    protected String name;

    public RaceGroup getRaceGroup() {
        return r;
    }

    public void setRacingTeam(RacingTeam rt) {
        r = rt;
    }

    public String getName() {
        return name;
    }

    public void setName(String n) {
        name = n;
    }

    // return TimeDriver1
    public int getT1() {
        return t1;
    }

    public void setTimeDriver1(int t) {
        t1 = t;
    }

    // return TimeDriver2
    public int getT2() {
        return t;
    }

    public void setT2(int t) {
        t2 = t;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof ActionEnt) {
          ActionEnt other = (ActionEnt) o;
            return this.r.equals(other.r()) && this.name.equals(other.getName());
        }
        return false;
    }

}

在另一个课程中,我有一个方法可以确定哪支球队在什么时间赢了。

为了完成这项工作,我必须将该列表转换为使用此方法的树集列表:

  public static TreeSet<Standing> winnerIs(List<ActionEnt> hp) {
    int size = hp.size();
    ArrayList<ActionEnt> listofWinner = new ArrayList<ActionEnt>();
    Map<String, Integer> map = new HashMap<String, Integer>();
    Map<String, ArrayList<Integer>> map2 = new HashMap<String, ArrayList<Integer>>();
    Map<Integer, Integer> timeOfDrivers = new HashMap<Integer, Integer>();
    ArrayList<Integer> driverTimes = new ArrayList<Integer>();
    TreeSet<Standing> standingSet, treereverse;

    ArrayList<Integer> timeFor = new ArrayList<Integer>();
    for (int i = 0; i < size; i++) {
      driverTimes = new ArrayList<Integer>();
      listofWinner.add(hp.get(i));
      String name = hp.get(i).getName();
      map.put(name, Math.max(listofWinner.get(i).getTimeDriver1(), listofWinner.get(i).getTimeDriver2()));
      timeOfDrivers.put(hp.get(i).getTimeDriver1(), hp.get(i).getTimeDriver2());
      driverTimes.add(hp.get(i).getTimeDriver1());
      driverTimes.add(hp.get(i).getTimeDriver2());
      map2.put(hp.get(i).getName(), driverTimes);
    }

    for (ArrayList<Integer> time : map2.values()) {
      timeFor.addAll(time);
    }
    Collections.sort(timeFor);
    Collections.reverse(timeFor);
    standingSet = new TreeSet<Standing>();
    treereverse = new TreeSet<Standing>();

    for (String team : map2.keySet()) {
      standingSet.add(new Standing(team, map2.get(team).get(0)));
      standingSet.add(new Standing(team, map2.get(team).get(1)));
    }
    treereverse = (TreeSet<Standing>) standingSet.descendingSet();
    return treereverse;
  }

现在,当我尝试为此进行单元测试时,我正确地列出了列表,我收到以下错误:

java.util.ArrayList<[team1 -> 37987932, team2 -> 509360, team3 -> 439900, team4 -> 24128, team2 -> 13810, team4 -> 8584, team3 -> 1907, team1 -> 946]> but was:
java.util.ArrayList<[team1 -> 37987932, team2 -> 509360, team3 -> 439900, team4 -> 24128, team2 -> 13810, team4 -> 8584, team3 -> 1907, team1 -> 946]>

为什么这样我真的看不出差异!!

2 个答案:

答案 0 :(得分:0)

Set<Standing> yourSet = ....
List<ActionEnt> yourList = yourSet.stream()
                                  .map(yourTransformationFunction()
                                  .collect(Collectors.toList());

然后,您只需要提供一个能够将Standing - 类型转换为ActionEnt类型的函数,例如

Function<Standing, ActionEnt> yourTransformationFunction() {
  return standing -> {
    // actually do a real transformation here ;-) the following should get you started
    ActionEnt newActionEnt = new ActionEnt(); 
    newActionEnt.set...(standing.get...);
    ...
    return newActionEnt;
  }
}

答案 1 :(得分:0)

我能够做到以下工作:

public List<ActionEnt> showWinner(TreeSet<Standing> t) throws IOException {
    (1) List yourList = new ArrayList<ActionEnt>();
    (2) yourList.addAll(t);
    (3) return yourList;
  }

它返回正确的列表,其中包含正确的顺序和正确的类型:

我怎么总是有这个警告:

(1)List is a raw type. References to generic type List<E> should be parameterized
(2)Type safety: The method addAll(Collection) belongs to the raw type List. References to generic type List<E> should be parameterized
(3)Type safety: The expression of type List needs unchecked conversion to conform to List<ActionEnt>

这是一个很好的解决方法吗?