How To Avoid And Resolve ConcurrentModificationException in Java And Groovy
Posted By : Ankit Nigam | 03-Feb-2014
Class User{ String username String password static hasMany = [questions : Question] static constraints = { username nullable: false password nullable: false } } Class Question{ String content boolean isValid = false static belongsTo = [user : User] static constraints = { content nullable: false } }
Now In Groovy If we want to delete all the Questions which are having isValid set to false then everyone would have simply written
def currentUser = springSecurityService.currrentUser def questionList = Question.findAllByUserAndIsValid(currentUser,false) questionList.each{ currentUser.removeFromQuestions(it) it.delete(flush : true) }
def currentUser = springSecurityService.currrentUser def questionList = Question.findAllByUserAndIsValid(currentUser,false) Question[] ques = questionList.toArray(new Question[questionList.size()]); for(int i=0;i< ques.length ;i++){ currentUser.removeFromQuestions(ques[i]) ques[i].delete(flush : true) }
For simple use in Java , you can convert the List into Array using the following code :
List<Type> list = ..; Type[] array = list.toArray(new Type[list.size()]);
Now enjoy the power of Java & Groovy
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankit Nigam
Ankit has worked on development of various SaaS applications using Grails technologies. He has good exposure on FFMPEG and video content management applications. Ankit likes mobile gaming and going out with friends.