Modularization introduced in Java 9 is to help developers call out explicitly what are they going to use in their application and what features are they ready to expose.
You can see that JDK itself now is viewed as a combination of modules rather than a single monolith unit. One can mention all modules they are going to use in the application and keep the application lightweight. This can be done by adding a “requires” section in the module-info file.
Another problem Modules solve is when you are sharing your library (a jar), it exposes all the packages, though you might not want users to play around with internal helper files. The “exports” keyword gives you control over what is being exposed from the package.
Java 8 arguably is the most common of the Java versions and I see many projects still use Java 8. Among many other features, two of the essential elements released with Java 8 were Lambda expressions and streams. Here are some old notes recap on these topics.
ArrayDequeue: Double-ended queue implementation. Provides methods like add, addFirst, addLast.
Set Implementations
HashSet: Implementation of hash table data structure. Do not guarantee order.
LinkedHashSet: Uses a doubly linked list and hence maintains the order.
Sorted Set Implementation
TreeSet: Ordering is maintained as natural order or explicit comparator based ordering.
publicclass TreeSetDemo {
publicstaticvoid main(String args[])
{
TreeSet<String> ts = new TreeSet<String>();
ts.add("this");
ts.add("is");
ts.add("just");
ts.add("a");
ts.add("test");
Iterator<String> itr = ts.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
}
}
Output
a
is
just
test
this
As the name suggests, a Java Collection is a collection of objects represented as a single unit. The idea is to provide a set of operations on the logically grouped elements like searching, sorting, etc.
As the image above shows, there are two core interfaces, Collection and Map. The collection further has interfaces for List, Queue, and Set. For each of these interfaces than we have a concrete set of classes implementing the functionality.
Let’s take a very simple example
Collection values = new ArrayList();
values.add(1);
values.add(2);
values.add("kamal");
We can see collection interface helps us create a new ArrayList, which gives us flexibility over the array where the List can grow during runtime.
But one challenge we can see in the code above is that when we are able to add numbers as well as String to the ArrayList. This is in contrast with the type safety provided by Java. To solve this Java provides us Generics to control over types of elements that can be added to a collection.
Generics Example
class MyClass<T>{
T value;
public T getValue() {
return value;
}
publicvoid setValue(T value) {
this.value = value;
}
}
Coming to our previous code for creating collections
Collection<Integer> values = new ArrayList<>();
values.add(1);
In this example, we cannot add a String to values now.
What if we want to get or add into ArrayList at a particular index. List Interface adds these features on top of Collection. As already depicted in the hierarchy, List inherits Collection and then adds features on top of it.
List<Integer> values = new ArrayList<>();
values.add(1);
int num = values.get(0);
Iterating over a Collection
There are multiple ways to iterate over a collection
Another important thing one would like to do with the collection is to arrange elements in sorted order. For example, say we have a List created for Student class objects, and we want to arrange based on Roll numbers.
The easiest way is to make the class implement comparable
class Student implements Comparable<Student>{
int rollno;
String name;
public Student(int rollno, String name) {
this.rollno=rollno;
this.name=name;
}
@Override
publicint compareTo(Student o) {
returnthis.rollno<o.rollno?-1 : (this.rollno==o.rollno)?0:1;
}
}
// In calling code, we can sort list made up of students
Collections.sort(list);
There can be cases where it is handy to maintain comparison based on multiple criteria. For example, along with roll number, we want to make sure we can sort the list of students based on names in alphabetical order. In such a case it makes sense to implement Comparator as we can implement multiple comparators based on our need.
class NameComparator implements Comparator<Student>{
@Override
publicint compare(Student o1, Student o2) {
return o1.name.compareTo(o2.name);
}
}
// In calling code, we will send compatator instance
Collections.sort(list, new NameComparator());
This is a short tutorial about creating a simple REST webservice with spring boot. This assumes you have basic knowledge of Spring and Java.
To get started we will create a Spring project. The simplest way to do this is to go to http://start.spring.io/ and create a project.
This will give us a blank project on which we can build upon. Import the project as Maven project to Eclipse or any other ID. By default, it will provide a class which will look like
@SpringBootApplication
public class NewtestApplication {
public static void main(String[] args) {
SpringApplication.run(NewtestApplication.class, args);
}
}
As you can see this is the main class and all the execution starts here. Also, note the @SpringBootApplication annotation. Spring documentation states that
“The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes”
For this example, we will keep it simple and introduce a controller
@RestController
public class HelloController {
@GetMapping("/sayhello")
public String sayHello() {
return("Hello");
}
}
Finally, Run the main class as a normal Java application, it will automatically start the Spring Boot server at 8080.
We can now access our application at http://localhost:8080/sayhello
Recently I upgraded my Java version from 9 to 10 on a Linux machine. I was able to set JAVA_Home in .bashrc file properly but version did not get updated when checked java -version on shell.
Following commands helped to change the system’s Java version