Sort a String LinkedList in Java

Sorting a string LinkedList in Java is easy. You can sort the string LinkedList in ascending alphabetical order by using sort(List<T> list) . You can also sort the string LinkedList in descending alphabetical order by using sort(List<T> list, Comparator<? super T> c) . Here is the code: public static void main(String[] args) { LinkedList<String> stringList … Read more

Java PriorityQueue Class Example

In Java, the PriorityQueue class is implemented as a priority heap. Heap is an important data structure in computer science. For a quick overview of heap, here is a very good tutorial. 1. Simple Example The following examples shows the basic operations of PriorityQueue such as offer(), peek(), poll(), and size(). import java.util.Comparator; import java.util.PriorityQueue; … Read more

The Interface and Class Hierarchy Diagram of Java Collections

1. Collection vs Collections First of all, “Collection” and “Collections” are two different concepts. As you will see from the hierarchy diagram below, “Collection” is a root interface in the Collection hierarchy but “Collections” is a class which provide static methods to manipulate on some Collection types. 2. Class hierarchy of Collection The following diagram … Read more

Simple practice with Scanner in Java

What is the result of the following program? import java.util.Scanner; public class Looking { public static void main(String[] args){ String input = "1 2 a 3 45 6"; Scanner sc = new Scanner(input); int x = 0; do{ x = sc.nextInt(); System.out.print(x + " "); }while(x != 0); } }import java.util.Scanner; public class Looking { … Read more