Showing posts with label Z0_Java_Collection. Show all posts
Showing posts with label Z0_Java_Collection. Show all posts

Sunday, May 1, 2016

String vs StringBuffer vs StringBuilder

n Java, a String is a primitive data type and almost all developers use String for a character strings operation. Other than that, Sun provided StringBuffer, StringBuilder.

What are the differences between the String, Stringbuffer, and Stringbuilder?
1. String: A string is immutable, which means that once a String is created, its value cannot be changed.
?
1
2
3
4
5
String s = "Hello";
 
s = s + " World!";
 
System.out.println(s);
When executing this code, the string Hello World! will be printed. How?
Java created a new String object and stored "Hello World!" as its value. If this style of coding is used often in a program, the program will have performance problems due to the lack of memory.
2. StringBuffer: A StringBuffer is mutable, which means once a StringBuffer object is created, we just append the content to the value of the object instead of creating a new object. Their methods are synchronized when neccessary so that the StringBuffer will be used effectively in threads. The StringBuffer runs slow in a one-thread program.
?
1
2
3
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World!");
System.out.println(sb.toString()); //Hello World!
3. StringBuilder: The StringBuilder is essentially the same as StringBuffer but it is not thread-safe, that means that their methods are not synchronized. In comparison to the other Strings, the Stringbuilder runs the fastest.
?
1
2
3
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World!");
System.out.println(sb.toString()); //Hello World!

Comparator and Comparable


The Comparator and Comparable interfaces are two interfaces avaiable in Java for sorting the user defined objects.
For example, if you want to sort the List of Employee Objects based on Employee Id, name, or address, you would need to use either the comparable interface or the comparator interface.

Comparable Vs Comparator

  • Both interfaces are used for comparing two different objects of same class.
  • If the source code of the class(the object about to be sorted) is accessible and modifiable, then we can implement the Comparable interface. Otherwise, if a source code is not available, we can make use of the Comparator interface.
The examples below demostrates how to use the Comparator and Comparable interfaces for sorting an Employee Object by its id.

Using the Comparable Interface 

Overriding the compareTO() method if a source code is available is the proper way to use the comparable interface.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
class Employee implements Comparable {
    int id;
    String name;
     
    public Employee(int id, String name) {
        this.id=id;
        this.name=name;
    }
    @Override
    public int compareTo(Object o) {
        Employee emp = (Employee)o;
        return this.id-emp.id;
    }
}
 
public class EmployeeSorting {
    public static void main(String[] args) {
        List employees = new ArrayList<>();
        Employee emp1 = new Employee(3, "Jerome");
        Employee emp2 = new Employee(1, "Albert");
        Employee emp3 = new Employee(2, "Samiya");
        Employee emp4 = new Employee(5, "Stella");
        Employee emp5 = new Employee(4, "Kent");
        employees.add(emp1);
        employees.add(emp2);
        employees.add(emp3);
        employees.add(emp4);
        employees.add(emp5);
         
        Collections.sort(employees);
         
        for (Employee employee : employees) {
            System.out.println(employee.id + ", " + employee.name);
        }
    }
}
After executing the class in the example above, the output will be as follows:
     1, Albert
     2, Samiya
     3, Jerome
     4, Kent
     5, Stella
The compareTo() method should compare this object to another object and return intValue. Below are the rules for intValue
if the method returns:
  • -ve value, then the object is smaller than another object.
  • 0, then the object value is same as another value.
  • +ve value, then the object is larger than another object.

Using Comparator

When a source code is not available for the object that we want to sort, we can use the comparator interface for sorting.
In this example, we will introduce a new class that implements the comparator interface and override the compare() method and Pass the Comparator object to the sort() method of collections.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
class EmployeeComparator implements Comparator
{
    @Override
    public int compare(Object o1, Object o2) {
        Employee emp1 = (Employee)o1;
        Employee emp2 = (Employee)o2;
        return emp1.id-emp2.id;
    }
}
 
public class EmployeeSorting {
    public static void main(String[] args) {
        List employees = new ArrayList<>();
        Employee emp1 = new Employee(3, "Jerome");
        Employee emp2 = new Employee(1, "Albert");
        Employee emp3 = new Employee(2, "Samiya");
        Employee emp4 = new Employee(5, "Stella");
        Employee emp5 = new Employee(4, "Kent");
        employees.add(emp1);
        employees.add(emp2);
        employees.add(emp3);
        employees.add(emp4);
        employees.add(emp5);
         
        Collections.sort(employees, new EmployeeComparator());
         
        for (Employee employee : employees) {
            System.out.println(employee.id + ", " + employee.name);
        }
    }
}
The compare() method should compare one object with another object and return intValue