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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:42:03+00:00 2026-05-13T17:42:03+00:00

I have several objects that look like this: class PhoneNumber { String getNumber(); String

  • 0

I have several objects that look like this:

class PhoneNumber
{
   String   getNumber();
   String   getExtension();
   DateTime getLastCalled();
}
class Address
{
   String getCity();
   string getState();
   int    getZip();
}

I’d like to be able to take a List of any one of those objects and get a list of a particular property. This is a trivial operation if I were to write a function for each property, but as I understand it, it is bad practice to duplicate code so many times.

So I would like to be able to do something like this for any property in either object:

List<Address> AddressList = getAddresses();
List<String> CityList = getListOfProperties( AddressList, Address.getCity /*<--Instruction as to what property to populate the returned list with */ )

I’ve racked my brain trying to do this with generics. I’m not even sure that this is possible, but as I understand it, a lot of magical things can be done with the Java programming language.

  • 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-13T17:42:03+00:00Added an answer on May 13, 2026 at 5:42 pm

    You can do this with generics and a utility class to do the work. I like this a little bit better than using reflection because you get compile time checking that the property you’re retrieving exists (and isn’t misspelled etc.).

    First a class that does the actual conversion. Note the abstract “get” method which we’ll override in an anonymous inner class when we call getListOfProperties().

    public abstract class ListPropertyGetter<R,T>
    {
      /** Given a source object, returns some property of type R. */ 
      public abstract R get(T source);
    
      /** Given a list of objects, use the "get" method to retrieve a single property
          from every list item and return a List of those property values. */
      public final List<R> getListOfProperties(List<T> list)
      {
        List<R> ret = new ArrayList<R>(list.size());
        for(T source : list)
        {
          ret.add(get(source));
        }
        return ret;
      }
    }
    

    Then you use it like this. Not a pretty one liner, but compile time checking goodness:

    List<PhoneNumber> phoneNumbers = new ArrayList<PhoneNumber>();
    // add some PhoneNumbers to the list
    List<String> extensions =
      new ListPropertyGetter<String, PhoneNumber>()
      {
        @Override
        public String get(PhoneNumber source)
        {
          return source.getExtension();
        }
      }.getListOfProperties(phoneNumbers);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that is called regularly by several objects. I would like
I have several objects in C# that's roughly like this: A: { Id: 1,
Strange issue- I have a complex DTO with several child objects, declared like this:
I have several objects in my app that can become nil at some point
I have users that have several objects and can upload images for those objects.
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
I have several methods that populate a SQLCommand objects parameter collection from an object
I have a mutable array that is retained and storing several objects. At some
I'm trying to put together a form_tag that edits several Shift objects. I have
I have read in several places that it's possible to share the objects directory

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.