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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:19:27+00:00 2026-06-03T11:19:27+00:00

I have an ArrayList<Foo> structure in Java with standard tree-like fields: id , id_parent

  • 0

I have an ArrayList<Foo> structure in Java with standard tree-like fields: id, id_parent, value

I want to get a list of all leaves (id_element‘s) of a given node.

My example data is:

Foo ArrayList<E>
    elementData Object[10]
        [0] Foo
            id          1333393146
            id_element  1333398441937
            id_parent   1333393120
            value       "1.1."
        [1] Foo
            id          1333393120
            id_element  0
            id_parent   0
            value       "1."
        [2] Foo
            id          1333400050
            id_element  0
            id_parent   0
            value       "2."
        [3] Foo
            id          1333400480
            id_element  0
            id_parent   1333400050
            value       "2.1."
        [4] Foo
            id          1333400596
            id_element  1335957085269
            id_parent   1333400480
            value       "2.1.1."
        [5] Foo
            id          1333401059
            id_element  1335957088564
            id_parent   1333400480
            value       "2.1.2."
        [6] Foo
            id          1333401973
            id_element  1335957090492
            id_parent   1333400480
            value       "2.1.3."
        [7] Foo
            id          1333401974
            id_element  1335957093220
            id_parent   1333400050
            value       "2.2."
        [8] Foo
            id          1333392031
            id_element  0
            id_parent   0
            value       "3."
        [9] Foo
            id          1333394672
            id_element  1335957098326
            id_parent   1333392031
            value       "3.1."

I need to do a function public ArrayList<Long> GetIds(ArrayList<Foo> tree, Long id_node) { } where tree is my structure, and id_node is an id of a node.

I need only parent node leaves not child nodes.

e.g.:


input: [above structure], id = 1333400050

output: 1335957085269, 1335957088564, 1335957090492, 1335957093220


I do not know why I have a blackout about 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-03T11:19:30+00:00Added an answer on June 3, 2026 at 11:19 am
    public static ArrayList<Long> getIds(ArrayList<Foo> tree, Long id_node) {
        ArrayList<Long> leaves = new ArrayList<Long>();
        for (Foo foo : tree) {
            if (foo.id_parent == id_node) {
                ArrayList<Long> ids = getIds(tree, foo.id);
                if (ids == null) {
                    leaves.add(foo.id);
                } else {
                    leaves.addAll(ids);
                }
            }
        }
        if (leaves.isEmpty()) {
            return null;
        }
        return leaves;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i have this ArrayList: list.get(0) == love list.get(1) == foo list.get(2) == make
I have java API which return this type: ArrayList[ArrayList[String]] = Foo.someJavaMethod() In scala program,
I have the arraylist which has some process names like Notepad, mspaint I want
Possible Duplicate: Java: How to convert List to Map I have arrayList ArrayList<Product> productList
I have arraylist myorderdata . I want to retrieve this arraylist to one String
I have an arraylist that gets different type of values in it, 1st value->
I have an ArrayList that I want to use to hold RaceCar objects that
I have a class Foo which overrides equals() and hashCode() properly. I would like
I have the following Programm package utests; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.ArrayList; import
If I have this class: class Foo<T> implements SomeInterface { final private List<T> list

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.