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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:46:59+00:00 2026-06-06T18:46:59+00:00

I have three lists, I need to move Animal object from list animalSource to

  • 0

I have three lists, I need to move Animal object from list animalSource to list animalTarget using list animalFilterName. Only Animal with names present in the list animalFilterName, should be moved from animalSource to animalTarget, performance wise is there a better way of doing it than I am doing below. Using just sample data for now.

public class Animal {

    private String name;
    private String color;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}


public class MoveAnimal {

    /**
     * @param args
     */
    public static void main(String[] args) {

        List<Animal> animalSource = new ArrayList<Animal>();
        List<String> animalFilterName = new ArrayList<String>();
        List<Animal> animalTarget = new ArrayList<Animal>();

        animalFilterName.add("Name1");
        animalFilterName.add("Name2");

        Animal a1 = new Animal();
        a1.setColor("Color1");
        a1.setName("Name1");

        Animal a2 = new Animal();
        a2.setColor("Color2");
        a2.setName("Name2");

        Animal a3 = new Animal();
        a3.setColor("Color1");
        a3.setName("Name3");

        Animal a4 = new Animal();
        a4.setColor("Color1");
        a4.setName("Name4");

        Animal a5 = new Animal();
        a5.setColor("Color5");
        a5.setName("Name1");


        animalSource.add(a1);
        animalSource.add(a2);
        animalSource.add(a3);
        animalSource.add(a4);
        animalSource.add(a5);


        for(String s: animalFilterName) {
            for(Animal a: animalSource) {
                if(s.equals(a.getName())) {
                    animalTarget.add(a);
                }
            }
        }

    }

}
  • 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-06T18:47:01+00:00Added an answer on June 6, 2026 at 6:47 pm

    For better performance, you would want to use Sets. I’m pretty sure what you’re doing is an O(m * n) operation for m = animalSource.size() and n = animalFilterName.size(), since lookups in ArrayLists are order n (to the lists size)

    Lookups, inserts, and removals in Sets are generally either amortized constant time or logarithmic time (to the size of the set) (depending on the specifics of the set implementation) so worst case, using sets would reduce this to O(m * log(n)) for the same m and n.

    Set<Animal> animalSource = ...;
    Set<Animal> animalTarget = ...;
    Set<String> animalFilterName = ...;
    
    // add matching animals to new set
    for (Animal a : animalSource)
        if (animalFilterName.contains(a.getName())) animalTarget.add(a);
    
    // if you need to remove them from the first set, uncomment these lines
    // for (Animal a : animalTarget)
    //     animalSource.remove(a);
    

    I think omitting line breaks in one line ifs and loops makes them look neater. It’s personal preference, you are not obliged to copy my style.

    Edit: fixed time complexity

    Another Edit: When you say move, do you mean add to one list and remove from the other? Or do you just mean add to one list?

    Third Edit: fixed fragmented line

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

Sidebar

Related Questions

I have a list of MyObjects which I need to divide into three groups:
I have three lists that I want to convert into one list. When I
I have these three lists with the same number of elements List<String>competitoinsIDs = new
I have a list of rows from a dataset that I need to iterate
I have a task where I need to move a bunch of files from
I have passed Python list to template like l=['text','there is no salary','I need alcohol','and
i have three lists with the same number of elements in each other, i
I have three lists of lists, and I'm trying to write a generator function
Suppose I have three lists: list1 = a, c, d, r, t list2 =
So I currently have three unordered lists in my header. This is a single-page

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.