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

  • Home
  • SEARCH
  • 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 7544611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:42:58+00:00 2026-05-30T08:42:58+00:00

I would like to add a collection of objects to an arrayList ,only if

  • 0

I would like to add a collection of objects to an arrayList ,only if the particular attribute is not null.

I am thinking of extending the ArrayList and implementing the check inside the child class.

One alternate way is to check for the the attribute before putting it in a Arraylist, but that would mean , i will have to scatter the if checks every where if i need to add the objects to the arraylist based on the logic.

I would like to know your thoughts on it … on a second thought is it a overkill ?

  • 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-30T08:43:00+00:00Added an answer on May 30, 2026 at 8:43 am

    Decorator pattern

    I would actually recommend wrapping ArrayList using well-documented Decorator pattern. You simply wrap your ArrayList with another List implementation that delegates most of the methods but adds validation logic:

    public class ValidatingListDecorator extends AbstractList<MyBusinessObject>
    {
    
        private final List<MyBusinessObject> target;
    
        public ValidatingListDecorator(List<MyBusinessObject> target) {
            this.target = target;
        }
    
        @Override
        public MyBusinessObject set(int index, MyBusinessObject element)
        {
            validate(element);
            return target.set(index, element);
        }
    
        @Override
        public boolean add(MyBusinessObject o)
        {
            validate(o);
            return target.add(o);
        }
    
        //few more to implement
        
    }
    

    Advantages:

    • You can still access raw list without validation if you want (but you can restrict this)
    • Easier to stack different validations, turn them on and off selectively.
    • Promotes composition over inheritance as noted by @helios
    • Improves testability
    • Does not tie you to a specific List implementation, you can add validation to LinkedList or Hibernate-backed persistent lists. You can even think about generic Collection decorator to validate any collection.

    Implementation notes

    Despite the implementation remember there are quite a lot of methods you have to remember about while overriding: add(), addAll(), set(), subList() (?), etc.

    Also your object must be immutable, otherwise the user can add/set valid object and modify it afterwards to violate the contract.

    Good OO design

    Finaly I wrote:

    validate(element)
    

    but consider:

    element.validate()
    

    which is a better design.

    Stacking validations

    As noted before if you want to stack validations, validating each proprty/apsect in a single, separate class, consider the following idiom:

    public abstract class ValidatingListDecorator extends AbstractList<MyBusinessObject>
    {
    
        private final List<MyBusinessObject> target;
    
        public ValidatingListDecorator(List<MyBusinessObject> target) {
            this.target = target;
        }
    
        @Override
        public MyBusinessObject set(int index, MyBusinessObject element)
        {
            validate(element);
            return target.set(index, element);
        }
    
        protected abstract void validate(MyBusinessObject element);
    
    }
    

    …and few implementations:

    class FooValidatingDecorator extends ValidatingListDecorator {
    
        public FooValidatingDecorator(List<MyBusinessObject> target)
        {
            super(target);
        }
    
        @Override
        protected void validate(MyBusinessObject element)
        {
            //throw if "foo" not met
        }
    }
    
    class BarValidatingDecorator extends ValidatingListDecorator {
    
        public BarValidatingDecorator(List<MyBusinessObject> target)
        {
            super(target);
        }
    
        @Override
        protected void validate(MyBusinessObject element)
        {
            //throw if "bar" not met
        }
    }
    

    Want to only validate foo?

    List<MyBusinessObject> list = new FooValidatingDecorator(rawArrayList);
    

    Want to validate both foo and bar?

    List<MyBusinessObject> list = 
      new BarValidatingDecorator(new FooValidatingDecorator(rawArrayList));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement a fast search on an ArrayList of objects. These
I would like to store a collection of custom objects in a user.config file
I would like to do something like add a nice-to-Excel-functions Name property to the
I would like to add the following MIME type to a site run by
I would like to add a DataGridViewTextBoxCell cell to a DataGridViewCell control, but as
I would like to add AES encryption to a software product, but am concerned
I would like to add graphing to my User Controls in ASP.NET MVC. I
I would like to add a typing speed indicator just below the textarea we
I would like to add a BuildListener to my headless build process, which is
I would like to add some icons to my treeview. Is there a way

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.