Top 25 Useful Java Classes

In this article, we will discuss common and frequently used classes in  Java libraries.
There are no strict rules for the selection, in fact, there are no rules followed. Classes that popped up on top of mind are listed below. You are welcome to add your own list. This list will vary depending on the type of java project you work on.

1. java.lang.String

As we know Strings are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.  The Java platform provides the  java.lang.String class to create and manipulate strings. Strings are constant; their values cannot be changed after they are created.
For Example, two ways to create a String object:
String s="Java Guides";
String str = new String("Java Guides");

Read more about Java String class and it's methods with examples at Java String Class API Guide.
JavaDoc at https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html.

2. java.lang.Object

The Object class, in the java.lang package sits at the top of the class hierarchy tree. Every class is a descendant, direct or indirect, of the Object class. Every class you use or write inherits the instance methods of Object. You need not use any of these methods, but, if you choose to do so, you may need to override them with code that is specific to your class.
Read more about Java Object class and it's methods with examples at Object Class Methods in Java with Examples.
Javadoc at https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html.

3. java.lang.Thread

Thread class is used to create a new thread of execution. It implements the Runnable interface. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Read more about Java Thread class and it's methods with examples at Thread Class in Java with Examples.
Javadoc at https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html.

4. java.util.ArrayList

Java ArrayList is one of the most widely used Collection class. java.util.ArrayList class implements java.util.List interface. Java ArrayList also implements RandomAccess, Cloneable, and Serializable interfaces. Java ArrayList class extends AbstractList class that is the skeleton implementation of List interface.
The important points about Java ArrayList class are:
  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non-synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation is slow because a lot of shifting needs to have occurred if any element is removed from the array list.
Read more about Java ArrayList class and it's methods with examples at Guide to ArrayList Class.

5. java.util.HashMap

Java HashMap is one of the most popular Collection classes in java. Java HashMap is Hash table based implementation. HashMap in java extends AbstractMap class that implements Map interface.
The important points about Java HashMap class:
  • HashMap contains values based on the key.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It maintains no order.
  • Java HashMap is not thread-safe. You must explicitly synchronize concurrent modifications to the HashMap.
Read more about Java HashMap class and it's methods with examples at Guide to HashMap Class.
Javadoc at https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/HashMap.html.

6. java.lang.Exception

The Exception class and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

Below are Java built-in commonly used exceptions:
Read more above Java exceptions at Java Exception Handling Tutorial.

7. java.util.HashSet

Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSetclass and implements Set interface.
The important points about Java HashSet class are:
  • HashSet stores the elements by using a mechanism called hashing.
  • HashSet contains unique elements only.
  • Difference between List and Set
  • List can contain duplicate elements whereas Set contains unique elements only.
Read more about Java HashSet class and it's methods with examples at Guide to HashSet Class.

8. java.lang.StringBuilder

Java StringBuilder class is used to create a mutable (modifiable) string. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5.
Read more about Java StringBuilder class and it's methods with examples at Java StringBuilder Class API Guide.

9. java.util.Arrays

The java.util.Arrays class contains a static factory that allows arrays to be viewed as lists. Following are the important points about Arrays -
  1. This class contains various methods for manipulating arrays (such as sorting and searching).
  2. The methods in this class throw a NullPointerException if the specified array reference is null.
Read more about Java Arrays class and it's methods with examples at java.util.Arrays Class API Guide.

10. java.util.Collections

java.util.Collections class consists exclusively of static methods that operate on or return collections.  It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends.
The methods of Collections class all throw a NullPointerException if the collections or class objects provided to them are null.
Read more about Java Arrays class and it's methods with examples at java.util.Collections Class API Guide.

11. java.time.LocalDate

LocalDate represents a year-month-day in the ISO calendar and is useful for representing a date without a time. You might use a LocalDate to track a significant event, such as birth date or wedding date.
Read more about Java LocalDate class and it's methods with examples at Java 8 - LocalDate Class API Guide.

12. java.time.LocalDateTime

The java.time.LocalDateTime class is an immutable class which represents a date-time without time-zone information such as ‘2018-08-12T10:35:55’.
This is the most commonly used class when we need a combination of date and time. The class offers a variety of APIs and we will look at some of the most commonly used ones.
Read more about Java LocalDateTime class and it's methods with examples at Java 8 - LocalDateTime Class API Guide.

13. java.nio.file.Files

The Java NIO Files class (java.nio.file.Files) provides several methods for manipulating files in the file system. This class consists exclusively of static methods that operate on files, directories, or other types of files.
Read more about Java Files class and it's methods with examples at Java NIO Files Class API Guide.
The File class is an abstract representation of file and directory pathname. A pathname can be either absolute or relative.

The File class has several methods for working with directories and files such as creating new directories or files, deleting and renaming directories or files, listing the contents of a directory, etc.

15. java.util.Scanner

Scanner class in Java is found in the java.util package. Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them.

The Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default. It provides many methods to read and parse various primitive values.

The Java Scanner class is widely used to parse text for strings and primitive types using a regular expression. It is the simplest way to get input in Java. By the help of Scanner in Java, we can get input from the user in primitive types such as int, long, double, byte, float, short, etc.

16. java.lang.System

The System class of java contains several useful class fields and methods. It also provides facilities like standard input, standard output, and error output Streams. It can't be instantiated. The Java System class comes in the module of "java.base" & in the package of "java.lang".

17. java.util.Iterator

This is an interface. It is very popular and came as a replacement for Enumeration. It is simple to use convenience utility and works in sync with Iterable. It was introduced in JDK 1.2.

18. java.lang.Runnable

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run. This is a functional interface and therefore it can be used as the assignment target for a lambda expression or method reference.
Javadoc at https://docs.oracle.com/javase/9/docs/api/java/lang/Runnable.html.

19. java.lang.Class

Instances of the class Class represent classes and interfaces in a running Java application. An enum type is a kind of class and an annotation type is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
The following example uses a Class object to print the class name of an object:
     void printClassName(Object obj) {
         System.out.println("The class of " + obj +
                            " is " + obj.getClass().getName());
     }

20. java.net.HttpURLConnection

HttpURLConnection class from java.net package can be used to send Java HTTP Request programmatically. Check out Java HttpURLConnection Class Example – Java HTTP Request GET, POST article, here you will see how to use HttpURLConnection in java program to send GET and POST requests and then print the response.

21. java.io.BufferedReader 

Java BufferedReader class is used to read the text from a character-based input stream. It can be used to read the data line by line by readLine() method. It makes the performance fast.

22. java.net.URL

Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine.
Read more at Javadoc java.net.URL.

23. java.io.IOException

This class signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.
Read more at Javadoc java.io.IOException.

24. java.util.LinkedList

Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.
The important points about Java LinkedList are:
  • Java LinkedList class can contain duplicate elements.
  • Java LinkedList class maintains insertion order.
  • In Java LinkedList class, manipulation is fast because no shifting needs to have occurred.
  • The LinkedList class implements Queue and Deque interfaces. Therefore, It can also be used as a Queue, Deque or Stack.
  • Java LinkedList is not thread-safe. You must explicitly synchronize concurrent modifications to the LinkedList in a multi-threaded environment.

25. java.util.concurrent.Executors

Executors Class provides factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable classes defined in this package.
This class supports the following kinds of methods:
  • Methods that create and return an ExecutorService set up with commonly useful configuration settings.
  • Methods that create and return a ScheduledExecutorService set up with commonly useful configuration settings.
  • Methods that create and return a "wrapped" ExecutorService, that disables reconfiguration by making implementation-specific methods inaccessible.
  • Methods that create and return a ThreadFactory that sets newly created threads to a known state.
  • Methods that create and return a Callable out of other closure-like forms, so they can be used in execution methods requiring Callable.

26. java.util.Optional class

Java 8 has introduced a new class Optional in java.util package. It can help in writing a neat code without using too many null checks. The purpose of the class is to provide a type-level solution for representing optional values instead of null references.
Advantages of Java 8 Optional:
  • Null checks are not required.
  • No more NullPointerException at run-time.
  • We can develop a clean and neat APIs.
  • No more Boilerplate code

27. java.util.stream.Stream

Java provides a new additional package in Java 8 called java.util.stream. This package consists of classes, interfaces, and an enum to allows functional-style operations on the elements.

The java.util.stream.Stream interface provides many APIs to filter, collect, print, and convert from one data structure to other etc. Check out Javadoc at https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html.

Read more about java.util.stream package at https://www.javaguides.net/2018/07/java-8-stream-api.html.

Comments