Java utils collections: Map

Difference between HashMap, LinkedHashMap and TreeMap

  • HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.
  • TreeMap will iterate according to the “natural ordering” of the keys according to their compareTo() method (or an externally supplied Comparator). Additionally, it implements the SortedMap interface, which contains methods that depend on this sort order.
  • LinkedHashMap will iterate in the order in which the entries were put into the map

hashtables.JPG

taken from:

https://stackoverflow.com/questions/2889777/difference-between-hashmap-linkedhashmap-and-treemap

Advertisements

String … and Generating Javadocs

In java we can pass some arguments in methods. Sometimes we want to pass the array of objects. So if you see writeText(String … text) is actually nothing but array of Strings. It’s same as writeText(String[] text).

To easily Generate javadocs in Intelij Idea you can use this shorcut.

type /** then press enter. This applies for the methods. Later you can add more details to that javadoc

ex.:

/**

public String addText(String[] text){

return ‘abc’;}

becomes

/**
 *
 * @param text
 * @return 
 */

public String addText(String[] text){

return ‘abc’;}

Java Streams

Since java 8, we have access to java streams api. It works similar to java collections(List, Set, Queue).

The Java 8 Streams can be seen as lazily constructed Collections, where the values are computed when user demands for it. Actual Collections behave absolutely opposite to it and they are set of eagerly computed values (no matter if the user demands for a particular value or not).

Just like functional programming languages, Streams support Aggregate Operations. The common aggregate operations are filter, map, reduce, find, match, sort. These operations can be executed in series or in parallel.

The Streams also support Pipelining and Internal Iterations. The Java 8 Streams are designed in such a way that most of its stream operations returns Streams only. This help us creating chain of various stream operations. This is called as pipelining. The pipelined operations looks similar to a sql query. ex. a.b().c()

Source: https://dzone.com/articles/understanding-java-8-streams-1

Action time: Let’s start some project

Dear readers and me, I’ve decided that I should start developing my own application. Only this action can justify my accumulation of knowledge. Therefore, let’s decide on couple things:

  1. Frontend: jQuery, AJAX and JavaScript
  2. Backend: Spring framework

As for app idea, I’ve decided that I will be making clone of project which I’m working on now. But I won’t reuse any materials from it, only the knowledge I’ve acquired. I want my project to be opensource so all the codes will be available on my GitHub account. The link I will put once I got anything to show.

Thank you.

Java SE 8 OCA exam: Chapter 5

This is one of the most important chapters as it’s covers the principles of the OOP.

Note:

1. As you know, a Java fle can have many classes but at most one public class. In fact, it
may have no
public class at all. One feature of using the default package private modifier
is that you can define many classes within the same Java file

2.  The rules for applying class access modifiers are identical for interfaces. There can be at
most one
public class or interface in a Java file. Like classes, top-level interfaces can also
be declared with the
public or default modifiers.

3.  In Java, all classes inherit from a single class, java.lang.Object.  Furthermore, java.lang.Object is the only class that doesn’t have any parent classes. When we create new class it will automatically add extends java.lang.Object even thought it’s invisible:
public class Zoo {
}
public class Zoo extends java.lang.Object {
}

4.

Like the this() command that you saw in Chapter 4, the super() command may only
be used as the frst statement of the constructor. For example, the following two class defnitions will not compile:
public class Zoo {
public Zoo() {
System.out.println(“Zoo created”);
super(); // DOES NOT COMPILE
}
}

5. Super tricky. If the subclass has auto constructor it will call it’s super class constructor first, which may be also auto generated. However, when child’s auto constructor is calling the super class’s constructor AND super class’s constructor already have constructor with different signature (ex. public Father(int a)) then the child’s constructor will not compile. Because it’s superclass’s constructor becomes undefined. JAVA WTF?

Introducing Class Inheritance

Inheritance is the process by which the new child subclass automatically includes any
public or protected primitives, objects, or methods defined in the parent class.

Java supports single inheritance, by which a class may inherit from only one direct parent class.

Java also supports multiple levels of inheritance, by which one class may extend
another class, which in turn extends another class. You can extend a class any number of
times, allowing each descendant to gain access to its ancestor’s members.

Java does allow one exception to the single inheritance rule: classes may implement multiple interfaces, as you’ll see later in this chapter.

It is possible in Java to prevent a class from being extended by marking the class with
the
final modifer. If you try to defne a class that inherits from a final class, the compiler
will throw an error and not compile.

Java allows only one public class per file.

For the OCA exam, you should only be familiar with public and default
package-level class access modifiers, because these are the only ones that
can be applied to top-level classes within a Java file. The
protected and
private modifiers can only be applied to inner classes, which are classes
that are defined within other classes, but this is well out of scope for the
OCA exam.

Defining Constructors

In Java, the first statement of every constructor is either a call to another constructor
within the class, using
this(), or a call to a constructor in the direct parent class, using super().

Following three class and constructor definitions are equivalent, because the compiler will automatically convert them all to the last example:

public class Donkey {
}
public class Donkey {
public Donkey() {
}
}
public class Donkey {
public Donkey() {
super();
}
}

In this example no constructor is defned within the Elephant class, so the compiler tries
to insert a default no-argument constructor with a
super() call, as it did in the Donkey
example. The compiler stops, though, when it realizes there is no parent constructor that
takes no arguments. 

public class Mammal {
public Mammal(int age) {
}
}
public class Elephant extends Mammal { // DOES NOT COMPILE
}

In this example, we must explicitly defne at least one constructor, as
in the following code:

public class Mammal {
public Mammal(int age) {
}
}
public class Elephant extends Mammal {
public Elephant() { // DOES NOT COMPILE
}
}

This code still doesn’t compile, though, because the compiler tries to insert the noargument super() as the frst statement of the constructor in the Elephant class, and there
is no such constructor in the parent class. We can fx this, though, by adding a call to a parent constructor that takes a fxed argument:
public class Mammal {
public Mammal(int age) {
}
}
public class Elephant extends Mammal {
public Elephant() {
super(10);
}
}
This code will compile because we have added a constructor with an explicit call to a
parent constructor. Note that the class
Elephant now has a no-argument constructor even
though its parent class
Mammal doesn’t. Subclasses may defne no-argument constructors
even if their parent classes do not, provided the constructor of the child maps to a parent
constructor via an explicit call of the
super() command.
You should be wary of any exam question in which the parent class defnes a constructor
that takes arguments and doesn’t defne a no-argument constructor. Be sure to check that
the code compiles before answering a question about it.

***Reviewing Constructor Rules

Let’s review the rules we covered in this section.
Constructor Definition Rules:
1. The first statement of every constructor is a call to another constructor within the class
using
this(), or a call to a constructor in the direct parent class using super().
2. The super() call may not be used after the first statement of the constructor.

3. If no super() call is declared in a constructor, Java will insert a no-argument super()
as the first statement of the constructor.
4. If the parent doesn’t have a no-argument constructor and the child doesn’t define any
constructors, the compiler will throw an error and try to insert a default no-argument
constructor into the child class.
5. If the parent doesn’t have a no-argument constructor, the compiler requires an explicit
call to a parent constructor in each child constructor.
Make sure you understand these rules; the exam will often provide code that breaks one
or many of these rules and therefore doesn’t compile.

 

Java SE 8 OCA exam: Chapter 4

As the previous optimization attempt went well, where I studied for chapter 6, I would like to do the same for chapter 4. In this attempt, I will get specific parts of chapter 4, that will be useful for chapter 5, and once I understand the concepts, I will switch to chapter 5.

Applying Access Modifiers

You already saw that there are four access modifiers: public, private, protected, and
default access. We are going to discuss them in order from most restrictive to least restrictive:
private: Only accessible within the same class
default (package private) access: private and other classes in the same package
protected: default access and child classes
public: protected and classes in the other packages

Private Access

Private access is easy. Only code in the same class can call private methods or access
private fields.