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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:48:41+00:00 2026-05-11T13:48:41+00:00

package abc; class DependencyDataCollection { private int sNo; private String sessionID; private int noOfDependency;

  • 0
package abc;  class DependencyDataCollection {     private int sNo;     private String sessionID;     private int noOfDependency;     private int noOfRejection;     private int totalValue;      /** Creates a new instance of DependencyDataCollection */     public DependencyDataCollection(int sNo, String sessionID, int noOfDependency, int noOfRejection, int totalValue)     {         this.sNo = sNo;         this.sessionID = sessionID;         this.noOfDependency = noOfDependency;         this.noOfRejection = noOfRejection;         this.totalValue = totalValue;     }      public int getSNo()     {         return sNo;     }      public String getSessionID()     {         return sessionID;     }      public int getNoOfDependency()     {         return noOfDependency;     }      public int getNoOfRejection()     {         return noOfRejection;     }      public int getTotalValue()     {         return totalValue;     } }  public class DependencyStack {      LinkedList lList;      /** Creates a new instance of DependencyStack */     public DependencyStack()     {         lList = new LinkedList();     }      public void add(int sNo, String sessionID, int noOfDependency, int noOfRejection, int totalValue)     {         lList.add(new DependencyDataCollection(sNo,sessionID,noOfDependency,noOfRejection,totalValue));     }      public int size()     {         return lList.size();     }      public void show()     {         for(int i=0;i<lList.size();i++)         {             DependencyDataCollection ddc = (DependencyDataCollection)lList.get(i);             System.out.println(ddc.getSNo()+'   '+ddc.getSessionID()+'   '+ddc.getNoOfDependency()+'     '+ddc.getNoOfRejection()+'      '+ddc.getTotalValue());         }     }      public int returnIndexOfSession(String sessionID)     {         DependencyDataCollection ddc = null;         for(int i=0;i<lList.size();i++)         {             ddc = (DependencyDataCollection)lList.get(i);             if(ddc.getSessionID().equals(sessionID))                 break;         }         return ddc.getSNo();     }      public static void main(String args[])     {         DependencyStack ds = new DependencyStack();         ds.add(1,'a',0,0,0);         ds.add(2,'b',0,0,0);         ds.show();          //System.out.println(ds.returnIndexOfSession('a'));  //        DependencyDataCollection ddc = new DependencyDataCollection(1,'a',0,0,0); //        System.out.println(ds.indexOf(ddc));     } } 

This is a simple Linked List program in java which is using inbuilt linkedlist class from java.util package. The linked list is used to store different amount of data using DependencyDataCollection Class..

Now my question is

1) Please evaluate this program, and temme am i respecting all java concepts like private member access,which i have done, etc..

2) I am facing problem to find the indexOf a particular session.

For e.g. Node 1 contains 1,’a’,0,0,0……………Node 2 contains 2,’b’,0,0,0

Now i wanted to find the indexOf the node which is containing one of the data as ‘b’ or ‘a’. What could be the shortest inbuilt method which can do so, as i have made a function named ‘public int returnIndexOfSession(String sessionID)’ which makes use of for loop, i find this very time consuming.. Is there any other way out..

Please evaluate and guide as i am a newbie in java.

  • 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-11T13:48:42+00:00Added an answer on May 11, 2026 at 1:48 pm

    Here are the changes I would make, rationale is in the comments.

    import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; import java.util.ArrayList; import java.util.List;   class DependencyDataCollection {     // makte them fnal, then you hava an immutible object and your code is much safer.     // in your case you had noset methods so it was safe, but always try to make things final.     private final int sNo;     private final String sessionID;     private final int noOfDependency;     private final int noOfRejection;     private final int totalValue;      public DependencyDataCollection(final int    sNo,                                      final String sessionID,                                      final int    noOfDependency,                                      final int    noOfRejection,                                      final int    totalValue)     {         this.sNo            = sNo;         this.sessionID      = sessionID;         this.noOfDependency = noOfDependency;         this.noOfRejection  = noOfRejection;         this.totalValue     = totalValue;     }      public int getSNo()     {         return sNo;     }      public String getSessionID()     {         return sessionID;     }      public int getNoOfDependency()     {         return noOfDependency;     }      public int getNoOfRejection()     {         return noOfRejection;     }      public int getTotalValue()     {         return totalValue;     } }  class DependencyStack {     // change the type to be as generic as poosible - List interface     // added generics so you get compile time safety and don't use casts later on     // renamed it to something meaningful     private final List<DependencyDataCollection> dependencies;      // use an ArrayList instead of a LinkedList, it'll be faster since you are not inserting/deleting     // into the middle of the list     {         dependencies = new ArrayList<DependencyDataCollection>();     }      // your Stack shouldn't know how to make the collections... (in my opinion)     public void add(final DependencyDataCollection ddc)     {         dependencies.add(ddc);     }      public int size()     {         return dependencies.size();     }      // the next 3 methods are just convenience since you don't know if someione     // will want to write to a file or a writer instead of a stream     public void show()     {         show(System.out);     }      public void show(final OutputStream out)     {         show(new OutputStreamWriter(out));     }      public void show(final Writer writer)     {         show(new PrintWriter(writer));     }      public void show(final PrintWriter writer)     {         // use the new for-each instead of the old style for loop         // this also uses an iterator which is faster than calling get         // (well on an ArrayList it probably is about the same, but a LinkedList it'll be faster)         for(final DependencyDataCollection ddc : dependencies)         {             writer.println(ddc.getSNo()            + '   ' +                            ddc.getSessionID()      + '   ' +                            ddc.getNoOfDependency() + '   ' +                            ddc.getNoOfRejection()  + '   ' +                            ddc.getTotalValue());         }     }      public int returnIndexOfSession(final String sessionID)     {         DependencyDataCollection foundDDC;         final int                retVal;          foundDDC = null;          for(final DependencyDataCollection ddc : dependencies)         {             if(ddc.getSessionID().equals(sessionID))             {                 foundDDC = ddc;                 break;             }         }          // deal with the fact that you might have not found the item and it would be null.         // this assumes -1 is an invalid session id         if(foundDDC == null)         {             retVal = -1;         }         else         {             retVal = foundDDC.getSNo();         }          return (retVal);     }      public static void main(final String[] args)     {         DependencyStack ds = new DependencyStack();         ds.add(new DependencyDataCollection(1,'a',0,0,0));         ds.add(new DependencyDataCollection(1,'a',0,0,0));         ds.show();          //System.out.println(ds.returnIndexOfSession('a'));  //        DependencyDataCollection ddc = new DependencyDataCollection(1,'a',0,0,0); //        System.out.println(ds.indexOf(ddc));     } } 

    Edit:

    This one will speed up the lookups (and deletes).

    class DependencyStack {     // A Map provides quick lookup     private final Map<String, DependencyDataCollection> dependencies;      // a LinkedHashMap allows for quick lookup, but iterates in the order they were added... if that matters for show.     {         dependencies = new LinkedHashMap<String, DependencyDataCollection>();     }      // your Stack shouldn't know how to make the collections... (in my opinion)     public void add(final DependencyDataCollection ddc)     {         if(ddc == null)         {             throw new IllegalArgumentException('ddc cannot be null');         }          dependencies.put(ddc.getSessionID(), ddc);     }      public int size()     {         return dependencies.size();     }      // the next 3 methods are just convenience since you don't know if someione     // will want to write to a file or a writer instead of a stream     public void show()     {         show(System.out);     }      public void show(final OutputStream out)     {         show(new OutputStreamWriter(out));     }      public void show(final Writer writer)     {         show(new PrintWriter(writer));     }      public void show(final PrintWriter writer)     {         // use the new for-each instead of the old style for loop         // this also uses an iterator which is faster than calling get         // (well on an ArrayList it probably is about the same, but a LinkedList it'll be faster)         for(final DependencyDataCollection ddc : dependencies.values())         {             writer.println(ddc.getSNo()            + '   ' +                            ddc.getSessionID()      + '   ' +                            ddc.getNoOfDependency() + '   ' +                            ddc.getNoOfRejection()  + '   ' +                            ddc.getTotalValue());         }     }      public int returnIndexOfSession(final String sessionID)     {         final DependencyDataCollection ddc;         final int                      retVal;          if(sessionID == null)         {             throw new IllegalArgumentException('sessionID cannot be null');         }          // get it if it exists, this is much faster then looping through a list         ddc = dependencies.get(sessionID);          // deal with the fact that you might have not found the item and it would be null.         // this assumes -1 is an invalid session id         if(ddc == null)         {             retVal = -1;         }         else         {             retVal = ddc.getSNo();         }          return (retVal);     }      public static void main(final String[] args)     {         DependencyStack ds = new DependencyStack();         ds.add(new DependencyDataCollection(1,'a',0,0,0));         ds.add(new DependencyDataCollection(1,'a',0,0,0));         ds.show();          //System.out.println(ds.returnIndexOfSession('a'));  //        DependencyDataCollection ddc = new DependencyDataCollection(1,'a',0,0,0); //        System.out.println(ds.indexOf(ddc));     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 95k
  • Answers 95k
  • 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 What's exotic is that x and y represent a single… May 11, 2026 at 6:57 pm
  • Editorial Team
    Editorial Team added an answer This one is totally googleable: http://www.google.com/search?q=c%23+outlook+style+listview+control May 11, 2026 at 6:57 pm
  • Editorial Team
    Editorial Team added an answer Are you using Maven 2.1.0 right? I had the same… May 11, 2026 at 6:57 pm

Related Questions

Sometimes it's necessary to modify Windows registry settings during an application install. I've recently
I package our server releases into zip files using a batch file (Windows), running
I'm receiving Package Load Failure error when I open VS 2005 after I installed
I have an SSIS package, which depending on a boolean variable, should either go

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.