gpt4 book ai didi

java - Java 8 中 Collections.sort 的问题

转载 作者:搜寻专家 更新时间:2023-10-31 08:11:57 26 4
gpt4 key购买 nike

 @Entity
@NamedQueries({
@NamedQuery(
name = "FolderNode.findByName",
query = "SELECT f FROM FolderNode f WHERE f.name = :name AND f.parentNode = :parentNode"),
@NamedQuery(
name = "FolderNode.findRootNodeByName",
query = "SELECT f FROM FolderNode f WHERE f.name = :name AND f.parentNode is null")
})
public class FolderNode extends InstructorTreeNode {
public FolderNode() {
super();
}

public FolderNode(String name) {
this();
setName(name);
}

public FolderNode(int sortOrder, String name) {
this(name);
this.sortOrder = sortOrder;
}

public FolderNode(int sortOrder, String name, EmployeeState status) {
this(sortOrder, name);
this.status = status;
}

public static FolderNode addWaitingListNode(String name) {
EntityManager em = getDao().getEntityManager();
em.getTransaction().begin();
FolderNode waitingListNode = getWaitingListFolder();
FolderNode folderNode = new FolderNode(0, name);
waitingListNode.addChild(folderNode);
em.merge(waitingListNode);
em.getTransaction().commit();
em.close();
return folderNode;
}

public static void addWaitingListStudent(String waitingList, Student s) {
EntityManager em = FolderNode.getDao().getEntityManager();
em.getTransaction().begin();
FolderNode waitingListsNode = getWaitingListFolder();
FolderNode waitingListNode = getDao().findFolderNodeByName(waitingListsNode, waitingList);
waitingListNode.addChild(new EmployeeLeaf(s.getInmate()));
em.merge(waitingListNode);
em.getTransaction().commit();
em.close();
}

public static FolderNode getAMClassFolder() {
return getDao().findFolderNodeByName(getStudentsFolder(), "AM Class");
}

public static FolderNode getAttendanceFolder() {
return getDao().findFolderNodeByName(getRootFolder(), "Employee Attendance");
}

public static FolderNode getFormerParaprosFolder() {
return getDao().findFolderNodeByName(getParaprosFolder(), "Former");
}

public static FolderNode getFormerStudentsFolder() {
return getDao().findFolderNodeByName(getStudentsFolder(), "Former");
}

public static FolderNode getPMClassFolder() {
return getDao().findFolderNodeByName(getStudentsFolder(), "PM Class");
}

public static FolderNode getParaprosFolder() {
return getDao().findFolderNodeByName(getRootFolder(), "Parapros");
}

public static FolderNode getPendingStudentsFolder() {
return getDao().findFolderNodeByName(getRootFolder(), "Pending Students");
}

public static FolderNode getRootFolder() {
return getDao().findFolderNodeByName(null, EducationPreferences.getInstructor().getInstructorName());
}

public static FolderNode getStudentsFolder() {
return getDao().findFolderNodeByName(getRootFolder(), "Students");
}

public static FolderNode getWaitingListFolder(String name) {
FolderNode waitingListsNode = getWaitingListFolder();
return getDao().findFolderNodeByName(waitingListsNode, name);
}

public static FolderNode getWaitingListFolder() {
return getDao().findFolderNodeByName(getRootFolder(), "Waiting List");
}

public static void setClassFolder(Student aStudent, EntityManager entityManager) {
EntityManager em = entityManager;
if (entityManager == null) {
em = FolderNode.getDao().getEntityManager();
em.getTransaction().begin();
}

EmployeeLeaf leaf = EmployeeLeaf.findActiveStudentLeaf(aStudent);
FolderNode node = aStudent.getShift() == Shift.AM ? getAMClassFolder() : getPMClassFolder();
leaf.setParentNode(node);
em.merge(leaf);
GlobalEntityMethods.updateHistory(leaf);
if (entityManager == null) {
em.getTransaction().commit();
em.close();
}
}

public static void transferWaitingListStudent(String currentFolder, String toFolder, Student student) {
EntityManager em = FolderNode.getDao().getEntityManager();
em.getTransaction().begin();
FolderNode waitingListsNode = getWaitingListFolder();
FolderNode currentWaitingListNode = getDao().findFolderNodeByName(waitingListsNode, currentFolder);
EmployeeLeaf employeeLeaf = EmployeeLeaf.getDao().findWaitingListLeafByInmate(student.getInmate());
currentWaitingListNode.removeChild(employeeLeaf);
FolderNode toWaitingListNode = getDao().findFolderNodeByName(waitingListsNode, toFolder);
toWaitingListNode.addChild(employeeLeaf);
em.merge(currentWaitingListNode);
em.merge(toWaitingListNode);
em.getTransaction().commit();
em.close();
}

public void addChild(InstructorTreeNode node) {
childNodes.add(node);
node.setParentNode(this);
}

public List<InstructorTreeNode> getChildNodes() {
Collections.sort(childNodes);
return childNodes;
}

@Override
public Set<Inmate> getInmates() {
Set<Inmate> inmateSet = new HashSet<> (50);
for (InstructorTreeNode node: getChildNodes()) {
inmateSet.addAll(node.getInmates());
}
return inmateSet;
}

public int getSortOrder() {
return sortOrder;
}

public EmployeeState getStatus() {
return status;
}

@Override
public List<InstructorTreeNode> getTree() {
List <InstructorTreeNode> result = new ArrayList<> (25);
for (InstructorTreeNode childNode: getChildNodes()) {
if (childNode instanceof FolderNode) {
result.add(childNode);
}
result.addAll(childNode.getTree());
}
return result;
}

@Override
public JPanel getView(EmployeeViewController controller) {
if ("Employee Attendance".equals(getName())) {
return new AttendanceView();
} else if ("Waiting List".equals(getName())) {
return new AllWaitingListsPanel(controller);
} else if (getParentNode().getName().equals("Waiting List")) {
return new WaitingListPanel(controller);
} else if ("Pending Students".equals(getName())) {
return new PendingStudentsPanel(controller);
} else if ("Students".equals(getName())) {
return new AllStudentsPanel(controller);
} else if ("AM Class".equals(getName())) {
return new AllStudentsPanel(controller, Shift.AM);
} else if ("PM Class".equals(getName())) {
return new AllStudentsPanel(controller, Shift.PM);
} else if (getParentNode().getName().equals("Students") && "Former".equals(getName())) {
return new FormerStudentsPanel(controller);
} else if ("Parapros".equals(getName())) {
return new AllParaprosPanel(controller);
} else if (getParentNode().getName().equals("Parapros") && "Former".equals(getName())) {
return new FormerParaprosPanel(controller);
}
throw new UnsupportedOperationException("unknown folder");
}

public void removeChild(InstructorTreeNode node) {
childNodes.remove(node);
node.setParentNode(null);
}

public void removeEmployeeLeaf(Inmate inmate) {
for (InstructorTreeNode node: childNodes) {
if (node instanceof EmployeeLeaf) {
EmployeeLeaf employeeLeaf = (EmployeeLeaf) node;
if (employeeLeaf.getInmate().equals(inmate)) {
childNodes.remove(employeeLeaf);
break;
}
}
}
}

public void setChildNodes(List<InstructorTreeNode> childNodes) {
this.childNodes = childNodes;
}

public void setSortOrder(int sortOrder) {
this.sortOrder = sortOrder;
}

public void setStatus(EmployeeState status) {
this.status = status;
}

@OneToMany(mappedBy = "parentNode", cascade = CascadeType.ALL, orphanRemoval = true)
private List<InstructorTreeNode> childNodes;

private int sortOrder;

@Enumerated(EnumType.STRING)
private EmployeeState status;
}


@Entity
@Table(catalog = "education", name = "instructortreenode", uniqueConstraints = @UniqueConstraint(columnNames = {
"PARENTNODE_ID", "NAME"
}))
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class InstructorTreeNode implements Comparable<InstructorTreeNode> {
public InstructorTreeNode() {
super();
}

public static InstructorTreeNodeDAO getDao() {
return dao;
}

@Override
public int compareTo(InstructorTreeNode o) {
if (o instanceof FolderNode && this instanceof FolderNode) {
FolderNode thisFolder = (FolderNode) this;
FolderNode otherFolder = (FolderNode) o;
if (thisFolder.getSortOrder() != otherFolder.getSortOrder()) {
return thisFolder.getSortOrder() - otherFolder.getSortOrder();
} else {
return thisFolder.getName().compareToIgnoreCase(otherFolder.getName());
}
} else if (o instanceof EmployeeLeaf && this instanceof EmployeeLeaf) {
return getName().compareToIgnoreCase(((InstructorTreeNode) o).getName());
}
return (o instanceof FolderNode) ? -1 : +1;
}

public int getCount() {
return getTree().size();
}

public abstract Set<Inmate> getInmates();

public String getName() {
return name;
}

public FolderNode getParentNode() {
return parentNode;
}

public abstract List<InstructorTreeNode> getTree();

public abstract JPanel getView(EmployeeViewController theController);

public void setName(String name) {
this.name = name;
}

public void setParentNode(FolderNode parentNode) {
this.parentNode = parentNode;
}

@Override
public String toString() {
return name;
}

private static final InstructorTreeNodeDAO dao = new InstructorTreeNodeDAO();
private String name;

@ManyToOne
private FolderNode parentNode;
}

这是我的问题:Collections.sort 行在 Java 8u5 及之前的版本中工作得很好,但是在 Java 8u20 中,他们似乎已经更改了 Collections.sort 的代码它不再使用自然顺序,即使您指定一个比较器。

我应该使用另一种方法对我的列表进行排序吗?集合.排序。

任何帮助将不胜感激,因为这让我发疯。

我忘了说这段代码没有使用指定的比较器,但根据文档它应该使用 CompareTo,如果你的类实现了 Comparable,这就是我正在使用的。我还尝试指定一个比较器,但它也不起作用。

最佳答案

由于 Collections.sort 现在委托(delegate)给 List.sort,因此实际的 List 实现会产生影响。 ArrayListVector 等实现利用机会以比默认实现更有效的方式实现 List.sort,因为它们直接传递其内部数组到 Arrays.sort 省略默认实现的复制步骤。

除非程序员使用子类化实现(而不是使用委托(delegate))覆盖方法的反模式来实现矛盾的行为,否则这将无缝地工作。这些来自 EclipseLink/JPA 的惰性填充列表 are known to have problems with this因为他们试图在继续之前拦截每个读取方法来填充列表,但错过了新的 sort 方法。如果在调用 sort 时列表尚未填充,sort 将看到一个空列表状态。

在您的代码中,没有指示列表确实来自何处以及它具有哪个实际实现类,但由于我看到了很多看起来很熟悉的注释,我猜,您正在使用这样的一个框架……

关于java - Java 8 中 Collections.sort 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27050398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com