0% completed
Java provides several utility classes that contain useful static methods for performing common operations such as working with collections, handling strings, generating random numbers, and more. These classes help in reducing boilerplate code and improving efficiency by providing ready-to-use functionalities.
Below is a list of essential utility classes in Java, their purpose, key methods, and simple examples.
Utility Class | Package | Purpose |
---|---|---|
Arrays | java.util | Utility methods for working with arrays |
Collections | java.util | Utility methods for working with collections (lists, sets, etc.) |
Math | java.lang | Mathematical functions like rounding, trigonometry, etc. |
Random | java.util | Generating random numbers |
String | java.lang | String manipulation methods |
StringBuilder | java.lang | Efficient string operations for mutable strings |
The Arrays
class provides static methods for array operations such as sorting, searching, and conversion.
Method | Description |
---|---|
sort(array) | Sorts the array in ascending order |
binarySearch(array, key) | Searches for a key in a sorted array and returns its index |
asList(array) | Converts an array into a List |
copyOf(array, newLength) | Copies an array into a new array of given length |
The Collections
class provides utility methods for working with collections like lists and sets.
Method | Description |
---|---|
sort(List) | Sorts a list in ascending order |
reverse(List) | Reverses the elements of a list |
max(Collection) | Returns the maximum element |
min(Collection) | Returns the minimum element |
shuffle(List) | Randomly shuffles the elements of a list |
The Math
class provides mathematical functions like exponentiation, rounding, and trigonometry.
Method | Description |
---|---|
abs(value) | Returns absolute value |
pow(base, exp) | Returns base raised to the power of exp (base^exp ) |
sqrt(value) | Returns the square root |
ceil(value) | Rounds up to the nearest integer |
floor(value) | Rounds down to the nearest integer |
The Random
class is used for generating random numbers.
Method | Description |
---|---|
nextInt(bound) | Returns a random integer within the given bound |
nextDouble() | Returns a random double between 0.0 and 1.0 |
nextBoolean() | Returns a random boolean value |
The String
class provides various methods for manipulating strings.
Method | Description |
---|---|
length() | Returns the length of the string |
toUpperCase() | Converts all characters to uppercase |
toLowerCase() | Converts all characters to lowercase |
substring(start, end) | Extracts a substring |
replace(old, new) | Replaces occurrences of a character or string |
The StringBuilder
class is used for efficient string modifications.
Method | Description |
---|---|
append(text) | Appends text to the string |
insert(index, text) | Inserts text at a given index |
replace(start, end, text) | Replaces part of the string |
reverse() | Reverses the string |
Mastering these utility classes enhances productivity and enables efficient coding in Java.
.....
.....
.....