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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:36:05+00:00 2026-05-10T15:36:05+00:00

I want to implement in Java a class for handling graph data structures. I

  • 0

I want to implement in Java a class for handling graph data structures. I have a Node class and an Edge class. The Graph class maintains two list: a list of nodes and a list of edges. Each node must have an unique name. How do I guard against a situation like this:

Graph g = new Graph();  Node n1 = new Node('#1'); Node n2 = new Node('#2');  Edge e1 = new Edge('e#1', '#1', '#2');  // Each node is added like a reference g.addNode(n1); g.addNode(n2); g.addEdge(e1);  // This will break the internal integrity of the graph n1.setName('#3');    g.getNode('#2').setName('#4');  

I believe I should clone the nodes and the edges when adding them to the graph and return a NodeEnvelope class that will maintain the graph structural integrity. Is this the right way of doing this or the design is broken from the beginning ?

  • 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. 2026-05-10T15:36:05+00:00Added an answer on May 10, 2026 at 3:36 pm

    I work with graph structures in Java a lot, and my advice would be to make any data member of the Node and Edge class that the Graph depends on for maintaining its structure final, with no setters. In fact, if you can, I would make Node and Edge completely immutable, which has many benefits.

    So, for example:

    public final class Node {      private final String name;      public Node(String name) {            this.name = name;     }      public String getName() { return name; }     // note: no setter for name } 

    You would then do your uniqueness check in the Graph object:

    public class Graph {     Set<Node> nodes = new HashSet<Node>();     public void addNode(Node n) {         // note: this assumes you've properly overridden          // equals and hashCode in Node to make Nodes with the          // same name .equal() and hash to the same value.         if(nodes.contains(n)) {             throw new IllegalArgumentException('Already in graph: ' + node);         }         nodes.add(n);     } } 

    If you need to modify a name of a node, remove the old node and add a new one. This might sound like extra work, but it saves a lot of effort keeping everything straight.

    Really, though, creating your own Graph structure from the ground up is probably unnecessary — this issue is only the first of many you are likely to run into if you build your own.

    I would recommend finding a good open source Java graph library, and using that instead. Depending on what you are doing, there are a few options out there. I have used JUNG in the past, and would recommend it as a good starting point.

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

Sidebar

Ask A Question

Stats

  • Questions 101k
  • Answers 101k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Make the graph bigger and you'll see that O(n logn)… May 11, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer There's a delegate method in NSApplication's delegate: - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication… May 11, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer I found the solution. Normal (hexed) hashes of sha1() are… May 11, 2026 at 8:04 pm

Related Questions

I have been programming in C# and Java for a little over a year
I'm making a mini ORM for a Java program I'm writing... there is a
I think, the following can't be done in Java. But I would be happy
I'm a freshman in college going for my computer science degree... I've programmed plenty

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.