Change priorityQueue to max priorityqueue

How about like this: PriorityQueue<Integer> queue = new PriorityQueue<>(10, Collections.reverseOrder()); queue.offer(1); queue.offer(2); queue.offer(3); //… Integer val = null; while( (val = queue.poll()) != null) { System.out.println(val); } The Collections.reverseOrder() provides a Comparator that would sort the elements in the PriorityQueue in a the oposite order to their natural order in this case.

Why is a ConcurrentModificationException thrown and how to debug it

This is not a synchronization problem. This will occur if the underlying collection that is being iterated over is modified by anything other than the Iterator itself. Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Entry item = it.next(); map.remove(item.getKey()); } This will throw a ConcurrentModificationException when the it.hasNext() is called the second time. The correct … Read more

Union or intersection of Java Sets

The simplest one-line solution is this: set1.addAll(set2); // Union set1.retainAll(set2); // Intersection The above solution is destructive, meaning that contents of the original set1 my change.If you don’t want to touch your existing sets, create a new set: Set<E> result = new HashSet<>(set1); // └─ your specific type result.addAll(set2); // Union result.retainAll(set2); // Intersection