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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:56:27+00:00 2026-06-08T13:56:27+00:00

Suppose you have a List<Elements> in memory and a List<Files> , each of which

  • 0

Suppose you have a List<Elements> in memory and a List<Files>, each of which is a serialized representation of List<Elements>

The goal is to perform the same algorithm against each of the elements.

Lets say the job is to count elements

int i = 0;
for (Element e : list) {
       ++i;
}

for (File f : directory()) {
    for (Element e : listWeSomehowGetFromTheFile) {
           ++i;
    }
}

Can this be further abstracted somehow? Essentially, algorithm is identical (++i). Is there a way to clean this up?

I certainly can have it’s own class Counter(List<Element>), that has count () method that runs the algo on the list. This way we can:

 i+=Counter.count(list);
 for (File f : directory()) {
   i+=Counter.count(listWeSomehowGetFromTheFile);
 }

But even this seems like it can be improved.

For the purposes of this example, lets assume that all operations (a bunch of them) will be performed on list in memory and a list coming from the same files in same directories. The only thing that would change is the algo.

What pattern would fit best to handle something like this?

  • 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-06-08T13:56:30+00:00Added an answer on June 8, 2026 at 1:56 pm

    The Visitor Pattern is quite popular when you want to do something to each item in a collection.

    The visitor pattern is also good because it allows you to expand the calculations you can perform without rewriting the class on which visitors operate.

    If you don’t need to follow a formal pattern exactly,

    1) define an interface like Operation with a method calculate that takes an element and operates.
    2) You calculation is an implementation of Operation.
    3) Loop over your elements, passing the elements to your operation implementation.

    If the results of calculate are cumulative (depend on previous calculate invocations against other elements), you can modify calculate to take an object that contains the state of the calculation, and then when calculate is fired against an element, you update the state. You keep passing the same state object to every calculate invocation.

    As an outline (this wont compile):

    public Interface Operation {
       // don't necessarily need state
       public void calculate (Element e, State state);
    }
    
    CountOp extends Operation {
       count = 0;
    
       public void calculate(Element e, State state){
          // not using element or state because this is so simple....
          count++;
       }
    }
    
    Operation op = new CountOp();
    State state = new SomeStateImpl();
    for (File f : directory()) {
        for (Element e : listWeSomehowGetFromTheFile) {
              op.calculate(e, state);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a list of lists of elements which are all the same
Suppose I have a list, called elements , each of which does or does
Suppose I have a list, in which no new nodes are added or deleted.
Suppose I have a list box with different items, each with unique values, and
Suppose I have a list of elements and I want to randomly select an
Suppose I have a List with the elements 4,7,9,17,24 and I want to insert
Suppose we have 5 checkboxes and each has unique value mapped with respective list
Suppose I have a list of elements like [5, 3, 1, 2, 4] ,
Suppose I have a huge list of elements with a structure like: <div class=item>
Let's suppose that I have a list of elements, and I want to select

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.