Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:37:07+00:00 2026-06-10T14:37:07+00:00

I am new to groovy Lists, and I have list as shown below: Class

  • 0

I am new to groovy Lists, and I have list as shown below:

Class File{
 String name 
 int type
 int revision
}

def fileList = [File1, File2, File3,....]

I want the fileList to have the latest files

  1. It should not have the items of same type
  2. It should have the highest revision files means, if two or more files have the same type then the list must have the single file with the highest revision.

How do I do this in Groovy?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:37:09+00:00Added an answer on June 10, 2026 at 2:37 pm

    You can use Collection#groupBy to create a map with an entry for each unique file name. You can then use Map#collect to iterate over the contents of this map and create the list you want. The map’s values will be a list of instances of File, so Collection#max will let you search for the one with the highest revision number.

    class File {
        String name
        int type
        int revision
    
        String toString() { "File(name: $name; type: $type; revision: $revision)" }
    }
    
    final files = [
        new File(name: 'foo', type: 0, revision: 0),
        new File(name: 'bar', type: 0, revision: 0),
        new File(name: 'bar', type: 0, revision: 1),
        new File(name: 'baz', type: 0, revision: 0),
        new File(name: 'baz', type: 0, revision: 1),
        new File(name: 'baz', type: 1, revision: 1),
    ]
    
    final result = files.groupBy { it.name }
                 . collect { name, revisions -> revisions.max { it.revision } }
    

    I’m not sure what you meant when you said that the list should not have items of the same type. You’ll notice that if there are two instances of File with the same name and revision number but different types, this solution picks one arbitrarily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Groovy, how do I extract a new list from the following: def people
String sharedLocation = \\\\hostname new File(sharedLocation).list() returns null. exists() returns false as well. I
Having: class Task { Integer code String name } def classDefinition = package untitled24
I have a simple groovy script that generates xml def builder = new groovy.xml.StreamingMarkupBuilder()
I have the below groovy code import java.util.regex.Matcher; import java.util.regex.Pattern; String myInput=License_All (12313) String
I'm new to groovy, and I've used the ExcelBuilder code referenced below to iterate
I am executing groovy script in java: final GroovyClassLoader classLoader = new GroovyClassLoader(); Class
I have a project which uses groovy 1.7.5 and grails 1.3.4. but my new
I have a very simple Groovy class where I'm just trying to select a
I'm having a problem while using constructors with a Groovy class. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.