Example usage for java.util PriorityQueue PriorityQueue

List of usage examples for java.util PriorityQueue PriorityQueue

Introduction

In this page you can find the example usage for java.util PriorityQueue PriorityQueue.

Prototype

public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) 

Source Link

Document

Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

Usage

From source file:Main.java

public static void main(String args[]) {

    PriorityQueue<Integer> prq = new PriorityQueue<Integer>(10, Collections.reverseOrder());

    for (int i = 0; i < 10; i++) {
        prq.add(i);/* w  ww . ja v  a2s.  com*/
    }
    System.out.println(prq);
}

From source file:Main.java

public static void main(String args[]) {

    PriorityQueue<Integer> prq = new PriorityQueue<Integer>(10, Collections.reverseOrder());

    for (int i = 0; i < 10; i++) {
        prq.add(i);/*  www . j  av a2 s  .  c o m*/
    }

    PriorityQueue<Integer> p = new PriorityQueue<Integer>(prq);

    System.out.println(prq);
}

From source file:Main.java

public static void main(String... args) {
    PriorityQueue<Flight> flights = new PriorityQueue<Flight>(5, new SortQueueViaPriority());
    flights.add(new Flight("0001", 9));
    flights.add(new Flight("0002", 7));
    flights.add(new Flight("0003", 1));
    flights.add(new Flight("0004", 2));
    flights.add(new Flight("0005", 1));

    while (!flights.isEmpty())
        System.out.println(flights.remove());
}

From source file:PriorityQueueTester.java

public static void main(String[] args) {

    PriorityQueue<Integer> pq = new PriorityQueue<Integer>(20, new Comparator<Integer>() {
        public int compare(Integer i, Integer j) {
            int result = i % 2 - j % 2;
            if (result == 0)
                result = i - j;// w ww  .  jav  a 2 s  .  com
            return result;
        }
    });

    // Fill up with data, in an odd order
    for (int i = 0; i < 20; i++) {
        pq.offer(20 - i);
    }

    // Print out and check ordering
    for (int i = 0; i < 20; i++) {
        System.out.println(pq.poll());
    }
}

From source file:ComparablePerson.java

public static void main(String[] args) {
    int initialCapacity = 5;
    Comparator<ComparablePerson> nameComparator = Comparator.comparing(ComparablePerson::getName);

    Queue<ComparablePerson> pq = new PriorityQueue<>(initialCapacity, nameComparator);
    pq.add(new ComparablePerson(1, "Oracle"));
    pq.add(new ComparablePerson(4, "XML"));
    pq.add(new ComparablePerson(2, "HTML"));
    pq.add(new ComparablePerson(3, "CSS"));
    pq.add(new ComparablePerson(4, "Java"));

    System.out.println("Priority  queue: " + pq);

    while (pq.peek() != null) {
        System.out.println("Head  Element: " + pq.peek());
        pq.remove();/*from   w  w  w. ja v a 2 s . c  o  m*/
        System.out.println("Removed one  element from  Queue");
        System.out.println("Priority  queue: " + pq);
    }
}

From source file:amie.keys.CSAKey.java

public static void main(String[] args) throws IOException, InterruptedException {
    final Triple<MiningAssistant, Float, String> parsedArgs = parseArguments(args);
    final Set<Rule> output = new LinkedHashSet<>();

    // Helper object that contains the implementation for the calculation
    // of confidence and support
    // The file with the non-keys, one per line
    long timea = System.currentTimeMillis();
    List<List<String>> inputNonKeys = Utilities.parseNonKeysFile(parsedArgs.third);
    System.out.println(inputNonKeys.size() + " input non-keys");
    final List<List<String>> nonKeys = pruneBySupport(inputNonKeys, parsedArgs.second,
            parsedArgs.first.getKb());/*from   w w  w .  j  a  v  a 2 s.  co m*/
    Collections.sort(nonKeys, new Comparator<List<String>>() {

        @Override
        public int compare(List<String> o1, List<String> o2) {
            int r = Integer.compare(o2.size(), o1.size());
            if (r == 0) {
                return Integer.compare(o2.hashCode(), o1.hashCode());
            }

            return r;
        }

    });
    System.out.println(nonKeys.size() + " non-keys after pruning");
    int totalLoad = computeLoad(nonKeys);
    System.out.println(totalLoad + " is the total load");
    int nThreads = Runtime.getRuntime().availableProcessors();
    //int batchSize = Math.max(Math.min(maxBatchSize, totalLoad / nThreads), minBatchSize);
    int batchSize = Math.max(Math.min(maxLoad, totalLoad / nThreads), minLoad);

    final Queue<int[]> chunks = new PriorityQueue(50, new Comparator<int[]>() {
        @Override
        public int compare(int[] o1, int[] o2) {
            return Integer.compare(o2[2], o1[2]);
        }

    });

    final HashSet<HashSet<Integer>> nonKeysInt = new HashSet<>();
    final HashMap<String, Integer> property2Id = new HashMap<>();
    final HashMap<Integer, String> id2Property = new HashMap<>();
    final List<Integer> propertiesList = new ArrayList<>();
    int support = (int) parsedArgs.second.floatValue();
    KB kb = parsedArgs.first.getKb();
    buildDictionaries(nonKeys, nonKeysInt, property2Id, id2Property, propertiesList, support, kb);
    final List<HashSet<Integer>> nonKeysIntList = new ArrayList<>(nonKeysInt);
    int start = 0;
    int[] nextIdx = nextIndex(nonKeysIntList, 0, batchSize);
    int end = nextIdx[0];
    int load = nextIdx[1];
    while (start < nonKeysIntList.size()) {
        int[] chunk = new int[] { start, end, load };
        chunks.add(chunk);
        start = end;
        nextIdx = nextIndex(nonKeysIntList, end, batchSize);
        end = nextIdx[0];
        load = nextIdx[1];
    }

    Thread[] threads = new Thread[Math.min(Runtime.getRuntime().availableProcessors(), chunks.size())];
    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    int[] chunk = null;
                    synchronized (chunks) {
                        if (!chunks.isEmpty()) {
                            chunk = chunks.poll();
                        } else {
                            break;
                        }
                    }
                    System.out.println("Processing chunk " + Arrays.toString(chunk));
                    mine(parsedArgs, nonKeysIntList, property2Id, id2Property, propertiesList, chunk[0],
                            chunk[1], output);
                }

            }
        });
        threads[i].start();
    }

    for (int i = 0; i < threads.length; ++i) {
        threads[i].join();
    }
    long timeb = System.currentTimeMillis();
    System.out.println("==== Unique C-keys =====");
    for (Rule r : output) {
        System.out.println(Utilities.formatKey(r));
    }
    System.out.println(
            "VICKEY found " + output.size() + " unique conditional keys in " + (timeb - timea) + " ms");
}

From source file:Main.java

public static List<String> getSortedStringList(Collection<String> toSort) {
    List<String> sorted = new LinkedList<String>();
    final PriorityQueue<String> ordered = new PriorityQueue<String>(
            toSort.size() + 1/*In case the toSort is empty*/, new Comparator<String>() {
                @Override/*from  ww w .j av a 2 s  .  c  o  m*/
                public int compare(String lhs, String rhs) {
                    lhs = lhs.replaceAll("[^a-zA-Z0-9]", "");
                    rhs = rhs.replaceAll("[^a-zA-Z0-9]", "");
                    int result = rhs.compareTo(lhs);
                    return result;
                }
            });
    ordered.addAll(toSort);
    int originalSize = ordered.size();
    for (int i = 0; i < originalSize; i++) {
        sorted.add(ordered.poll());
    }
    return sorted;
}

From source file:Main.java

public static <E> PriorityQueue<E> getPriorityQueue(int initialCapacity, Comparator<? super E> comparator) {
    return new PriorityQueue<E>(initialCapacity, comparator);
}

From source file:manager.NoteManager.java

/**
 * Initializes the manager./* ww w .  j a  v  a  2  s.  co  m*/
 * 
 * @param filepath the filename to save the notes.
 * @throws IOException 
 */
public static void initManager(String filepath)
        throws IOException, ParseException, InstantiationException, IllegalAccessException {
    saveFile = filepath;
    modelQueue = new PriorityQueue(11, new NoteComparator());
    file = new File(filepath);
    JSONWriter.setFile(file);
    JSONReader.setFile(file);
    NoteManager.loadModels();
    ready = true;
}

From source file:Main.java

public static <E> PriorityQueue<E> newPriorityQueue(final int initialCapacity,
        final Comparator<? super E> comparator) {
    return new PriorityQueue<E>(initialCapacity, comparator);
}