Java Gists

Concatenate Strings Using String.join() for Collections

There is an overloaded version of the String.join method that accepts as parameter an Iterable implementation. Since all Collection classes implement the Iterable interface, this method can be used to concatenate all the elements in a Collection. The following code demonstrates this: This code creates a List of String values...

Concatenate Strings Using String.join()

Java 8 has added a static join method to the String class. This concatenates Strings using a delimiter. Performance wise, it is similar to StringBuffer.append, but it has the added advantage that it automatically adds a delimiter. As before, this code creates string1 and string2. It invokes the join...

Concatenate Strings Using StringBuilder.append()

In addition to String, Java has the java.lang.StringBuffer and java.lang.StringBuilder classes. These can also be used to concatenate Strings. The advantage of these classes is that both these classes are mutable, so using them does not result in the creation of new String objects. So StringBuffer and StringBuilder...

Concatenate Strings Using StringBuffer.append()

In addition to String, Java has the java.lang.StringBuffer and java.lang.StringBuilder classes. These can also be used to concatenate Strings. The advantage of these classes is that both these classes are mutable, so using them does not result in the creation of new String objects. So StringBuffer and StringBuilder...

Concatenate Strings Using String.concat()

The String class has a method called concat. It accepts as parameter a String value and concatenates it with the current String. The following code demonstrates this: As before, this code creates string1 and string2. It then invokes the concat() method on string1 and passes string2 as an argument....

Concatenate Strings Using Plus Operator

In Java, a String is a sequence of characters. There are often programming situations where you will need to concatenate Strings. There are several ways in which you can achieve this. The + operator is also known as the concatenation operator. It can be used to concatenate Strings. The...

Remove Duplicates from List Using Stream

Java 8 has added the Stream API that helps to easily perform bulk operations on Collections. A new method called stream() method has been added to all the collection interfaces that returns a Stream corresponding to the underlying collection. The Stream interface has a method called distinct that can...

Convert List to Array Using ToArray With Param

There is an overloaded version of the toArray method. This accepts an array as a parameter and returns a result array that is of the same data type as the input array. So basically, this toArray  method can be used to obtain an array that is of the same data type...

Remove Duplicates from List Using Set.addAll

The Set interface has a method called addAll. This accepts a 'Collection' as the parameter. So if you invoke this method by passing the 'List', the duplicates will be eliminated. The following code snippet demonstrates this method. Here, a new LinkedHashSet is created and the addAll method is invoked by...

Convert List to Array Using ToArray Without Param

Java provides a toArray method on the 'List' interface. This can be used to convert a 'List' to an array. Since there are are two overloaded versions, one of these method is explained below. This toArray method does not accept any input parameters. It returns an object array. The following...

Remove Duplicates from List Using LinkedHashSet

Another implementation of the Set interface is LinkedHashSet. LinkedHashSet maintains the order of elements and helps to overcome the HashSet limitation. The following code demonstrates this: This code is quite similar to the one seen earlier, except that it uses a LinkedHashSet. The input List is passed as a parameter...

Convert List to Array Using Stream with Param

There is an overloaded version of the Stream.toArray method which can be used to return a result array that is of the same data type as the input array. The following code demonstrates this: Firstly, the 'stream()' method is invoked on the input List which returns a Stream corresponding to...

POPULAR | JAVA GISTS

POPULAR | TAGS

LATEST POSTS

CronTab Basic Commands With Examples

Cron is a time-based job scheduler in Unix-like operating systems, which is used to automate repetitive tasks. It is a daemon (a background process)...

Basic PowerShell Commands That You Should Know

One of the key benefits of PowerShell is its object-oriented nature. Instead of working with text-based output like traditional command-line interfaces, PowerShell returns objects...

How To Always Open Files in New Tab in VSCode

The VSCode has rocketed its way to one of the top development IDEs due to its fresh look, usability, tons of feature sets, and...

TOP | CATEGORIES

Most Commented

Interpreted Vs Compiled Languages

This is based on an excerpt from one of my favourite literature on VBScript and in fact, is quite relevant with respect to one of...