黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 學習攻略 Java學習 快速學習指南:Java中的堆棧

快速學習指南:Java中的堆棧

更新時間:2022-04-02 11:19:59 來源:動力節點 瀏覽2099次

1.概述

在這篇快速文章中,我們將介紹java.util.Stack類并開始研究如何使用它。

堆棧是一種通用數據結構,它表示允許在恒定時間內推送/彈出元素的對象的 LIFO(后進先出)集合。

對于新的實現,我們應該支持Deque接口及其實現。 雙端隊列 定義了一組更完整和一致的 LIFO 操作。但是,我們可能仍然需要處理 Stack類,尤其是在遺留代碼中,所以理解它很重要。

2.創建堆棧

讓我們首先使用默認的無參數構造函數創建一個空的Stack實例:

@Test
public void whenStackIsCreated_thenItHasSizeZero() {
    Stack<Integer> intStack = new Stack<>();    
    assertEquals(0, intStack.size());
}

這將創建一個默認容量為 10的Stack 。如果添加的元素數量超過Stack總大小,它將自動加倍。但是,它的大小在刪除元素后永遠不會縮小。

3.堆棧同步

Stack是Vector的直接子類;這意味著與它的超類類似,它是一個同步的實現。

但是,并不總是需要同步,在這種情況下,建議使用ArrayDeque。

4.加入堆棧

讓我們首先使用push()方法將一個元素添加到Stack的頂部- 該方法還返回添加的元素:

@Test
public void whenElementIsPushed_thenStackSizeIsIncreased() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(1);    
    assertEquals(1, intStack.size());
}

使用push()方法與使用addElement()的效果相同。唯一的區別是addElement ()返回操作的結果,而不是添加的元素。

我們還可以一次添加多個元素:

@Test
public void whenMultipleElementsArePushed_thenStackSizeIsIncreased() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);    
    boolean result = intStack.addAll(intList);    
    assertTrue(result);
    assertEquals(7, intList.size());
}

5.從堆棧中檢索

接下來,讓我們看看如何獲??取和刪除Stack中的最后一個元素:

@Test
public void whenElementIsPoppedFromStack_thenElementIsRemovedAndSizeChanges() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    Integer element = intStack.pop();    
    assertEquals(Integer.valueOf(5), element);
    assertTrue(intStack.isEmpty());
}

我們也可以在不移除S大頭釘的情況下獲得最后一個元素:

@Test
public void whenElementIsPeeked_thenElementIsNotRemovedAndSizeDoesNotChange() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    Integer element = intStack.peek();
    assertEquals(Integer.valueOf(5), element);
    assertEquals(1, intStack.search(5));
    assertEquals(1, intStack.size());
}

6.在堆棧中搜索元素

(1)搜索

Stack允許我們搜索一個元素 并獲取它與頂部的距離:

@Test
public void whenElementIsOnStack_thenSearchReturnsItsDistanceFromTheTop() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    intStack.push(8);
    assertEquals(2, intStack.search(5));
}

結果是給定對象的索引。如果存在多個元素,則返回最接近頂部的元素的索引 。位于堆棧頂部的項目被視為位于位置 1。

如果找不到對象,search()將返回 -1。

(2)獲取元素索引

要獲取 S大頭釘上元素的索引,我們還可以使用indexOf()和lastIndexOf()方法:

@Test
public void whenElementIsOnStack_thenIndexOfReturnsItsIndex() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);    
    int indexOf = intStack.indexOf(5);    
    assertEquals(0, indexOf);
}

lastIndexOf()將始終找到最接近堆棧頂部的元素的索引。這與search()的工作方式非常相似——重要的區別是它返回索引,而不是與頂部的距離:

@Test
public void whenMultipleElementsAreOnStack_thenIndexOfReturnsLastElementIndex() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    intStack.push(5);
    intStack.push(5);    
    int lastIndexOf = intStack.lastIndexOf(5);    
    assertEquals(2, lastIndexOf);
}

7.從堆棧中刪除元素

除了用于刪除和檢索元素的pop()操作之外,我們還可以使用從Vector類繼承的多個操作來刪除元素。

(1)刪除指定元素

我們可以使用removeElement()方法刪除給定元素的第一次出現:

@Test
public void whenRemoveElementIsInvoked_thenElementIsRemoved() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    intStack.push(5);
    intStack.removeElement(5);    
    assertEquals(1, intStack.size());
}

我們還可以使用removeElementAt()來刪除Stack中指定索引下的元素:

    @Test
    public void whenRemoveElementAtIsInvoked_thenElementIsRemoved() {
        Stack<Integer> intStack = new Stack<>();
        intStack.push(5);
        intStack.push(7);        
        intStack.removeElementAt(1);        
        assertEquals(-1, intStack.search(7));
    }

(2)刪除多個元素

讓我們快速看一下如何使用removeAll() API 從Stack中刪除多個元素——它將Collection作為參數并從Stack中刪除所有匹配的元素:

@Test
public void givenElementsOnStack_whenRemoveAllIsInvoked_thenAllElementsFromCollectionAreRemoved() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    intStack.addAll(intList);
    intStack.add(500);
    intStack.removeAll(intList);
    assertEquals(1, intStack.size());
    assertEquals(1, intStack.search(500));
}

也可以使用clear()或removeAllElements()方法從堆棧中刪除所有元素;這兩種方法的工作原理相同:

@Test
public void whenRemoveAllElementsIsInvoked_thenAllElementsAreRemoved() {
    Stack<Integer> intStack = new Stack<>();
    intStack.push(5);
    intStack.push(7);
    intStack.removeAllElements();
    assertTrue(intStack.isEmpty());
}

(3)使用過濾器刪除元素

我們還可以使用條件從堆棧中刪除元素。讓我們看看如何使用removeIf ()執行此操作,并使用過濾器表達式作為參數:

@Test
public void whenRemoveIfIsInvoked_thenAllElementsSatysfyingConditionAreRemoved() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    intStack.addAll(intList);    
    intStack.removeIf(element -> element < 6);    
    assertEquals(2, intStack.size());
}

8.迭代堆棧

Stack允許我們同時使用Iterator和ListIterator。主要區別在于第一個允許我們在一個方向上遍歷Stack,第二個允許我們在兩個方向上執行此操作:

@Test
public void whenAnotherStackCreatedWhileTraversingStack_thenStacksAreEqual() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    intStack.addAll(intList);    
    ListIterator<Integer> it = intStack.listIterator();    
    Stack<Integer> result = new Stack<>();
    while(it.hasNext()) {
        result.push(it.next());
    }
    assertThat(result, equalTo(intStack));
}

Stack返回的所有迭代器都是快速失敗的。

9.Java 堆棧的 Stream API

Stack是一個集合,這意味著我們可以將它與 Java 8 Streams API 一起使用。將Stream與Stack一起使用類似于將其與任何其他Collection集合一起使用:

@Test
public void whenStackIsFiltered_allElementsNotSatisfyingFilterConditionAreDiscarded() {
    Stack<Integer> intStack = new Stack<>();
    List<Integer> inputIntList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10);
    intStack.addAll(inputIntList);
    List<Integer> filtered = intStack
      .stream()
      .filter(element -> element <= 3)
      .collect(Collectors.toList());
    assertEquals(3, filtered.size());
}

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 伊人精品视频在线观看 | 操你啦在线播放 | 久久99精品九九九久久婷婷 | 国产精品成人久久久 | 日本一区二区在线播放 | 免费高清伧理片午夜伧理片 | 五月婷综合 | 久久88香港三级 | 欧美人在线一区二区三区 | 日本免费福利视频 | 毛片毛多| 日韩欧美综合在线二区三区 | 亚洲免费播放 | 国产精品自在线天天看片 | 日韩中文字幕在线看 | 91亚洲免费视频 | 中国美女大战黑人国产 | 最近韩国日本免费 | 亚洲伦理一区二区 | 天天干天天综合 | 丁香六月狠狠激情综合基地 | 中文字幕精品1在线 | 国产一区二区三区精品视频 | 一级毛片免费毛片一级毛片免费 | 欧美丝袜制服 | 三级黄a| 欧洲一区二区 | 男男羞羞视频免费网站 | 中文字幕日韩高清版毛片 | 欧美日本日韩 | 精品福利一区 | 色愉拍亚洲偷自拍 | 国产高清视频在线观看不卡v | 亚洲第一中文字幕 | 天天摸日日碰天天看免费 | 成人短视频在线观看视频 | 美女又美女又黄又免费网站 | 欧美日韩极品 | 夜夜躁日日躁狠狠久久 | 亚洲精品中文字幕字幕 | 天天夜天干天天爽 |