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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:51:33+00:00 2026-06-15T11:51:33+00:00

I have a tree that consists of different type of objects for example Tree

  • 0

I have a tree that consists of different type of objects for example

   Tree
    |  \
  apple  cat
    |  \
  dog  grass
        |   \
       door something

For example I can access the object something using Tree.getApple().getGrass().getSomething() where on every getter I have to check if the node exists. The problem is that the structure I use is pretty big and I want to have a utility class to speed this up.

I want only given the name of the object to be able to extract it from the structure. For example to have a method to which after I give Something.class as an argument to traverse the structure, return the object if it exists and return null otherwise. (I cannot have multiple occurrences of one class in the structure, i.e. I can have only one apple object)

Is that possible? What can I use to implement it?

  • 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-15T11:51:34+00:00Added an answer on June 15, 2026 at 11:51 am

    Implement a visitor that traverses the three and implement the filter in the visitor.

    Roughly:

        public class LeafSearching {
    
        private final Class typeToFind;
    
        private Object result;
    
        public LeafSearching(Class typeToFind) {
            this.typeToFind = typeToFind;
        }
    
        public void visit(Node node) {
            if (typeToFind.isAssignableFrom(node.getClass())){
                result = node;
                return;
            }
    
            for (Node child : node.getChildren()) {
                child.accept(this);
            }
        }
    }
    
    interface Node {
    
        boolean hasChildren();
    
        List<Node> getChildren();
    
        void accept(LeafSearching ls);
    }
    
    class Apple implements Node {
        //...
    }
    
    class Cat implements Node {
        // ...
    }
    
    class TreeRoot implements Node {
    
        private Apple apple;
    
        private Cat cat;
    
        public Apple getApple() {
            return apple;
        }
    
        public Cat getCat() {
            return cat;
        }
    
        @Override
        public boolean hasChildren() {
            return false;
        }
    
        @Override
        public List<Node> getChildren() {
            final List<Node> children = new ArrayList<Node>;
    
            if (getApple() != null) {
                children.add(this.apple);
            }
    
            if (getCat() != null) {
                children.add(this.cat);
            }
    
            return children;
        }
    
        public void accept(LeafSearching ls) {
            ls.visit(this);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tree-like object structure that consists of two types of objects: object
I have a tree that consists of several objects, where each object has a
I have a tree structure that can be n-levels deep, without restriction. That means
I have a tree with n elements that store something like |id|parent_id| . I
I have an app that consists mostly of one instance that is a tree
I have a basic file system tree that consists of two tables: folders =======================
I have a flash game that consists of many different screens (Login, Menu, Game,
I have tree structures that are stored in a DB table. The table can
If I have a Tree that has Apples, how should I model the fact
I have to walk a tree that reaches me from a NodeList, I need

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.