Java Advanced Sorting

Java Advanced Sorting

In the List Sorting Article, you learned how to sort lists alphabetically and numerically, but what if the list has objects in it?

To sort objects you need to specify a rule that decides how objects should be sorted. For example, if you have a list of cars you might want to sort them by year, the rule could be that cars with an earlier year go first.

The Comparator and Comparable interfaces allow you to specify what rule is used to sort objects.

Being able to specify a sorting rule also allows you to change how strings and numbers are sorted.


Continue reading Java Advanced Sorting

Java Lambda Expressions

Java Lambda Expressions

Lambda Expressions were added in Java 8.

A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.


Continue reading Java Lambda Expressions

Java Threads

Java Threads

Threads allows a program to operate more efficiently by doing multiple things at the same time.

Threads can be used to perform complicated tasks in the background without interrupting the main program.


Continue reading Java Threads

Java Regular Expressions

What is a Regular Expression?

A regular expression is a sequence of characters that forms a search pattern. When you search for data in a text, you can use this search pattern to describe what you are searching for.

A regular expression can be a single character, or a more complicated pattern.

Regular expressions can be used to perform all types of text search and text replace operations.

Java does not have a built-in Regular Expression class, but we can import the java.util.regex package to work with regular expressions. The package includes the following classes:

  • Pattern Class – Defines a pattern (to be used in a search)
  • Matcher Class – Used to search for the pattern
  • PatternSyntaxException Class – Indicates syntax error in a regular expression pattern

Continue reading Java Regular Expressions

Java Exceptions – Try…Catch

Java Exceptions

When executing Java code, different errors can occur: coding errors made by the programmer, errors due to wrong input, or other unforeseeable things.

When an error occurs, Java will normally stop and generate an error message. The technical term for this is: Java will throw an exception (throw an error).


Continue reading Java Exceptions – Try…Catch

Java Wrapper Classes

Java Wrapper Classes

Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.

The table below shows the primitive type and the equivalent wrapper class:

Primitive Data Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

Sometimes you must use wrapper classes, for example when working with Collection objects, such as ArrayList, where primitive types cannot be used (the list can only store objects): Continue reading Java Wrapper Classes

Java Iterator

Java Iterator

An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet. It is called an “iterator” because “iterating” is the technical term for looping.

To use an Iterator, you must import it from the java.util package.


Continue reading Java Iterator

Java HashSet

Java HashSet

A HashSet is a collection of items where every item is unique, and it is found in the java.util package:

Example

Create a HashSet object called cars that will store strings:

import java.util.HashSet; // Import the HashSet class

HashSet<String> cars = new HashSet<String>();

Continue reading Java HashSet

Java HashMap

Java HashMap

In the ArrayList chapter, you learned that Arrays store items as an ordered collection, and you have to access them with an index number (int type). A HashMap however, store items in “key/value” pairs, and you can access them by an index of another type (e.g. a String).

One object is used as a key (index) to another object (value). It can store different types: String keys and Integer values, or the same type, like: String keys and String values: Continue reading Java HashMap

Java List Sorting

Java Sort a List

In the previous chapters, you learned how to use two popular lists in Java: ArrayList and LinkedList, which are found in the java.util package.

Another useful class in the java.util package is the Collections class, which include the sort() method for sorting lists alphabetically or numerically.


Continue reading Java List Sorting