Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve error: 15: error: cannot infer type arguments for PriorityQueue<> in openjdk 1.7.0_95?

I am solving a question on hackerearth and it is successfully compiling and passing when I am using java version Java 8 (oracle 1.8.0_131) but when Java (openjdk 1.7.0_95) is used it gives an error 15: error: cannot infer type arguments for PriorityQueue<> .The error is on the line when mx priority queue is being declared. I want to know how to resolve it and why this error occurs. Here is the code : (note that this question is not part of any ongoing contest) and the relevant part of the code is in main function only.

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);
        }
    }
}
like image 990
fuzious Avatar asked Jul 28 '19 15:07

fuzious


2 Answers

In Java 7 there is no PriorityQueue constructor that takes only Comparator as an argument. Take a look Java 7 Priority queue docs. However in Java 8+ there is such constructor for this class.

Your best choice would be to use constructor that takes initial capacity and a Comparator :

PriorityQueue<Integer> mx = new PriorityQueue<Integer>(10, Collections.reverseOrder());
like image 155
Michał Krzywański Avatar answered Oct 24 '22 03:10

Michał Krzywański


That constructor was added in java-8, so there is no way this would work against 1.7.

There is a feature called target type that was added in java-8, but that is un-related to your question; so it is as simple as adding one more constructor parameter for example, like initial capacity.

PriorityQueue<Integer> mx = new PriorityQueue<>(5, Collections.reverseOrder());
like image 34
Eugene Avatar answered Oct 24 '22 03:10

Eugene