- Java.util 包类
- Java.util - 首页
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util 包其他内容
- Java.util - 接口
- Java.util - 异常
- Java.util - 枚举
- Java.util 有用资源
- Java.util - 有用资源
- Java.util - 讨论
Java PriorityQueue contains(Object) 方法
描述
Java PriorityQueue contains(Object) 方法如果此队列包含指定元素则返回true。对于自定义对象,应实现 equals() 方法,以便 contains 可以正确比较对象。
声明
以下是java.util.PriorityQueue.contains() 方法的声明
public boolean contains(Object o)
参数
o - 要检查是否包含在队列中的对象。
返回值
如果此队列包含指定的元素,则此方法返回true,否则返回false。
异常
无
检查整数 PriorityQueue 中元素是否存在示例
以下示例演示了 Java PriorityQueue contains() 方法的使用。我们使用的是 Integer。我们将添加一些元素,然后检查某些元素是否存在。
package com.tutorialspoint; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) { // create an empty priority queue PriorityQueue<Integer> queue = new PriorityQueue<>(); // use add() method to add elements in the queue queue.add(20); queue.add(30); queue.add(10); queue.add(18); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // queue contains element 10 if (queue.contains(10)) { System.out.println("element 10 is present in the queue"); } else { System.out.println("element 10 is not present in the queue"); } // queue does not contain element 25 if (queue.contains(25)) { System.out.println("element 25 is present in the queue"); } else { System.out.println("element 25 is not present in the queue"); } } }
输出
让我们编译并运行上述程序,这将产生以下结果:
PriorityQueue = [10, 18, 20, 30] element 10 is present in the queue element 25 is not present in the queue
检查字符串 PriorityQueue 中元素是否存在示例
以下示例演示了 Java PriorityQueue contains() 方法与字符串一起使用。我们将添加一些元素,然后检查某些元素是否存在。
package com.tutorialspoint; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) { // create an empty priority queue PriorityQueue<String> queue = new PriorityQueue<>(); // use add() method to add elements in the queue queue.add("Welcome"); queue.add("to"); queue.add("tutorialspoint"); queue.add(".com"); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // queue contains element tutorialspoint if (queue.contains("tutorialspoint")) { System.out.println("element tutorialspoint is present in the queue"); } else { System.out.println("element tutorialspoint is not present in the queue"); } // queue does not contain element html if (queue.contains("html")) { System.out.println("element html is present in the queue"); } else { System.out.println("element html is not present in the queue"); } } }
输出
让我们编译并运行上述程序,这将产生以下结果:
PriorityQueue = [.com, Welcome, tutorialspoint, to] element tutorialspoint is present in the queue element html is not present in the queue
检查对象 PriorityQueue 中元素是否存在示例
以下示例演示了 Java PriorityQueue contains() 方法与 Student 对象一起使用。我们将添加一些元素,然后检查某些元素是否存在。
package com.tutorialspoint; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) { // create an empty priority queue PriorityQueue<Student> queue = new PriorityQueue<>(); // use add() method to add elements in the queue queue.add(new Student(1, "Julie")); queue.add(new Student(2, "Robert")); queue.add(new Student(3, "Adam")); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // queue contains element Robert if (queue.contains(new Student(2, "Robert"))) { System.out.println("Student Robert is present in the queue"); } else { System.out.println("Student Robert is not present in the queue"); } // queue does not contain element Jane if (queue.contains(new Student(4, "Jane"))) { System.out.println("Student Jane is present in the queue"); } else { System.out.println("Student Jane is not present in the queue"); } } } class Student implements Comparable<Student> { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } @Override public int compareTo(Student student) { return this.rollNo - student.rollNo; } }
输出
让我们编译并运行上述程序,这将产生以下结果:
PriorityQueue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] Student Robert is present in the queue Student Jane is not present in the queue
java_util_priorityqueue.htm
广告