java - 需要了解Java中LinkedList类的一些基础知识

标签 java

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));
    }
}

这是一个简单的 Java 链表程序,它使用 java.util 包中的内置链表类。链表用于使用 DependencyDataCollection 类存储不同数量的数据。

现在我的问题是

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..

请评估和指导,因为我是java新手。

最佳答案

以下是我要做出的更改,理由请参见评论。

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));
    }
}

编辑:

这将加快查找(和删除)速度。

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));
    }
}

关于java - 需要了解Java中LinkedList类的一些基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/628730/

相关文章:

java - 如何在执行时设置spring bootstrap.properties

java - 当我运行 Codenameone 程序时,按钮未显示在模拟器中

java - sun.misc.BASE64 到 apache commons

Java 获取数据库中的值总和时出错

java - 使用 @PostConstruct 模拟类会导致初始化失败

Java隐藏JFrame1和JFrame2,当JFrame0停用时?

java - 整数数组开关Android JAva

Oracle 的 TO_DATE 中的 Java Date.toString

java - 尝试使用 Jersey 和 tomcat 部署休息服务时无法打开特定网址

java - 替换 c :foreach on ui:repeat