Optional In Java Tutorial

Optional is a container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value. Additional methods that depend on the presence of the contained value are provided, such as orElse(), which returns a default value if value not present, and … Read more

Alternative Collections in Java Tutorial

Multimap in Guava, Apache and Eclipse Collections This multimap allows duplicate key-value pairs. JDK analogs are HashMap, HashMap and so on. Examples using Multimap Task: Parse “Hello World! Hello All! Hi World!” string to separate words and print all indexes of every word using MultiMap (for example, Hello=[0, 2], World!=[1, 5] and so on) MultiValueMap … Read more

Documenting Java Code Tutorial

Documentation for java code is often generated using Javadoc. Javadoc was created by Sun Microsystems for the purpose of generating API documentation in HTML format from Java source code. Using the HTML format gives the convenience of being able to hyperlink related documents together. Building Javadocs From the Command Line Many IDEs provide support for … Read more

Interfaces In Java Tutorial

Java Interfaces Tutorial – An interface is a reference type, similar to a class, which can be declared by using the interface keyword. Interfaces can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Like abstract classes, Interfaces cannot be instantiated—they can … Read more

Scanner In Java Tutorial

In this Java Scanner Tutorial, we will learn about Java Scanner and its methods with the help of examples. Parameter Details Source Source could be either one of String, File or any kind of InputStream General Pattern that does most commonly asked about tasks The following is how to properly use the java.util.Scanner class to … Read more

New File I/O in Java with Examples

Creating paths The Path class is used to programmatically represent a path in the file system (and can therefore point to files as well as directories, even to non-existent ones) A path can be obtained using the helper class Paths: Path p1 = Paths.get(“/var/www”);Path p2 = Paths.get(URI.create(“file:///home/testuser/File.txt”));Path p3 = Paths.get(“C:\Users\DentAr\Documents\HHGTDG.odt”);Path p4 = Paths.get(“/home”, “arthur”, “files”, … Read more

Lambda Expressions in Java

Lambda expressions provide a clear and concise way of implementing a single-method interface using an expression. They allow you to reduce the amount of code you have to create and maintain. While similar toanonymous classes, they have no type information by themselves. Type inference needs to happen. Method references implement functional interfaces using existing methods … Read more

The Java Exception Hierarchy – Unchecked and Checked Exceptions

All Java exceptions are instances of classes in the Exception class hierarchy. This can be represented as follows: java.lang.Throwable – This is the base class for all exception classes. Its methods and constructors implement a range of functionality common to all exceptions. java.lang.Exception – This is the superclass of all normal exceptions. various standard and … Read more

Using the Static Keyword In Java Tutorial

The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class. Reference to non-static member from static context Static variables and methods are not part of an instance, There will always be a single copy of … Read more

Super Keyword in Java

Super keyword use with examples super keyword performs important role in three places Constructor Level Method Level Variable Level Constructor Level super keyword is used to call parent class constructor. This constructor can be default constructor or parameterized constructor. Default constructor : super(); Parameterized constructor : super(int no, double amount, String name); class Parentclass { … Read more

Choosing Collections in Java

Java offers a wide variety of Collections. Choosing which Collection to use can be tricky. See the Examples section for an easy-to-follow flowchart to choose the right Collection for the job. Java Collections Flowchart Use the following flowchart to choose the right Collection for the job.

Concurrent Collections in Java

A concurrent collection is a [collection][1] which permits access by more than one thread at the same time. Different threads can typically iterate through the contents of the collection and add or remove elements. The collection is responsible for ensuring that the collection doesn’t become corrupt. Thread-safe Collections By default, the various Collection types are … Read more