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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:57:24+00:00 2026-06-08T02:57:24+00:00

Would anyone be so kind to post the equivalent Java code for a closure

  • 0

Would anyone be so kind to post the equivalent Java code for a closure like this one (obtained using C#) with anonymous inner classes?

    public static Func<int, int> IncrementByN()
    {
        int n = 0; // n is local to the method

        Func<int, int> increment = delegate(int x)
        {
            n++;
            return x + n;
        };

        return increment;
    }
static void Main(string[] args)
   {

        var v = IncrementByN();
        Console.WriteLine(v(5)); // output 6
        Console.WriteLine(v(6)); // output 8
    }

Furthermore, can anyone explain how partial applications can be obtained if lexical closures are available and viceversa? For this second question, C# would be appreciated but it’s your choice.
Thanks so much.

  • 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-08T02:57:26+00:00Added an answer on June 8, 2026 at 2:57 am

    There is no closure yet in Java. Lambda expressions are coming in java 8. However, the only issue with what you’re trying to translate is that it has state, which not something that lamba expressions will support i don’t think. Keep in mind, it’s really just a shorthand so that you can easily implement single method interfaces. You can however still simulate this I believe:

    final AtomicInteger n = new AtomicInteger(0);
    IncrementByN v = (int x) -> x + n.incrementAndGet();
    System.out.println(v.increment(5));
    System.out.println(v.increment(6));
    

    I have not tested this code though, it’s just meant as an example of what might possibly work in java 8.

    Think of the collections api. Let’s say they have this interface:

    public interface CollectionMapper<S,T> {
        public T map(S source);
    }
    

    And a method on java.util.Collection:

    public interface Collection<K> {
      public <T> Collection<T> map(CollectionMapper<K,T> mapper);
    }
    

    Now, let’s see that without closures:

    Collection<Long> mapped = coll.map(new CollectionMapper<Foo,Long>() {
        public Long map(Foo foo) {
           return foo.getLong();
        }
    }
    

    Why not just write this:

    Collection<Long> mapped = ...;
    for (Foo foo : coll) {
        mapped.add(foo.getLong());
    }
    

    Much more concise right?

    Now introduce lambdas:

    Collection<Long> mapped = coll.map( (Foo foo) -> foo.getLong() );
    

    See how much nicer the syntax is? And you can chain it too (we’ll assume there’s an interface to do filtering which which returns boolean values to determine whether to filter out a value or not):

     Collection<Long> mappedAndFiltered =
        coll.map( (Foo foo) -> foo.getLong() )
            .filter( (Long val) -> val.longValue() < 1000L );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would anyone offer tips, links, code snippets on how to browse to a file
Would anyone like to recommend free alternative to SharePoint Portal ? We want to
Would anyone happen to know a trick that will keep this MSBuild task from
Would anyone now why this script is disabling my nav in webkit browsers $(document).ready(function
Would anyone one know if Apple has restrictions on providing a user feedback/bug report
I have created a small game in Java and I would like to add
Would anyone be kind enough to dissect the merge! method? Its usage of conditions
Would anyone be kind enough to explain what infix, postfix and prefix notation is
Would anyone know why the bmlabel is not updated? and the log score shows
Would anyone please advise how in jade for nodejs I can truncate a string

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.