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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:37:28+00:00 2026-05-20T19:37:28+00:00

Say I have two Collections : Collection< Integer > foo = new ArrayList< Integer

  • 0

Say I have two Collections:

Collection< Integer > foo = new ArrayList< Integer >();
Collection< Integer > bar = new ArrayList< Integer >();

and say sometimes I would like to iterate over them individually, but sometimes together. Is there a way to create a wrapper around foo and bar so that I can iterate over the combined pair, but which is also updated whenever foo and bar change? (i.e. Collection.addAll() is not suitable).

For example:

Collection< Integer > wrapper = ... // holds references to both bar and foo

foo.add( 1 );
bar.add( 99 );

for( Integer fooInt : foo ) {
    System.out.println( fooInt );
} // output: 1

for( Integer barInt : bar ) {
    System.out.println( barInt );
} // output: 99

for( Integer wrapInt : wrapper ) {
    System.out.println( wrapInt );
} // output: 1, 99

foo.add( 543 );

for( Integer wrapInt : wrapper ) {
    System.out.println( wrapInt );
} // output: 1, 99, 543

Thanks!

  • 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-20T19:37:29+00:00Added an answer on May 20, 2026 at 7:37 pm

    I wrote two functions for that :

    /**
     * Create an Iterator from multiple Iterators. The returned Iterator
     * traverses all elements from all sources, in the order, as if they belong
     * to the same source.
     * 
     * @param <T> type of elements
     * @param sources sources of the elements, in order of traversal
     * @return an iterator over multiple iterators in sequence
     */
    public static <T> Iterator<T> concatenate(final Iterator<T> ... sources) {
        if (sources.length == 0) {
            return new Iterator<T>() {
                @Override public boolean hasNext() { return false; }
                @Override public T next() { throw new NoSuchElementException("end of iteration"); }
                @Override public void remove() { throw new IllegalStateException("no previous element"); }
            };
        }
        return new Iterator<T>() {
    
            Iterator<Iterator<T>> sourcesIterator = Arrays.asList(sources).iterator();
            Iterator<T> currentIterator = sourcesIterator.next();
    
            @Override
            public boolean hasNext() {
                if (currentIterator.hasNext()) {
                    return true;
                } else {
                    if (sourcesIterator.hasNext()) {
                        currentIterator = sourcesIterator.next();
                        return hasNext();
                    } else {
                        return false;
                    }
                }
            }
    
            @Override
            public T next() {
                if (hasNext()) {
                    return currentIterator.next();
                } else {
                    throw new NoSuchElementException("end of iteration");
                }
            }
    
            @Override
            public void remove() {
                currentIterator.remove();
            }
        };
    }
    
    /**
     * Create an Iterable from multiple Iterables. The returned Iterable
     * traverses all elements from all sources, in the order, as if they belong
     * to the same source.
     * 
     * @param <T> type of elements
     * @param sources sources of the elements, in order of traversal
     * @return an iterable over multiple iterators in sequence
     */
    @SuppressWarnings("unchecked") // impossible to create a generic array
    public static <T> Iterable<T> concatenate(final Iterable<T> ... sources) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                final Iterator[] iteratorsArrays = new Iterator[sources.length];
                for (int i = 0; i < sources.length; i++) {
                    iteratorsArrays[i] = sources[i].iterator();
                }
                return concatenate(iteratorsArrays);
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say I have the following desire, to simplify the IConvertible's to allow me
(please excuse that I didn't use aliases). I would like my query output to
There doesn't seem to be any tried and true set of best practices to

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.