以LIFO顺序从列表中获取对象? (后进先出)(初级)

时间:2016-09-18 15:12:13

标签: java list arraylist linked-list lifo

(请记住,这是初学者问题)

我有两节课; MailItemMailServer。我有一个测试类,我在其中制作并将MailItems发送到我的MailServer。我发送给MailItems给一个特定的用户,我期望以LIFO顺序(Last-In-First-Out)检索这些,所以最后发送的必须是我收到的第一个。

这是我的课程。

MailItem类:

public class MailItem
{
    private String from;
    private String to;
    private String message;
    private String subject;

    public MailItem(String from, String to, String message, String subject)
    {
        this.from = from;
        this.to = to;
        this.message = message;
        this.subject = subject;
    }

    public String getFrom()
    {
        return from;
    }

    public String getTo()
    {
        return to;
    }

    public String getSubject(){
        return subject;
    }

    public String getMessage()
    {
        return message;
    }

    public void print()
    {
        System.out.println("From: " + from);
        System.out.println("To: " + to);
        System.out.println("Message: " + message);
        System.out.println("Subject: " + subject);
    }
}

MailServer类:

public class MailServer
{
    private List<MailItem> items;
    private HashMap<String, List<MailItem>> hashmap;

    public int howManyMessages(){
        return items.size();
    }

    public int howManyUsers(){
        return hashmap.size();
    }

    public MailServer()
    {
        items = new LinkedList<MailItem>();
        hashmap = new HashMap<String, List<MailItem>>();
    }

    public int howManyMailItems(String who)
    {
        int count = 0;
        for(MailItem item : items) {
            if(item.getTo().equals(who)) {
                count++;
            }
        }
        return count;
    }

    public MailItem getNextMailItem(String who)
    {
        Iterator<MailItem> it = items.iterator();
        while(it.hasNext()) {
            MailItem item = it.next();
            if(item.getTo().equals(who)) {
                it.remove();
                return item;
            }
        }
        return null;
    }

    public void post(MailItem item)
    {
        if(!isEmpty(item)){
            items.add(item);
        }
    }

    private boolean isEmpty(MailItem mail){
        if(mail.getFrom() == "" || mail.getTo() == ""){
            return true;
        }
        return false;
    }

    public int createMailbox(String user){
        if(hashmap.containsKey(user)){
            return 0;
        }
        List<MailItem> mil = new ArrayList<MailItem>();
        hashmap.put(user, mil);
        return 1;
    }

    public int createMailbox(String[] users){
        int createdBoxes = 0;
        for(String user: users){
            int created = createMailbox(user);
            if(created == 1){
                createdBoxes++;
            }
        }
        return createdBoxes;
    }

    private List<MailItem> getMailbox(String who){
        if(hashmap.containsKey(who)){
            List<MailItem> ls = hashmap.get(who);
            return ls;
        }else{
            return null;
        }
    }
}

以下是我的测试结果:

@Test
    public void testReceiveOrder(){
        mailServer.post(mess1User1to2);
        mailServer.post(mess2User1to2);
        assertEquals(mess2User1to2,mailServer.getNextMailItem("user2"));
    }

当我运行此测试时,我以FIFO(先进先出)的方式检索这些电子邮件。我没有检索名为 mess2User1to2 的邮件,而是获取 mess1User1to2 。我尝试使用LinkedList在LIFO中检索它们,但它对我不起作用。我做错了什么?

1 个答案:

答案 0 :(得分:2)

您的post方法会将该项添加到列表末尾,并且您的getNextMailItem方法会开始搜索列表前面的项目。因此,您将获得FIFO(=队列)行为。

要更改此项,请在列表末尾开始搜索:

public MailItem getNextMailItem(String who) {
    ListIterator<MailItem> it = items.listIterator(items.size());
    while(it.hasPrevious()) {
        MailItem item = it.previous();
        if(item.getTo().equals(who)) {
            it.remove();
            return item;
        }
    }
    return null;
}

或者您也可以将项目添加到列表的前面,但是我没有检查过,如果这违反了任何其他方法:

private LinkedList<MailItem> items;

public void post(MailItem item) {
    if(!isEmpty(item)){
        items.addFirst(item);
    }
}