We always love working with Strings in our own programs. Strings is one of the most common data structure in most programming language. One common thing we wish to do with String is to clean it up. One common way of cleaning up Strings is by removing spaces from the beginning and the end of the value. For example, when we want to save first name or last name, having spaces at the beginning or the end of a name doesn't make sense. So we want to remove them. This is where the Java String's Trim() method comes in handy. This is what it exactly do. Below are more discussion about how to use Trim method of Java String.
The switch statement is a beautiful alternative to multiple if else, when all we want to do is match a variable's value to one of many values, to determine what to do. The switch statement is both concise, readable, and have better performance compared to multiple if else version. One issue though, before Java 7 was introduced, we can't use String value with switch statements. That is not fun as String is a well used data type. Sometimes we don't want to define enum and just want to work with Strings. Below are some way on how to use String in Java Switch statements.
Most String manipulation problems requires us to come up with combination of information and summarize into one String. For example, imagine we wish to convert an integer value into words as String, this will require us to combine multiple Strings to come up with an answer String. What is common to this is we need to append one String to another, to form a closer form to the answer. Below we explore how to do String Append in Java.
When we are programming with String, or doing String manipulation in short, a typical problem we encounter is we wish to concatenate together some Strings using a specific delimiter. For example, if we have an array of 4 String representing fruits [Apple, Banana, Orange, Grapes], and wish to concatenate them using delimiter dash "-", the resulting String we want is "Apple-Banana-Orange-Grapes". Good thing we don't need to implement our own, nor we don't need an external library from Apache or Google. There is already a method we can use inside Java - String Join. Below are some examples on how we could concatenate a list of String using a specific delimiter using Java String Join. Note that this feature only exists from Java 8 and above.
List in Java is a powerful data structure because it can hold arbitrary number of Objects. Unlike Arrays, List in Java can be resized at any point in time. For example, if we have a Java List of String, depending on the implementation, we can add as many String object as we want into the List. Contrast it with Arrays, if we declared an array of size 10, then we can only have around 10 Objects in an Array. If we want to have more, then we need to recreate the array. But for example, in Java List of String, we can add as many String instance as we want into the List as memory limitation would allow, without needing to recreate the data structure. Below are some examples on how we work with Java List of String.
Java Enums is a new feature introduced in Java 5. Using Java Enum can help define a collection of constants in a more logical and meaningful way. Using Java Enum is a fluent way of defining constants that makes things more readable and maintainable. But sometimes we also need to convert a Java Enum to String, and that is what we will show in the examples below:
Java hashCode() method has a close relationship with the equals() method as they complement and are used together. On a high level, the equals method should inspect the relevant data of two instances to determine if they are logically the same object. What internal data the equals() method used should also be used to generate the hashCode() value of a specific object instance. Below are more detailed discussion with code examples about Java hashCode.
When we are dealing with a handful of data of the same type, we can use a different variable for each. But if we are working with arbitrarily more numbers of data of same type, array can be a good choice because it is a simple data structure to work with. List or Set is also a good choice, but arrays is much simpler to manipulate and work with. We explore below how do we work with Java Char Array. We will check different common scenario we encounter when working with char arrays.
Although a very old and dated data structure, array is still a very popular data structure to work with a collection of objects. This is because manipulation of arrays is very simple to learn and use. The only issue is it lacks advance features than can be found in the Collections API. For this tutorial, we will show how to Add elements to an Array in Java.
Byte Array is a convenient data structure that has many use cases. One scenario is when serializing some object to a file, and so we need to convert object to byte array when we write to file, and convert back from byte array to objects when we read from file. Another scenario is when sending data from one machine to another. Complex data structure is converted to a byte array before sending over the network, and on the other end the byte array is converted back to the original data structure. One of the simplest type of Object that we can convert to and from byte array is a String. This post will show how to convert Java Byte Array To String that can be applied to the use cases mentioned earlier.