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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:26:51+00:00 2026-06-12T09:26:51+00:00

I have got stuck in some issue where I need to traverse through a

  • 0

I have got stuck in some issue where I need to traverse through a hierarchy and compare some values and moment I got match need to return the result. Here is my complete use-case

I am getting Collection<Categories> categoryList and A Collection<MyCustomObject>,
MyCustomObject{
// some properties
private Category category
}

I need to compare the code of category with the code of category coming under MyCustomObject and need to send back MyCustomObject with first match.

For this comparison I need to traverse through the hierarchy of Collection i.w if the match is not found in the current category need to get Category list by calling
categoryList.getSuperCategories() and then go upward.

I though of recursive method call but seems like this is not working in my case as if I keep traversing the first category till the root I am not able to keep track of the second one.

I was trying something (not completed nor working correctly)

public ProductAllotmentData getProductAllotmnet(final Collection<CategoryModel> categoryModelList)
 {
  if (CollectionUtils.isNotEmpty(categoryModelList))
  {
   final List<ProductAllotment> productAllotmentList = getProductAllotmentEntries();
   if (CollectionUtils.isNotEmpty(productAllotmentList))
   {
    for (final CategoryModel model : categoryModelList)
    {
     for (final ProductAllotmentData productAllotmentData : productAllotmentList)
     {
      if (model.getCode().equals(productAllotmentData.getAllotmentCategory().getCode()))
      {
       return ProductAllotmentData;

      }
      else
      {
       return getAllotmentForCategory(model.getAllSupercategories(), productAllotmentyData);
      }
   }
    }
   }
  }

  return null;
 }

—

public ProductAllotmentData getAllotmentForCategory(final Collection<CategoryModel> categoryModelList,
       final ProductAllotmentData productAllotmentaData)
     {
      ProductAllotmentData allotmentData = null;
      for (final CategoryModel model : categoryModelList)
      {
       if (model.getCode().equals(productAllotmentData.getAllotmentCategory().getCode()))
       {
        allotmentEntryData = productAllotmentData;

       }
       else
       {
        return getAllotmentForCategory(model.getSupercategories(), productAllotmentData);
       }
      }

      return allotmentEntryData;
     }

other option came to my mind is to create a kind of stack and put all categories in that and den pop them one at a time compare them and send back the first match ignoring rest of the items in the stack but i m looking for much better and flexible solution.

Problem

I am not sure how I can traverse hierarchy of each category up to compare its code. getAllotmentForCategory is where i am trying to traverse my category hierarchy.

Any help in this regard is really good

  • 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-12T09:26:52+00:00Added an answer on June 12, 2026 at 9:26 am

    You need to traverse back to child categories when you reach the root. Instead Go directly to root and go down until you find them with recursive method.

    like… (the code is not copy paste code)

    public ProductAllotmentEntryData recursive(root, code) { 
     if(root.code == code) 
        return root;
     }else{ 
        for(root.childs){ 
            object = recursive(child, code) 
            if(object!=null) 
                return object;
        } 
     return null;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been making a wordpress template. i got stuck at some place... the
I have an issue that has got me pretty stumped, and would appreciate some
I am imlementing a simple merge function and I have got stuck, as the
I have been working on this for few hours and got stuck, so how
I am working on a problem and got stuck at a wall I have
Hi i have parsed a online xml containing images, I got stuck on parsing
i have got <div><a class='link' href='index.php' data-container='target'>Load</a></div> <div class='target'></div> i need to write jquery
I'm doing some tests on a wicket prototype here, and I got stuck on
I need some help with the design of a query. I've got one table
i've got (i hope) a little issue doing some development for Google TV what

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.