Posts

StringBuffer and StringBuilder

Both StringBuffer and StringBuilder are two important classes in Java which represents mutable String i.e. the String object, whose value can be changed. Since String is Immutable in Java, any change or operation on String object e.g. converting it to upper or lower case, adding character, removing a character, or getting a substring, all results in a new String object. Anyway, when StringBuffer was introduced it has its own problem e.g. it was synchronized, methods like append have synchronized and hence they were slower. Even if you use them by just one thread and don’t share with other threads, the cost of acquiring and releasing lock due to Synchronization is still significant. Since StringBuffer is mostly used as local mutable String variable making it synchronized wasn’t a great decision and Java designer realized their mistake and corrected it in Java 1.5 by introducing the StringBuilder class. As I have said that both StringBuffer and StringBuilder are a mutable alte...

comparing two objects using java

Java provides two mechanisms to compare objects: Using Comparable interface Using Comparator interface The purpose of using one of these interfaces depends upon the use case. We will discuss both the approaches one by one. Using Comparable Interface When we implement the comparable interface in a class, it imposes a total ordering on its objects. This ordering is referred to as the class’s natural ordering, and the class’s  compareTo  method is referred to as its natural comparison method. Following is the definition for this interface. Comparable.java public interface Comparable < T > { public int compareTo (T o) ; } Few important points: Lists of objects that implement this interface can be sorted automatically by  Collections.sort  or  Arrays.sort Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without need to specify a comparator. ...

How to convert Stream to Set using java

In Java 8, Stream is one of the most important class as it allows a lot of useful functional operations e.g. filter, map, flatmap, etc on a collection of object. Hence, going forward converting a Collection to Stream, performing operations and then converting the result back to different Collection classes like List, Set, and Map will be a common task.  You can convert a Stream to Set by using the Collectors.toSet() method. Since Set doesn't allow duplicates and doesn't provide any ordering guarantee, any duplicate elements from Stream are lost and ordering is also gone. Set<Integer> aSet = input.stream().collect(Collectors.toSet()); There are no implementation guarantees provided for the Set returned by collect method here. You can't assume it a HashSet, but it will something which implements Set interface. Also, the size of Set will be less than or equal to a number of elements in the final Stream because duplicate elements are lost when you covert Stream...

Why Angular framework best for web development

Why Angular is Better For Web Application Development? In this computerized period, the market is seeing the regularly expanding interest for web engineers. There are a great many sites on the web, and the numbers are rising each day. Numerous stages are accessible in the market for web improvement. Picking the correct one with the capacity to show a utilitarian and an easy to understand is extreme. AngularJS has picked up a great deal of footing since it was dispatch by Google. It is an open source web application which spins around HyperText Markup language (HTML), Cascading Style Sheet (CSS) and JS (JavaScript). Why Angular is Better For Web Development? What is AngularJS? Distributed by Google in 2009 AngularJS has turned out to be a standout amongst the most prominent JavaScript structures till now. To be valid, it doesn't bring any principal new and progressive answers for designers and developers. In any case, regardless it stays top best framework. Things being...

Highlights in latest Java 12 updates

Before we have a look at the new features, let’s clarify once again what it means when a version does not have a long-term support. As a short-term support release, Java 12 will receive commercial support from Oracle only until the release of the next Java version, in this case, Java 13. You should keep in mind, however, that although Java 12 is not a major release, in the sense that it does not come with long-term support, it is not a minor release either since it brings eight new features. Here is an overview: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)  — Add a new garbage collection (GC) algorithm named Shenandoah which reduces GC pause times by doing evacuation work concurrently with the running Java threads. Pause times with Shenandoah are independent of heap size, meaning you will have the same consistent pause times whether your heap is 200 MB or 200 GB. Microbenchmark Suite —  Add a basic suite of microbenchmarks to the JDK source code, and ma...

Spring Boot Annotations and managed objects

CDI Contexts and Dependency Injection provides the programmer with a mechanism that automatically manages class instances.  The main advantages are: No need to create classes using the  new  keyword  ; No need to pass a class instance to call its method in a different class. For comparison Code written in Java SE: 1 2 3 4 5 public class Hello {      public String sayHallo() {          return "Hello!" ;      } } 1 2 3 4 5 6 public class Sample {      public static void main(String[] args) {          Hello hello = new Hello();          hello.sayHallo();      } } The code written in Spring 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @SpringBootApplication publ...