Advanced

Data structures

Array List

Dynamic array
Elements are stored in a contiguous block of memory -> Fast random access

// Create a new ArrayList
ArrayList<String> myList = new ArrayList<>();

// Add elements to the list
myList.add("apple");
myList.add("banana");
myList.add("cherry");

// Print the list
System.out.println("My array list: " + myList);

// Get the element at index 1
String element = myList.get(1);
System.out.println("Element at index 1: " + element);

// Set the element at index 2 to "pineapple"
myList.set(2, "pineapple");
System.out.println("My array list after setting element at index 2 to 'pineapple': " + myList);

// Remove the element at index 0
String removed = myList.remove(0);
System.out.println("Removed element: " + removed);

// Add an element to the beginning of the list
myList.add(0, "orange");
System.out.println("My array list after adding an element to the beginning: " + myList);

// Check if the list contains a specific element
boolean contains = myList.contains("banana");
System.out.println("Does the list contain 'banana'? " + contains);

// Get the size of the list
int size = myList.size();
System.out.println("Size of the list: " + size);
My array list: [apple, banana, cherry]
Element at index 1: banana
My array list after setting element at index 2 to 'pineapple': [apple, banana, pineapple]
Removed element: apple
My array list after adding an element to the beginning: [orange, banana, pineapple]
Does the list contain 'banana'? true
Size of the list: 3

LinkedList

Elements are stored as nodes -> Fast insertion and deletion

// Create a new linked list
LinkedList<String> myList = new LinkedList<>();

// Add elements to the list
myList.add("apple");
myList.add("banana");
myList.add("cherry");

// Print the list
System.out.println("My linked list: " + myList);

// Get the first element of the list
String first = myList.getFirst();
System.out.println("First element: " + first);

// Get the last element of the list
String last = myList.getLast();
System.out.println("Last element: " + last);

// Remove the first element of the list
String removed = myList.removeFirst();
System.out.println("Removed element: " + removed);

// Add an element to the beginning of the list
myList.addFirst("orange");
System.out.println("My linked list after adding an element to the beginning: " + myList);

// Check if the list contains a specific element
boolean contains = myList.contains("banana");
System.out.println("Does the list contain 'banana'? " + contains);

// Get the size of the list
int size = myList.size();
System.out.println("Size of the list: " + size);
My linked list: [apple, banana, cherry]
First element: apple
Last element: cherry
Removed element: apple
My linked list after adding an element to the beginning: [orange, banana, cherry]
Does the list contain 'banana'? true
Size of the list: 3

HashMap

Stores key-value pairs

// Create a new HashMap
HashMap<String, Integer> myMap = new HashMap<>();

// Add key-value pairs to the map
myMap.put("apple", 1);
myMap.put("banana", 2);
myMap.put("cherry", 3);

// Print the map
System.out.println("My HashMap: " + myMap);

// Get the value for the "banana" key
int value = myMap.get("banana");
System.out.println("Value for key 'banana': " + value);

// Replace the value for the "apple" key
myMap.replace("apple", 4);
System.out.println("My HashMap after replacing value for key 'apple': " + myMap);

// Remove the "cherry" key-value pair
int removed = myMap.remove("cherry");
System.out.println("Removed value: " + removed);

// Add a new key-value pair to the map
myMap.put("orange", 5);
System.out.println("My HashMap after adding a new key-value pair: " + myMap);

// Check if the map contains a specific key
boolean contains = myMap.containsKey("banana");
System.out.println("Does the map contain key 'banana'? " + contains);

// Get the size of the map
int size = myMap.size();
System.out.println("Size of the map: " + size);
My HashMap: {banana=2, cherry=3, apple=1}
Value for key 'banana': 2
My HashMap after replacing value for key 'apple': {banana=2, cherry=3, apple=4}
Removed value: 3
My HashMap after adding a new key-value pair: {orange=5, banana=2, apple=4}
Does the map contain key 'banana'? true
Size of the map: 3

HashSet

Store unique elements

// Create a new HashSet
HashSet<String> mySet = new HashSet<>();

// Add elements to the set
mySet.add("apple");
mySet.add("apple"); // "apple" only appears once
mySet.add("banana");
mySet.add("cherry");

// Print the set
System.out.println("My HashSet: " + mySet);

// Remove an element from the set
boolean removed = mySet.remove("banana");
System.out.println("Removed 'banana' from the set? " + removed);
System.out.println("My HashSet after removing 'banana': " + mySet);

// Add a new element to the set
boolean added = mySet.add("orange");
System.out.println("Added 'orange' to the set? " + added);
System.out.println("My HashSet after adding 'orange': " + mySet);

// Check if the set contains a specific element
boolean contains = mySet.contains("apple");
System.out.println("Does the set contain 'apple'? " + contains);

// Get the size of the set
int size = mySet.size();
System.out.println("Size of the set: " + size);

// Clear the set
mySet.clear();
System.out.println("My HashSet after clearing it: " + mySet);
My HashSet: [banana, cherry, apple]
Removed 'banana' from the set? true
My HashSet after removing 'banana': [cherry, apple]
Added 'orange' to the set? true
My HashSet after adding 'orange': [orange, cherry, apple]
Does the set contain 'apple'? true
Size of the set: 3
My HashSet after clearing it: []

Exceptions

try {
    // Some code that may throw an exception

    // ArithmeticException
    int result = 5 / 0;

    // NullPointerException
    String str = null;
    int length = str.length();

    // ArrayIndexOutOfBoundsException
    int[] arr = {1, 2, 3};
    int element = arr[3];

} catch (ArithmeticException e) {
    // Handle arithmetic exception
    System.out.println("Arithmetic exception occurred: " + e.getMessage());
} catch (NullPointerException e) {
    // Handle null pointer exception
    System.out.println("Null pointer exception occurred: " + e.getMessage());
} catch (Exception e) {
    // Handle all other types of exceptions
    System.out.println("Some other exception occurred: " + e.getMessage());
} finally {
    // This block of code will always execute, regardless of whether an exception was thrown or not
    System.out.println("Finally block executed.");
}

RegEx

Match pattern in string

Match

String inputString = "Hello, Hiep";
String patternString = "Hello.*";

// Compile the regex pattern into a Pattern object
Pattern pattern = Pattern.compile(patternString);

// Create a Matcher object with the input string
Matcher matcher = pattern.matcher(inputString);

// Perform pattern matching using matches() method
boolean isMatched = matcher.matches();
System.out.println(isMatched ? "Pattern matched!" : "Pattern not matched.");
String inputString = "Hello, Hiep";
String patternString = "Hello.*";

// Perform pattern matching using matches() method
boolean isMatched = inputString.matches(patternString);
System.out.println(isMatched ? "Pattern matched!" : "Pattern not matched.");

Find

String inputString = "Hello, Ha!";
String patternString = "\\bHa\\b"; // Regex pattern to find person named Ha

// Compile the regex pattern into a Pattern object
Pattern pattern = Pattern.compile(patternString);

// Create a Matcher object with the input string
Matcher matcher = pattern.matcher(inputString);

// Use find() method to check if the pattern is found in the input string
if (matcher.find()) {
    System.out.println("Pattern found in the input string.");
    
    String firstMatch = matcher.group();
    System.out.println("First match found: " + firstMatch);
} else {
    System.out.println("Pattern not found in the input string.");
}

Find All

String inputString = "The quick brown fox jumps over the lazy dog";
String patternString = "\\b\\w+\\b"; // Regex pattern to match words

// Compile the regex pattern into a Pattern object
Pattern pattern = Pattern.compile(patternString);

// Create a Matcher object with the input string
Matcher matcher = pattern.matcher(inputString);

// Find all matches in the input string
while (matcher.find()) {
    // Get the matched substring
    String match = matcher.group();

    // Print the matched substring
    System.out.println("Match: " + match);
}

Replace All

String input = "Hello, [name]! Welcome [name] to EZSE.";

// Replace square brackets and their contents with Hiep
String output = input.replaceAll("\\[.*?\\]", "Hiep");
System.out.println("Output: " + output);

The .*? matches any character (except for a newline) zero or more times, but as few times as possible. The default greedy behavior of .* would match the longest possible sequence of characters.

RegExmở bằng cửa sổ mới

Lambda

Similar to methods, but they do not need a name and they can be implemented right in the body of a method.

parameter -> expression
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach((n) -> System.out.println(n));
// Define a functional interface with a single abstract method
interface SumCalculator {
    int calculate(int a, int b);
}

// Define a lambda expression that implements the SumCalculator interface
SumCalculator sum = (a, b) -> a + b;

// Use the lambda expression to calculate the sum of two numbers
int result = sum.calculate(5, 7);
System.out.println("Sum: " + result);

Lambdamở bằng cửa sổ mới

Stream

Allows to perform operations on a collection of elements in a concise and functional way, using methods such as map, filter, reduce ... among others.

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

// Example 1: Filtering
List<Integer> evenNumbers = numbers.stream()
        .filter(n -> n % 2 == 0) // Keep only even numbers
        .collect(Collectors.toList()); // Collect the result into a list

System.out.println("Even numbers: " + evenNumbers);

// Example 2: Mapping
List<Integer> squareNumbers = numbers.stream()
        .map(n -> n * n) // Square each number
        .collect(Collectors.toList());

System.out.println("Square numbers: " + squareNumbers);

// Example 3: Reducing
int sum = numbers.stream()
        .reduce(0, (a, b) -> a + b); // Calculate the sum of all numbers

System.out.println("Sum of numbers: " + sum);

// Example 4: Sorting (descending order)
List<Integer> sortedNumbers = numbers.stream()
        .sorted(Comparator.reverseOrder()) // Sort the numbers in descending order
        .collect(Collectors.toList());

With data:

public class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return name + " (" + age + ")";
    }
}

// Create a list of Person objects
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 30));
people.add(new Person("Charlie", 22));
people.add(new Person("Dave", 35));
people.add(new Person("Eve", 30));
people.add(new Person("Hiep", 26));
// Find the person with name "Hiep"
Person foundPerson = people.stream()
        .filter(person -> "Hiep".equals(person.getName()))
        .findFirst()
        .orElse(null);
        
// Find the eldest person
Person eldestPerson = people.stream()
        .max(Comparator.comparingInt(Person::getAge))
        .orElse(null);


// Calculate the average age
double averageAge = people.stream()
        .mapToInt(Person::getAge)
        .average()
        .orElse(0.0);

// Find people whose age is above the average age
List<Person> peopleAboveAverageAge = people.stream()
        .filter(person -> person.getAge() > averageAge)
        .collect(Collectors.toList());

// Find people whose age is above the average age and sort them by name
List<Person> peopleAboveAverageAgeSortedByName = people.stream()
        .filter(person -> person.getAge() > averageAge)
        .sorted(Comparator.comparing(Person::getName))
        .collect(Collectors.toList());

// Find people whose age is above the average age, sort them by name in descending order and by age in descending order
List<Person> peopleAboveAverageAgeSortedByNameDescAndAgeDesc = people.stream()
        .filter(person -> person.getAge() > averageAge)
        .sorted(Comparator.comparing(Person::getName).reversed().thenComparingInt(Person::getAge).reversed())
        .collect(Collectors.toList());

Streammở bằng cửa sổ mới

Sort method

// Sort the list of Person objects by age
people.sort(Comparator.comparingInt(Person::getAge));
// or
people.sort(Comparator.comparingInt(p -> p.getAge()));

// Sort the list of Person objects by age, and then by name
people.sort(Comparator.comparingInt(Person::getAge)
        .thenComparing(Person::getName));

// Sort the list of Person objects by age in ascending order, and then by name in descending order
people.sort(Comparator.comparingInt(Person::getAge)
        .thenComparing(Comparator.comparing(Person::getName).reversed()));

// Sort the list of Person objects by age in descending order, and then by name in descending order
people.sort(Comparator.comparingInt(Person::getAge).reversed()
        .thenComparing(Comparator.comparing(Person::getName).reversed()));

Comparator

Comparator is an interface that allows you to define custom sorting rules for objects in a collection.

The Comparator interface has a single method called compare(), which takes two objects as input and returns an integer value indicating their relative order.

The compare() method should return a negative value if the first object should be sorted before the second object, a positive value if the first object should be sorted after the second object, and 0 if the two objects are considered equal in terms of sorting.

Example
Sort a list of Person objects in Java based on their names
Use the Comparable interface and implement the compareTo()

class Person implements Comparable<Person> {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return name + " (" + age + ")";
    }

    @Override
    public int compareTo(Person o) {
        // Compare people based on their names
        return this.name.compareTo(o.getName());
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a list of Person objects
        List<Person> people = new ArrayList<>();
        people.add(new Person("John", 30));
        people.add(new Person("Alice", 25));
        people.add(new Person("Bob", 40));
        people.add(new Person("Charlie", 35));

        // Sort the list based on the names
        Collections.sort(people);

        // Print the sorted list of people
        System.out.println(people);
    }
}

Sort a list of Person objects in Java based on the second letter of their names

// Use a custom Comparator implementation
public static void main(String[] args) {
    // Create a list of Person objects
    List<Person> people = new ArrayList<>();
    people.add(new Person("John"));
    people.add(new Person("Alice"));
    people.add(new Person("Bob"));
    people.add(new Person("Charlie"));

    // Sort the list based on the second letter of the names
    Collections.sort(people, new Comparator<Person>() {
        @Override
        public int compare(Person o1, Person o2) {
            String name1 = o1.getName();
            String name2 = o2.getName();

            // Check if names have at least two letters
            if (name1.length() >= 2 && name2.length() >= 2) {
                // Extract the second letters
                char secondLetter1 = name1.charAt(1);
                char secondLetter2 = name2.charAt(1);

                // Compare the second letters
                return Character.compare(secondLetter1, secondLetter2);
            }

            // If names have less than two letters, consider them equal
            return 0;
        }
    });

    // Print the sorted list of people
    System.out.println(people);
}

Sort a list of Person objects in Java based on the second letter of their names, and then by age

public static void main(String[] args) {
    // Create a list of Person objects
    List<Person> people = new ArrayList<>();
    people.add(new Person("John", 30));
    people.add(new Person("Alice", 25));
    people.add(new Person("Bob", 40));
    people.add(new Person("Charlie", 35));

    // Sort the list based on the second letter of the names, then by age
    Collections.sort(people, new Comparator<Person>() {
        @Override
        public int compare(Person o1, Person o2) {
            String name1 = o1.getName();
            String name2 = o2.getName();

            // Check if names have at least two letters
            if (name1.length() >= 2 && name2.length() >= 2) {
                // Extract the second letters
                char secondLetter1 = name1.charAt(1);
                char secondLetter2 = name2.charAt(1);

                // Compare the second letters
                int result = Character.compare(secondLetter1, secondLetter2);
                if (result != 0) {
                    // If the second letters are different, return the result
                    return result;
                } else {
                    // If the second letters are the same, compare by age
                    return Integer.compare(o1.getAge(), o2.getAge());
                }
            }

            // If names have less than two letters, consider them equal
            return 0;
        }
    });

    // Print the sorted list of people
    System.out.println(people);
}

File Handling

File Handlingmở bằng cửa sổ mới