산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> java

자바 대기열 인터페이스

자바 대기열 인터페이스

이 자습서에서는 Java Queue 인터페이스와 해당 메서드에 대해 알아봅니다.

Queue Java 컬렉션 프레임워크의 인터페이스는 큐 데이터 구조의 기능을 제공합니다. Collection 확장 인터페이스.

<시간>

대기열을 구현하는 클래스

Queue 이후 인터페이스이므로 직접 구현할 수 없습니다.

Queue의 기능을 사용하려면 , 이를 구현하는 클래스를 사용해야 합니다.

<시간>

Queue를 확장하는 인터페이스

Queue 인터페이스는 다양한 하위 인터페이스로 확장됩니다.

<시간>

대기열 데이터 구조 작업

대기열에서 요소는 선입 선출 방식으로 저장되고 액세스됩니다. 방법. 즉, 요소가 뒤에서 추가됩니다. 및 앞에서 제거 .

<시간>

대기열을 사용하는 방법

Java에서는 java.util.Queue을 가져와야 합니다. Queue를 사용하기 위한 패키지 .


// LinkedList implementation of Queue
Queue<String> animal1 = new LinkedList<>();

// Array implementation of Queue
Queue<String> animal2 = new ArrayDeque<>();

// Priority Queue implementation of Queue
Queue<String> animal 3 = new PriorityQueue<>();

여기에서 animal1 개체를 만들었습니다. , 동물2animal3 LinkedList 클래스 , ArrayDequePriorityQueue 각기. 이러한 개체는 Queue의 기능을 사용할 수 있습니다. 인터페이스.

<시간>

대기열 방법

Queue 인터페이스는 Collection의 모든 메소드를 포함합니다. 상호 작용. Collection 때문입니다. Queue의 슈퍼 인터페이스입니다. .

Queue의 일반적으로 사용되는 방법 중 일부 인터페이스는 다음과 같습니다.

대기열 인터페이스 구현

1. LinkedList 클래스 구현

import java.util.Queue;
import java.util.LinkedList;

class Main {

    public static void main(String[] args) {
        // Creating Queue using the LinkedList class
        Queue<Integer> numbers = new LinkedList<>();

        // offer elements to the Queue
        numbers.offer(1);
        numbers.offer(2);
        numbers.offer(3);
        System.out.println("Queue: " + numbers);

        // Access elements of the Queue
        int accessedNumber = numbers.peek();
        System.out.println("Accessed Element: " + accessedNumber);

        // Remove elements from the Queue
        int removedNumber = numbers.poll();
        System.out.println("Removed Element: " + removedNumber);

        System.out.println("Updated Queue: " + numbers);
    }
}

출력

Queue: [1, 2, 3]
Accessed Element: 1
Removed Element: 1
Updated Queue: [2, 3]

자세한 내용은 Java LinkedList를 참조하십시오.

2. PriorityQueue 클래스 구현

import java.util.Queue;
import java.util.PriorityQueue;

class Main {

    public static void main(String[] args) {
        // Creating Queue using the PriorityQueue class
        Queue<Integer> numbers = new PriorityQueue<>();

        // offer elements to the Queue
        numbers.offer(5);
        numbers.offer(1);
        numbers.offer(2);
        System.out.println("Queue: " + numbers);

        // Access elements of the Queue
        int accessedNumber = numbers.peek();
        System.out.println("Accessed Element: " + accessedNumber);

        // Remove elements from the Queue
        int removedNumber = numbers.poll();
        System.out.println("Removed Element: " + removedNumber);

        System.out.println("Updated Queue: " + numbers);
    }
}

출력

Queue: [1, 5, 2]
Accessed Element: 1
Removed Element: 1
Updated Queue: [2, 5]

자세한 내용은 Java PriorityQueue를 방문하십시오.

<시간>

다음 튜토리얼에서는 Queue 인터페이스 및 구현에 대해 자세히 설명합니다.


java

  1. C# 인터페이스
  2. 자바 연산자
  3. 자바 슈퍼
  4. 자바 인터페이스
  5. 자바 리소스 사용
  6. 자바 주석
  7. Java NavigableSet 인터페이스
  8. 자바 람다 표현식
  9. 자바 - 인터페이스
  10. Java 9 - 개인 인터페이스 메소드