int[]arr=newint[10];arr[1]=34;arr[1]=35;inti=arr[1];intsize=arr.length;// Fill array with a valueArrays.fill(arr,-1);// SortArrays.sort(arr);// AscendingArrays.sort(arr,(a,b)->b-a);// Descending
// -----------------Queue-----------------Queue<Integer>q=newLinkedList<>();q.add(1);q.remove();q.peek();// Without exceptionq.offer(2);q.poll()// -----------------Deque-----------------Deque<Integer>dq=newLinkedList<>();dq.addFirst(1);dq.addLast(2);intf1=dq.getFirst();intl1=dq.getLast();intf2=dq.removeFirst();intl2=dq.removeLast();
7. PriorityQueue
// BasicsPriorityQueue<Integer>pq=newPriorityQueue<>();PriorityQueue<Integer>maxPQ=newPriorityQueue<>(Comparator.reverseOrder());// Add/Offerpq.add(5);// Remove/PollIntegeritem=pq.remove();// Return without removingIntegeritem=pq.peek();// -----------------Inefficient-----------------// Containsbooleanexists=pq.contains(5);// Remove specific objectpq.remove(5);//--------------Custom comparators--------------------// Max heapPriorityQueue<Integer>maxHeap=newPriorityQueue<>(Comparator.reverseOrder());// Based on string lengthPriorityQueue<String>strPQByLength=newPriorityQueue<>((s1,s2)->s2.length()-s1.length());// Lexicographical OrderPriorityQueue<String>strPQLexi=newPriorityQueue<>();// Reverse Lexicographical OrderPriorityQueue<String>strPQLexiDesc=newPriorityQueue<>(Comparator.reverseOrder());// ----------------Custom Objects------------------classPerson{Stringname;intage;publicPerson(Stringname,intage){this.name=name;this.age=age;}}// Based on age in descending orderPriorityQueue<Person>personPQ=newPriorityQueue<>((p1,p2)->p2.age-p1.age);
Top comments (0)
Subscribe
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)