- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正致力于动态创建一些 grails 域对象,然后将它们添加到另一个 grails 域对象中声明的 SortedSet。我已经创建了一个 Project 类,填充了它的值,并检查以确保它是有效的。它是有效的,所以我想将此项目添加到员工。
我的代码基本上是这样的
Employee employee = Employee.get(session.empid)
...
//populate some Project objects
...
//add projects to employee
employee.addToProjects(project)
这里可能出了什么问题?如果我执行 project.validate(),然后检查错误,唯一的错误是该项目没有与之关联的有效员工 - 但一旦我执行 employee.addToProjects,它就会消失。 Employee 有许多 Project 对象,它是这样声明的:
class Employee implements Comparable
{
static hasMany = [projects:Project]
static constraints =
{
}
static mapping = {
projects cascade:"all,delete-orphan", lazy:false
}
SortedSet<Project> projects = new TreeSet<Project>();
}
public class Project implements Comparable
{
static belongsTo = [employee:Employee]
static hasMany = [roles:Role]
static mapping = {
roles lazy:false, cascade:"all,delete-orphan"
}
@XmlElement
List<Role> roles = new ArrayList<Role>();
/*
* return sorted list. overwriting default getter was causing error upon saving multiple roles.
*
*/
def List getSortedRoles(){
Collections.sort(roles, new RoleComparator());
return roles;
}
String toString()
{
return name
}
// compare by latest date of roles, then by name + id
//if this is too intrusive, implement comparator with this logic and sort on rendering page
int compareTo(obj) {
if(obj == null){
return 1;
}
def myMaxRole = findMaxRole(roles);
def rhsMaxRole = findMaxRole(obj.roles);
def rcomparator = new RoleComparator();
System.out.println(myMaxRole.title + " " + rhsMaxRole.title + " " + rcomparator.compare(myMaxRole, rhsMaxRole));
return rcomparator.compare(myMaxRole, rhsMaxRole);
}
def List getExpandableRoleList()
{
return LazyList.decorate(roles, FactoryUtils.instantiateFactory(Role.class));
}
def setExpandableRoleList(List l)
{
return roles = l;
}
def Role findMaxRole(roles){
RoleComparator rc = new RoleComparator();
Role maxRole = roles.first();
for(role in roles){
if(rc.compare(maxRole, role) > 0){
maxRole = role;
}
}
return maxRole;
}
public class Role implements Comparable
{
static belongsTo = [project:Project]
static hasMany = [roleSkills:RoleSkill,roleTools:RoleTool]
static mapping = {
duties type:"text"
roleSkills cascade:"all,delete-orphan", lazy:false
roleTools cascade:"all,delete-orphan", lazy:false
}
static contraints = {
endDate(nullable: true)
}
boolean _deleted
static transients = ['_deleted']
@XmlElement
String title = ""
@XmlElement
String duties = ""
@XmlElement
int levelOfEffort
@XmlElement
Date startDate = new Date()
@XmlElement
Date endDate = new Date()
@XmlElement
Date lastModified = new Date()
@XmlElement
LocationType locationType = new LocationType(type: "Unknown")
@XmlElement
String rank
@XmlElement
List<RoleSkill> roleSkills = new ArrayList<RoleSkill>()
@XmlElement
List<RoleTool> roleTools = new ArrayList<RoleTool>()
String toString()
{
return title;
}
int compareTo(obj) {
return title.compareTo(obj.title)
}
def skills() {
return roleSkills.collect{it.skill}
}
def tools() {
return roleTools.collect{it.tool}
}
}
最佳答案
让 [].first() 从空列表中抛出“java.util.NoSuchElementException:无法访问 first() 元素”在我看来似乎“不合常规”。
我使用 groovy 的安全遵从运算符 (?) 来避免 NPE/NoSuchElementException
def list=[]
println list[0] //returns null
println list.first() //NoSuchElementException
println list?.first() //NoSuchElementException. Would prefer it to return null.
def list2=[null,3,6]
println list2.first() // returns null
println list2[0] //returns null
.first() 是唯一抛出异常的常规列表方法,这似乎令人沮丧。还有其他人经历过吗?
应更改文档以阐明这一点。list[0] - 如果找不到元素则返回 null。不区分空列表和第一个元素恰好为 null 的情况。
.first() 只在有第一个元素时返回第一个元素,否则抛出NoSuchElementException
关于java - 使用 Grails SortedSet 时无法访问空列表中的 first() 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3807280/
有人能解释一下为什么这段代码编译并运行良好,尽管 SortedSet是接口(interface)而不是具体类: public static void main(String[] args) {
鉴于以下 Scala 2.9.2 代码: 更新了非工作示例 import collection.immutable.SortedSet case class Bar(s: String) trait
我有一个对象 Test,它有两个属性:double x 和 double y。我想将这些对象添加到 SortedSet 中,使集合在 Test 的 x 上按 ASC 顺序排序。如果 Test 的两个实
我正在考虑用 SortedSet 替换 HashSet,因为它更适合我存储的数据。 但是,到目前为止我看到的所有示例都与存储简单对象有关 - int、字符串等。 我想为具有许多属性的自定义类实现此功能
我想知道如何更改 SortedSet 确定两个对象是否相等的方式。 我有SortedSet>(new Helpers.EdgeDistanceComparer())比较器方法是: public cla
我有一个类的实例,我想按特定顺序对其进行排序,但也能够使用不同的标准判断一个实例是否存在于集合中。示例: public class Foo { int x; int y; pu
我需要将 sourceList 中的对象添加到一个集合中,该集合在我们将对象添加到集合中时对集合进行排序。我正在考虑使用TreeSet . TreeSet bookSet 根据某些条件,我需要获取bo
尝试如下所示编写我的类会出现编译错误 public class CustomTreeSet> implements SortedSet> { } 错误: Syntax error on token "
以下代码创建了一个排序集,它按其值而不是键进行排序。 vertexRank 是一个对象,负责获取值。一切正常,除了代码:vertexCentralities.addAll(vMap.entrySet(
如 SortedSet 中所述 Represents a collection of objects that is maintained in sorted order SortedSet 中的“有
来自 SortedSet文档: several methods return subsets with restricted ranges. Such ranges are half-open, th
假设一个应用程序产生了一系列 HashMap数据结构,每个包含几十到几百个Comparable MyClass 类型的对象,需要以单个形式结束并排序 Collection . 此功能的两个可能实现返回
我试图使用排序集解决运行中值问题(在 hackerrank 上)。只有它的元素没有正确排序。 在此处查看实际效果:http://rextester.com/NGBN25779 public class
我想使用一个排序的集合,但我可以通过索引访问其中的元素,即我想要一些同时具有 Set 和 List 特征的东西。 Java.util.TreeSet 非常接近我的需要,但不允许通过索引访问。 我可以想
考虑这个类 public class A { float Order string Name public A(float order, string name)
在 SortedSet 的 Julia 文档中,有一个对“排序对象”的引用,可以在构造函数中使用。我正在做一个项目,我需要在一组结构上实现自定义排序。我想为此使用仿函数,因为我需要额外的状态来进行比较
我正在尝试解决 LeetCode https://leetcode.com/problems/nth-magical-number/ 的问题。我可以提交我的解决方案,但我想加快速度,我想这样做的方法之
这更多的是出于好奇,因为我从未注意到性能问题。假定设置大小介于 1-1000 之间。这是一个案例: private static SortedSet sortAuthorities( fina
在 SortedSet 的 Julia 文档中,有一个对“排序对象”的引用,可以在构造函数中使用。我正在做一个项目,我需要在一组结构上实现自定义排序。我想为此使用仿函数,因为我需要额外的状态来进行比较
我正在开发一个文字游戏,它的方法之一是打乱方法,它应该采用 String s,然后使用 Collections.shuffle(ListOfSChars); 对其进行洗牌;并检查 scramble 是
我是一名优秀的程序员,十分优秀!