In Java, a utility class is a class that defines a set of methods that perform common functions. This post shows the most frequently used Java utility classes and their most commonly used methods. Both the class list and their method list are ordered by popularity. The data is based on randomly selected 50,000 open source Java projects from GitHub.
Month: December 2015
LeetCode – Count of Smaller Numbers After Self (Java)
You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i]. Example: Input: [5,2,6,1] Output: [2,1,1,0] Java Solution 1 public List<Integer> countSmaller(int[] nums) { List<Integer> result = new ArrayList<Integer>(); ArrayList<Integer> sorted … Read more
Top 10 API Related Questions from Stack Overflow
Stack Overflow is a large repository of valuable programming knowledge. Millions of questions have been answered on Stack Overflow and many answers have very high quality. This is the reason that Stack Overflow answers are often located on the top of the search result from Google.
Java Dependency Injection Example
Dependency injection (DI) is a technique in which a class receives its dependency from outside. If class A uses class B, class A is dependent on class B, and B is a dependency of A.
LeetCode – Minimum Increment to Make Array Unique (Java)
Java Solution 1 public int minIncrementForUnique(int[] A) { Arrays.sort(A); int count = 0; for(int i=1; i<A.length; i++){ if(A[i]<=A[i-1]){ count+=A[i-1]+1; A[i]=A[i]+1; } } return count; }public int minIncrementForUnique(int[] A) { Arrays.sort(A); int count = 0; for(int i=1; i<A.length; i++){ if(A[i]<=A[i-1]){ count+=A[i-1]+1; A[i]=A[i]+1; } } return count; } Actually we do not need to increment … Read more
LeetCode – Most Stones Removed with Same Row or Column (Java)
The easiest solution for this problem is the union-find. The number of island problem can help understand how union-find works. The basic idea is that we use a disjoint set to track each component. Whenever two stones can be connected, the number of islands decrease one. The final result, the number of movement, is the … Read more