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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:05:01+00:00 2026-05-29T09:05:01+00:00

Can I write a generic method to trim all strings within an complex object

  • 0

Can I write a generic method to trim all strings within an complex object (object containing other objects)? Should java reflection api be used to achieve this?Thanks.

I have provided a sample below. However in reality there could be multiple objects within objects. Each object might contain a collection of String or collection of other objects which may contain String. Is there a way to trim the Strings – ones directly with the objects and ones within collection.

public class School{
   private List<Course> courses;
   private List<Student> students;       
   // Getters and Setters
}

public class Course{
   private String name;
   private String xxx;
   private String yyy;
   private List<String> zzzList;
}

public class Student{
   private Map<String,String> xxx;
   private List<Course> courseList;
}
  • 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-29T09:05:02+00:00Added an answer on May 29, 2026 at 9:05 am

    Yes, reflection is the way. Basically, you need to:

    • get the class of the top level object (with [object].getClass())

    • get all the fields of the object (with clazz.getFields() – beware, it works only with public fields)

    • check if the field is String (either get field.getType() and check it’s a string, or do a field.get(the object) and a instanceof String)

    • if it’s the case, replace the string in the object with the trimmed one, using field.set([your object],[trimmed string])

    • if the field is an object but not a string, call your method recursively

    That will do the trick.

    —- just seen your update

    Trimming strings in collection will be more tricky, since the strings are not exposed as public fields of the collection (List for example).

    You will need something more clever, that will check if an object is an instance of List, or Map, or etc… (or a derived class!).
    Main problem is also that java generics are done with erasing type at compile type. So you cannot know that your field is List[String] or List[Integer] or whatever. Every List[?] becomes List.

    Still you can try to do it like that:

    • if field type is List
    • iterate through the list values
    • if a value is instanceof String, you have to remove it from the list and insert in place the trimmed version
    • if a value is an object, there you go again recursively with your method.

    Not very interesting in real life samples, but more on a library side maybe.
    Long way to go though!

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

Sidebar

Related Questions

How can I write a generic method that can take a Nullable object to
I'm trying to write a generic method that can be used to deserialize xml
I'm trying to write a generic method to serialize an object that inherits from
Is it possible to write an inline generic method? For example, how can I
How can I write a generic method parameter at my interface method? public <T
I need to write java generic method that gets no parameter and returns a
I'd like to learn how to better write a more generic method which can
I can write an assertion message one of two ways. Stating success: assertEquals( objects
I want to write a generic helper method: def using(closeable: [B has close() method],
I would like some feedback on how we can best write a generic function

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.