如何解决错误:15:错误:无法在openjdk 1.7.0_95中推断PriorityQueue <>的类型参数?

fuz*_*ous 5 java priority-queue data-structures java-8

我正在解决有关hackerearth问题,当我使用Java版本Java 8(oracle 1.8.0_131)时,它已成功编译并通过,但是当使用Java(openjdk 1.7.0_95)时,它给出了一个error 15: error: cannot infer type arguments for PriorityQueue<>错误。mx优先级队列正在声明。我想知道如何解决它以及为什么会发生此错误。这是代码:(请注意,该问题不是任何正在进行的竞赛的一部分),而代码的相关部分仅在main函数中。

import java.io.*;
import java.util.*;

class TestClass {

    public static void main(String[] args) {
        InputReader sc = new InputReader(System.in);
        int Q=sc.nextInt();
        PriorityQueue<Integer> mn=new PriorityQueue<>();
        PriorityQueue<Integer> mx=new PriorityQueue<>(Collections.reverseOrder());
        int[] cnt =new int[100000+1];
        for (int q = 0; q < Q; q++) {
            String str=sc.nextLine();
            if(str.substring(0,4).equals("Push")) {
                int X=Integer.parseInt(str.substring(5));
                ++cnt[X];
                mx.add(X);
                mn.add(X);
            }
            else if (str.equals("Diff")) {
                if(mx.isEmpty()||mn.isEmpty())
                    out.println(-1);
                else {
                    int min = mn.poll();
                    int max = mx.poll();
                    if(min==max) {
                        --cnt[max];
                    }
                    else {
                        --cnt[min];
                        --cnt[max];
                    }
                    mn.remove(max);
                    mx.remove(min);
                    out.println(max-min);
                }
            }
            else if (str.equals("CountHigh")) {
                if(mx.isEmpty()) {
                    out.println(-1);
                }
                else {
                    out.println(cnt[mx.peek()]);
                }
            }
            else {
                if(mn.isEmpty()) {
                    out.println(-1);
                }
                else {
                    out.println(cnt[mn.peek()]);
                }
            }
//            System.out.println(q+" "+mx+" "+mn);
        }

        out.close();
    }
    static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
    static int mod = 1000000000+7;
    static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int snext() {
            if (snumChars == -1)
                throw new InputMismatchException();
            if (curChar >= snumChars) {
                curChar = 0;
                try {
                    snumChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (snumChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = snext();
            while (isSpaceChar(c)) {
                c = snext();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = snext();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = snext();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long nextLong() {
            int c = snext();
            while (isSpaceChar(c)) {
                c = snext();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = snext();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = snext();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public int[] nextIntArray(int n) {
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = nextInt();
            }
            return a;
        }

        public String readString() {
            int c = snext();
            while (isSpaceChar(c)) {
                c = snext();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = snext();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = snext();
            while (isSpaceChar(c))
                c = snext();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = snext();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        public double nextDouble() {
            return (Double.parseDouble(readString()));
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

mic*_*alk 4

在 Java 7 中,不存在PriorityQueueComparator作为参数的构造函数。查看Java 7 优先级队列文档。然而,在 Java 8+ 中,此类有这样的构造函数

您最好的选择是使用具有初始容量和 a 的构造函数Comparator

PriorityQueue<Integer> mx = new PriorityQueue<Integer>(10, Collections.reverseOrder());
Run Code Online (Sandbox Code Playgroud)