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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:13:16+00:00 2026-05-14T07:13:16+00:00

I wanna implement a javascript like method in java , is this possible ?

  • 0

I wanna implement a javascript like method in java , is this possible ?

Say , I have a Person class :

public class Person {
 private String name ;
 private int age ;
 // constructor ,accessors are omitted
}

And a list with Person objects:

Person p1 = new Person("Jenny",20);
Person p2 = new Person("Kate",22);
List<Person> pList = Arrays.asList(new Person[] {p1,p2});

I wanna implement a method like this:

modList(pList,new Operation (Person p) {
  incrementAge(Person p) { p.setAge(p.getAge() + 1)};
});

modList receives two params , one is a list , the other is the “Function object”, it loops the list ,and apply this function to every element in the list. In functional programming language,this is easy , I don’t know how java do this? Maybe could be done through dynamic proxy, does that have a performance trade off compares to native for loop ?

  • 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-14T07:13:16+00:00Added an answer on May 14, 2026 at 7:13 am

    You can do it with an interface and an anonymous inner class implementing it, like

    Person p1 = new Person("Jenny",20);
    Person p2 = new Person("Kate",22);
    List<Person> pList = Arrays.asList(p1, p2);
    
    interface Operation {
      abstract void execute(Person p);
    }
    
    public void modList(List<Person> list, Operation op) {
      for (Person p : list)
        op.execute(p);
    }
    
    modList(pList, new Operation {
      public void execute(Person p) { p.setAge(p.getAge() + 1)};
    });
    

    Note that with varargs in Java5, the call to Arrays.asList can be simplified as shown above.

    Update: A generified version of the above:

    interface Operation<E> {
      abstract void execute(E elem);
    }
    
    public <E> void modList(List<? extends E> list, Operation<E> op) {
      for (E elem : list)
        op.execute(elem);
    }
    
    modList(pList, new Operation<Person>() {
        public void execute(Person p) { p.setAge(p.getAge() + 1); }
    });
    

    Note that with the above definition of modList, you can execute an Operation<Person> on e.g. a List<Student> too (provided Student is a subclass of Person). A plain List<E> parameter type would not allow this.

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

Sidebar

Related Questions

How to implement like JAVA ‘Protect’ function in Object-C?I wanna access father class method
i have a div that it have image content .i wanna implement animation when
I wanna implement a download-and-install mechanism. I have a source link where my app
I wanna implement this using jquery instead of inline but Its not working, inline
I have this function I have to implement: protected override ValidationResult IsValid( Object value,
I wanna implement something like in facebook: after left click on photo, it is
i wanna implement a bookcase like as ibook's bookcase.who can tell me how to
is to possible to attach custom events to the document's body? I wanna implement
I wanna implement url rewriting so that, for example, all german pages have a
I have a big problem with a java program. I try to implement 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.