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 3302646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:52:25+00:00 2026-05-17T20:52:25+00:00

Ok, this should be easy… I’m new to groovy and I’m looking to implement

  • 0

Ok, this should be easy…

I’m new to groovy and I’m looking to implement the following logic:

def testFiles = findAllTestFiles();

So far, I’ve come up with the code below which successfully prints all files names. However, instead of printing, I just need to put them into a collection. Of course, I could do this the old java way: just instantiate a collection, add all the elements and return it. However, that wouldn’t teach me anything.

So how do you do this the cool, “Groovy” way?

static File[] findAllTestFiles() {
    def directory = new File("src/test/java");
    def closure = {File f -> if(f.name =~ /Test\.java$/) println f }
    directory.eachFileRecurse FileType.FILES, closure
    return null;
}

I’m looking to implement findAlltestFiles() in Groovy using as little code as possible while still being readable.

  • 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-05-17T20:52:26+00:00Added an answer on May 17, 2026 at 8:52 pm

    I’d try to avoid building the collection entirely. Using closures, you can separate the logic to select the files from what you actually want to do with them, like so:

    import groovy.io.FileType
    
    def withEachTestFile(Closure closure) {
        new File("src/test/java").eachFileRecurse(FileType.FILES) {
            if (it.name =~ /Test\.java$/) {
                closure.call(it)
            }
        }
    }
    

    Then if you want to do something on the test files, you can do it directly without building up a list in memory:

    withEachTestFile() { println it }
    

    or if you really want the list, you can easily generate it, using whatever collection makes sense:

    def files = []
    withEachTestFile() { files << it }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be easy, but I've managed to stump 2 people so far at
This should be easy, but since I'm new to programming, specially in javascript, I
This should be easy, but I'm looking to add an integer variable called rental_period
This should be easy... why is this not easy? I am looking to have
This should be easy for many of you, but for me it's just another
This should be easy, but I'm not sure how to best go about it.
This should be easy, just curious. I know httpd is the HTTP daemon, just
This should be easy (at least no one else seems to be having a
I think this should be easy, but it's evading me. I've got a many-to-many
OK, I'm feeling like this should be easy but am obviously missing something fundamental

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.