Friday, 29 May 2020

Comparing the Network Performance of AWS, Azure, GCP, IBM Cloud and Alibaba Cloud

Trying to figure if it's worthwhile to use IBM Cloud for an AI project, I found this nice and informative presentation by Angelique Medina from Thousandeyes. Below is an extract from it:










Packet loss caused by Chinese Firewall


So far AWS Global Accelerator doesn't really make much difference






Friday, 10 March 2017

Open Data Science

Anaconda team is giving away a free O'Reilly ebook Breaking Data Science Open:


See below some extracts from its preface:

Data science has captured the public’s attention over the past few years as perhaps the hottest and most lucrative technology field.
...
The leading edge of this tsunami is a combination of innovative business and technology trends that promise a more intelligent future based on the pairing of open source software and crossorganizational collaboration called Open Data Science. Open Data Science is a movement that makes the open source tools of data science—data, analytics, and computation—work together as a connected ecosystem.
...
This report discusses the evolution of data science and the technologies behind Open Data Science, including data science collaboration, self-service data science, and data science deployment. Because Open Data Science is composed of these many moving pieces, we’ll discuss strategies and tools for making the technologies and people work together to realize their full potential.

Saturday, 18 February 2017

Machine Learning Cheat Sheet Part 5 - Logistic Regression (Classification)

1. Logistic regression deals with data sets where $y$ may have only a small number of discrete values. For example, if $y\in \{0, 1\}$ then this is a binary classification problem in which $y$ can take only two values 0 and 1.

For instance, in an email spam classifier $x^{(i)}$ could be some feature of an email message and $y$ be 1 if it is a spam or 0 otherwise. In this case 0 would be a negative class and 1 a position class and they are sometimes also denoted by symbols "-" and "+". For given $x{(i)}$, the corresponding $y{(i)}$ is called the label of this training example.


2. Hypothesis representation:

$h_\theta (x) = g(z)$

where

$g(z) = \frac{1}{1+e^{-z}}$ - Sigmoid or Logistic function

and

$z = \theta^{T}x$

Sigmoid function $g(z)$ maps any real number to (0, 1) interval, making it useful for transforming an arbitrary-valued function into a function better suited for classification.

In this case $h_\theta(x)$ gives a probability that output is 1. For example, $h_\theta(x) = 0.7$ gives a probability of 70% that the output is 1 and probability of 30% that it is 0.

$h_\theta(x) = P(y = 1 | x; \theta) = 1 - P(y = 0 | x; \theta)$

or

$P(y = 0 | x; \theta) + P(y = 1 | x; \theta) =  1$


3. Decision Boundary

In order to get classification with discrete values 0 and 1, we can translate the output of the hypothesis function as follows:

$h_\theta(x) \geqslant 0.5 \rightarrow y = 1$

$h_\theta(x) < 0.5 \rightarrow y = 0$


Sigmoid function $g(z)$ behaves the way that if its input is greater than or equal to zero, its output is greater than or equal to 0.5:

$g(z) \geqslant 0.5$

when $z \geqslant 0$


Remember that:

$z = 0, e^0 = 1\Rightarrow g(z) = \frac{1}{2}$

$z \rightarrow \infty, e^{-\infty} \rightarrow 0 \Rightarrow g(z) = 1$

$z \rightarrow -\infty, e^{\infty} \rightarrow \infty \Rightarrow g(z) = 0$


So if $z = \theta^{T}x$, then it means that:

$h_\theta(x) = g(\theta^{T}x) \geqslant 0.5$

when $\theta^{T}x \geqslant 0$


From the statements above it's valid to say:

$\theta^{T}x \geqslant 0 \Rightarrow y = 1$

$\theta^{T}x < 0 \Rightarrow y = 0$


The decision boundary is a line that separates an area where y = 0 with an area where y = 1. This line is created by the hypothesis function.

An input to the sigmoid function $g(z)$ (e.g. $\theta^{T}x$) doesn't need to be linear and could be a function that describes, say, a circle ($z = \theta_0 + \theta_1 x_1^2 + \theta_2 x_2^2$) or any other shape that fits the data.


4. Cost function

Training set with $m$ examples:

$\{(x^{(1)}, y^{(1)}), (x^{(2)}, y^{(2)}), ..., (x^{(m)}, y^{(m)})\}$

$n$ features:

$x \in \left[\begin{array}{c} x_0 \\ x_1 \\ ... \\ x_n \end{array} \right]$ where $x_0 = 1$ and $y \in \{0, 1\}$

Hypothesis with sigmoid:

$h_\theta(x) = \frac{1}{1 + e^{-\theta^{T}x}}$

Cost function helps to select parameters $\theta$:
\[J(\theta) = \frac{1}{m}\sum_{i=1}^{m} Cost(h_\theta(x^{(i)}, y^{(i)})\]
$Cost(h_\theta(x^{(i)}, y^{(i)}) = -\log (h_\theta (x))$, if $y = 1$

$Cost(h_\theta(x^{(i)}, y^{(i)}) = -\log (1 - h_\theta (x))$, if $y = 0$


5. Simplified cost function:

We can combine two conditional cases into one case:

$Cost(h_\theta(x^{(i)}, y^{(i)}) = -y\log(h_\theta(x)) + (1 - y)\log(1 - h_\theta(x))$

Notice that when $y$ is equal to 1, then the second term $(1 -y)\log(1 - h_\theta(x))$ is zero and will not affect the result. If $y$ is equal to 0, then the first term $-y\log(h_\theta(x))$ is zero and will not affect the result.


The entire cost function will look like this:
\[ J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} [y^{(i)}\log(h_\theta(x^{(i)})) + (1 - y^{(i)})\log(1 - h_\theta(x^{(i)}))] \]
A vectorised representation is:
\[
h = g(X\theta) \\
J(\theta) = \frac{1}{m} \times (-y^{T}log(h) + (1 - y)^{T} \log(1 -1))
\]
6. Gradient descent:
\[
Repeat\ \{ \\
\theta_j = \theta_j - \frac{\alpha}{m} \sum_{i=1}^{m}(h_\theta(x^{(i)}) - y^{(i)})x_j^{(i)} \\
\}
\]
A vectorised representation is:
\[ \theta = \theta - \frac{\alpha}{m} X^{T} (g(X\theta) - \overrightarrow{y}) \]


6. Advanced Optimisation

Once cost function $J(\theta)$ is defined, we need to $\min_{\theta}J(\theta)$ in order to find optimal values $\theta$ for our hypothesis. In a normal case scenario, we need a code that can compute:

- cost function $J(\theta)$, and

- gradient descent $\frac{\partial}{\partial \theta_j}J(\theta) \ (for\ j = 0, 1, ..., n)$

where gradient descent is:

$Repeat\ \{ \\
\ \ \ \ \theta_j = \theta_j - \alpha \frac{\partial}{\partial \theta_j}J(\theta) \\
\}$

Luckily, there are existing optimisation algorithms that work quite well. Such Octave (and Matlab) algorithms as "Conjugate Gradient", "BFGS" and "L-BFGS" provide more sophisticated and faster way to optimise $\theta$ and could be used instead of gradient descent. The workflow is as follows:

1. Provide a function that evaluates the following two functions for a given input value $\theta$:

$J(\theta)$, and
$\frac{\partial}{\partial \theta_j}J(\theta)$

In Octave it may look like this:

$function\ [jVal, gradeint] = costFunction(theta) \\
\ \ \ \ jVal = [...\ code\ to\ compute\ J(\theta)...]; \\
\ \ \ \ gradient = [...\ code\ to\ compute\ derivative\ of\ J(\theta)...]; \\
end$

2. Use Octave optimisation algorithm $fminunc()$ together with $optimset()$ function that creates an object containing the options to be sent to $fminunc()$. Give to the function $fminunc()$ the cost function $J(\theta)$, an initial vector of $\theta$ values and the "options" object:

$options = optimset('GradObj', 'on', 'MaxIter', 100); \\
initialTheta = zeros(2, 1); \\
[optTheta, functionVal, exitFlag] = fminunc(@costFunction, initialTheta, options);$
$optTheta$ would contain $\theta$ values that we are after.

Advantages of using existing optimisation algorithms are:
- No need to manually pick up $\alpha$
- Often faster than gradient descent

Disadvantage:
 - Could be more complex for a given task than actually needed.


7. Multi-class Classification

This is an approach to classify the data when there are more than two categories. Instead of $y \in \{0, 1\}$, it could be $y \in \{0, 1, 2, ..., n\}$.

In this case we can divide the problem into $(n+1)$ binary classification problems and in each of them to predict that $y$ is a member of one of given classes.

$y \in \{0, 1, 2, ..., n\}$

$h_{\theta}^{(0)}(x) = P(y = 0 | x; \theta)$

$h_{\theta}^{(1)}(x) = P(y = 1 | x; \theta)$

$h_{\theta}^{(2)}(x) = P(y = 2 | x; \theta)$

$h_{\theta}^{(n)}(x) = P(y = n | x; \theta)$

$prediction = max_i(h_{\theta}^{(i)}(x))$

Basically, this is a selection of one class with all other classes combined into a single second class. It is done repeatedly, applying binary logistic regression to each case. Then for prediction we can use the hypothesis that returns the highest value.


To summarise:

1. Train a logistic regression classifier $h_{\theta}(x)$ for each class to predict the probability that $y = i$.

2. To make a prediction on a new $x$, pick the class that maximises $h_{\theta}(x)$.



Sunday, 5 February 2017

Machine Learning Cheat Sheet Part 4 - Polynomial Regression and Normal Equation

1. Polynomial Regression

The hypothesis function does not need to be linear (a straight line) if it does not fit the data well. It's possible to change the behaviour of the curve of the hypothesis function by making it a quadratic, cubic or square root function (or any other form).

We can combine existing feature into one. For example, combine $x_1$ and $x_2$ into a new feature $x_3$ by taking $x_1 \times x_2$.

If the hypothesis function is:

$ h_\theta (x) = \theta_0 + \theta_1 x $

then we can create additional features based just on $x_1$ to get a quadratic function:

$ h_\theta (x) = \theta_0 + \theta_1 x + \theta_2 x^2 $

or the cubic function:

$ h_\theta (x) = \theta_0 + \theta_1 x_1 + \theta_2 x_1^2 + \theta_3 x_1^3 $

where new features are:

$ x_2 = x_1^2 $ and $ x_3 = x_1^3 $, so the function becomes:

$ h_\theta (x) = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + \theta_3 x_3 $

NOTE: if features are chosen this way then feature scaling becomes very important.


2. Normal Equation

Normal Equation is the method to solve $\Theta$ analytically rather than using iterations with gradient descent. Normal Equation method will minimise $J$ by explicitly taking its derivatives with respect to the $\theta_j$'s and setting them to zero. This approach finds the optimum $\theta$ without iteration.

$m$ - number of training examples
$n$ - number of features

The Normal Equation formula is:

$ \Theta = (X^{T}X)^{-1}X^Ty $

Negative side-effects:

1. Normal Equation calculates matrix inversion $ (X^{T}X)^{-1} $ that has complexity $O(n^3)$ - this makes it slow if there is a very large number of features. In practice, when $n$ exceeds 10,000 it might be a good idea to switch from Normal Equation to iterations with Gradient Descent.

2. $X^{T}X^{-1}$ could be non-invertible (singular, degenerate). There are two possible solutions for such cases:

1) In Octave use $pinv(X'*X)*X'*y$ rather than $inv$ function as $pinv$ function might still be able to calculate the value of $\theta$ even if $X^{T}X^{-1}$ is non-invertible.

2) Common causes for non-invertibility could be:

  • Redundant features that are closely related (i.e. they are linearly dependent), or
  • There are too many features (e.g. $m\leqslant n$).
Solution to these problems could be:
  • Deleting a feature that is linearly dependent on another feature
  • Deleting some features or use regularisation when there are too many features.


Comparison of Gradient Descent and Normal Equation
Gradient Descent Normal Equation
Need to choose $\alpha$ No need to choose $\alpha$
Might need feature scaling No need for feature scaling
Needs many iterations No need to iterate
$O(kn^2)$ $O(n^3)$ to calculate inverse of $X^{T}X$
Works well for $n>10,000$ Works well for $n\leqslant10,000$

Saturday, 4 February 2017

Machine Learning Cheat Sheet Part 3 - Linear Regression with Multiple Variables

1. Notation:

$ m $ - number of training examples.

$ n = \vert x^{(i)} \vert $ - number of features.

$ x^{(i)} $ - column vector of all the feature inputs of the $ i^{th} $ training example.

$ x_{j}^{(i)} $ - value of feature $ j $ in the $ i^{th} $ training example.

$ \alpha $ - learning rate.


2. Hypothesis:

$ h_\theta(x) = \theta_0 + \theta_1x_1 + \theta_2x_2 + ... + \theta_nx_n $

or
$ h_\theta(x) =  \left[ \begin{array}{cc} \theta_0 & \theta_1 & ... & \theta_n \end{array} \right] \times \left[ \begin{array}{cc} x_1 \\ x_2 \\ ... \\ x_n \end{array} \right] = \Theta^{T}X $

where $ x_0^{(i)} = 1 $ for $ (i \in 1, ..., m) $


3. Parameters:

$ \theta_0, \theta_1, ..., \theta_n $ or $ \Theta $


4. Cost function:

\[
J(\theta_1, \theta_2, ..., \theta_n) = J(\Theta) = \frac{1}{2m} \sum_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)})^2
\]
5. Gradient descent:

repeat until convergence:
\[
{
\{
\\
\theta_0 = \theta_0 - \alpha \frac{1}{m} \sum_{i=1}^{m} (h_0(x^{(i)}) - y^{(i)}) \times x_0^{(i)}
\\
\theta_1 = \theta_1 - \alpha \frac{1}{m} \sum_{i=1}^{m} (h_0(x^{(i)}) - y^{(i)}) \times x_1^{(i)}
\\
\theta_2 = \theta_2 - \alpha \frac{1}{m} \sum_{i=1}^{m} (h_0(x^{(i)}) - y^{(i)}) \times x_2^{(i)}
\\
...
\\
\}
}
\]
6. Feature scaling:

This to be applied to the input data (training) set. Features may have significant differences in their values. For example, $ x_1 $ may have values from 100 to 10,000 (e.g. area of a house in $ feet^2 $) while $ x_1 $ may have values from 1 to 6 (e.g. number of bedrooms). It would be a good idea to scale the feature values, so that:
\[
-1 \leqslant x_i \leqslant 1  \\ or \\ -0.5 \leqslant x_i \leqslant 0.5
\]
Feature scaling technique could be a combination of feature scaling and mean normalisation:
\[
x_i = \frac{x_i - \mu_i}{s_i}
\]
where $\mu$ is an average of all the values of feature $x_i$ and $s_i$ is either the range of values $(\max - \min)$ of $x_i$ or their standard deviation. Note that dividing by the range, or dividing by the standard deviation, give different results.


7. Debugging gradient descent using learning rate $\alpha$:

7.1 Value of $J(\theta)$ and number of iterations:

Make a plot of values of cost function $J(\theta)$ (y-axis) over the number of iterations of gradient descent (x-axis). If $J(\theta)$ ever increases, then try to decrease $\alpha$ and repeat.

7.2 Automatic convergence test:

It has been proven that if learning rate $\alpha$ is sufficiently small, then $J(\theta)$ will decrease on each iteration. Declare convergence if $J(\theta)$ decreases by less than $\varepsilon$ in one iteration, where $\varepsilon$ is some small value such as $10^{-3}$. However in practice it could be difficult to choose this threshold value.

7.3 Trade-off:

If $\alpha$ is too small, it could result in slow convergence.
if $\alpha$ is too large,  it may not decrease on each iteration and thus may not converge.

Choose a small $\varepsilon$ and then increase it by $\approx3\times$:
$0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, ...$

Friday, 3 February 2017

Machine Learning Cheat Sheet Part 2 - Linear Regression with One Variable


1. Training set: $ (x^{(1)}, y^{(1)}), (x^{(2)}, y^{(2)}), ... (x^{(m)}, y^{(m)}) $

2. Hypothesis: $ h_\theta(x)=\theta_0+\theta_1x $

3. Parameters: $ \theta_0, \theta_1 $

4. Cost function: $J(\theta_0, \theta_1)$ uses parameters $\theta_0$ and $\theta_1$ to check the difference between hypothesis values $h_\theta(x)$ and given values $y$ from training example $(x,y)$:

\[ J(\theta_0, \theta_1) = \frac{1}{2m} \sum_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)})^2 \]
5. Goal: minimise cost function

$ min_{\theta_0, \theta_1} J(\theta_0, \theta_1) $

6. Gradient descent algorithm (minimisation of cost function):

$ \alpha $ - learning rate

repeat until convergence:
\[
{
\{
\\
 \theta_0 = \theta_0 - \alpha \frac{1}{m} \sum_{i=1}^{m} (h_0(x^{(i)} - y^{(i)})
\\
\theta_1 = \theta_1 - \alpha \frac{1}{m} \sum_{i=1}^{m} (h_0(x^{(i)} - y^{(i)})\times x^{(i)}
\\
\}
}
\]
(update $ \theta_0 $ and $ \theta_1 $ simultaneously!)

Thursday, 2 February 2017

Machine Learning Cheat Sheet Part 1 - Supervised and Unsupervised Machine Learning

1. Machine Learning definition:


Field of study that gives computer the ability to learn without being explicitly programmed.
Arthur Samuel (1959)

A computer program is said to learn from experience E with respect to some task  T and some performance measure P, if its performance on T, as measured by P, improves with experience E.
Tom Mitchel (1998)

2. Supervised Machine Learning

Supervised machine learning is the task of inferring a function from labeled training data. The training data consist of a set of training examples. Each example is a pair consisting of an input value/s (usually a vector) and a known output value. A supervised learning algorithm analyses the training data and produces an inferred function, which can be used for mapping new examples. An optimal scenario will allow for the algorithm to correctly labels unseen instances.

The inferred function could represent:
  • regression - predict continuous valued output, or
  • classification or categorisation - predict discrete valued output, for example, 0 and 1.

3. Unsupervised Machine Learning

Unsupervised machine learning is the task of inferring a function to describe hidden structure from unlabelled data (see Cocktail Party Effect).


Wednesday, 1 February 2017

Machine Learning Cheat Sheet - Introduction

This is a summary of my notes taken on Machine Learning course that I completed on Coursera. It was taught by Professor Andrew Ng, Associate Professor at Stanford University, Chief Scientist at Baidu and Chairman/Co-founder at Coursera. The notes include main math formulas and some practical tips mentioned during the course. Covered topics are:

  1. Supervised and unsupervised machine learning
  2. Linear regression with one variable
  3. Linear regression with multiple variables
  4. Polynomial regression and Normal Equation
  5. Logistic regression (Classification)
  6. (to be continued - work in progress)

Wednesday, 7 December 2016

Graph Database OrientDB

Neo4j's book 'Graph Databases' provides an easy introduction to the topic (google for its PDF version). Try to concentrate on schema design principles supported by practical examples and skip learning their query language Cypher unless you want to use Neo4j for your projects.

On Dec 7th, 2016 there was a good webinar about migrating data from Neo4j to OrientDB. It was surprising to see what OrientDB has under the hood - nice SQL, indexing, clustering, sharding, security model and visual representation of actual graphs stored in the database. Also OrientDB could be used as a scheme-less or schema-based  graph-oriented database or a document-oriented database or use both models (graph and document) simultaneously. Unfortunately it doesn't run on Java ME yet (it's in its roadmap though). A shortened earlier version of this webinar is available on Youtube. Below is a copy of questions and answers popped up during the presentation:

Q: Is OrientDB enterprise edition available under an open source license as well as commercial?
A: OrientDB Enterprise Edition is only commercial, but the Community is licensed as Open Source with Apache2. You can try Enterprise Edition for 45 days before to decide.

Q: LIke a viral license (AGPL) that would allow us to use enterprise edition if we open source the code we use with orient?
A: OrientDB Community Edition is licensed as Apache2, so it's not viral. You can use it for any purpose, even embedding it at no cost.

Q: Which version of OrientDB are you using?
A: OrientDB v2.2.13

Q: Do you offer a startup program? So that small companies can use enterprise edition at no/low cost?
A: Absolutely. We provide 50% of discount for startups. No hidden costs, it's all on our web site.

Q: when using the object model are ther constraints to consider can i mix match models?
A: The object model (JPA-like) doesn't work very well with the graph one, so we sugget to pick one of them. The Graph model is more powerful and supported.

Q: Does the choice of schema mode influence the performance of standard or index-based lookups?
A: Yes, using the schema makes the database much smaller (property names are not saved in the record) and therefore a smaller database is faster.

Q: Inaccurate to say that Neo4j does not support inheritance or polymorphic queries- it does with labels.
A: Neo4j labels are not polymorphic and there is not such concept in Neo4j. For more information look at http://stackoverflow.com/questions/24873067/how-to-work-with-type-hierarchies-in-neo4j, specially at the last answer/comment.

Q: How would you compare orient's extended SQL to Gremlin?
A: SQL and Gremlin are quite different in many senses, SQL is declarative, Gremlin is more oriented to step-by-step traversal/filtering. Please consider that both OrientDB and Neo4j support Gremlin, that is a standard, so you can easily migrate from one to another

Q: How do you do variable depth queries in orientdb? e.g. min depth 1 to max depth 5
A: you can use a mix of TRAVERSE and SELECT, like "SELECT FROM (TRAVERSE ... WHILE $depth < 10) WHERE $depth > 3. Or you can use the MATCH syntax, where you have distinct WHILE and WHERE conditions, one for the traversal and the other for filtering

Q: Can you query across clusters? For example if the graph is fully connected?
A: Yes, OrientDB will manage the query for you. Of course the query performance depends on how many hops you do between clusters

Q: So a cluster is a cut of the graph essentially?
A: Yes, exactly

Q: If you can write to multiple mastes, how does orient handle consistency / transactions?
A: OrientdB supports distributed transactions that assure the concistency of the database by using a 2-phase locking protocol across the servers.

Q: If you can write to multiple mastes, how does orient handle consistency / transactions?
A: Consistency is based on MVCC and quorum based consensus.

Q: What requirements does OrientDB have for Java runtime? Will it run on Java ME?
A: In terms of runtime, it only needs Java SE (no Java ME supported for now)

Q: What version of Neo4j are you comparing OrientDB to? And what version of OrientDB are you talking about?
A: We compared last GA version of both products, so Neo4j 3.0.6 and OrientDB 2.2.13

Q: Does orient work with LDAP / active directory?
A: OrientDB supports Kerberos and you can import LDAP users

Q: Neo4j seems to be a much larger company (in fact I think they just raised a bunch of money). Why do you think it is possible for orient to have so many more features than Neo4j while being a much smaller company?
A: Receiving funding is not a guarantee the company will be there tomorrow. Look at what's happened to RethinkDB and other companies that have received funding, but weren't focused on building a sunstainable business. OrientDB company is profitable since 3 years ago and its investors are its clients. We believe this is the only healthy business :-)

Q: Does key/value model (like Redis) stays in memory? We use Redis for the speed (memory residence). How will the speed be affected if we drop Redis and move to Orient for Key/Value?
A: As "just" Key/Value DBMS, Redis is faster, so if you just need a K/V we suggest to use Redis. But if your domain is more complex and requires documents, graphs, etc, then the Multi-Model approach is the best in terms of global performance and complexity.

Q: What is the theoretical/practical limit to the number of classes in ODB?
A: in current release you can have up to 32.000 data files, so if you have one per class you can have 32.000 classes

Q: What are the most common use cases for your customers in production now? How do these differ from the use-cases of customers using Neo4j?
A: While Neo4j is "only" a Graph Database, OrientDB can be used on a wider number of use cases, especially when an Operational database is required. For Operational I mean a primary database, while Neo4j in 99% of the cases is used as a secondary database, mostly for analytics with data loaded from a RDBMS (the primary one).

Q: Does the graph have size limits? Please give an example of a BIG graph already deployed in OrientDB.
A: These are the limitations: http://orientdb.com/docs/2.2/Limits.html. You can create up to 302,231,454,903 Trillion of vertices and edges, it should be enough :-) The biggest installation is for an energy company with +100 servers.

Sunday, 20 November 2016

Alibaba Cloud

Alibaba's statement on IP EXPO LONDON was plain and clear - tell us what you need and we'll do it for you, let's work together for our mutual benefit.

It didn't take long - Alibaba Cloud came to Europe. Although Alibaba has a quite efficient payment system that they technically polished in China while processing huge number of transactions on the daily basis, in Europe they started with offering just traditional cloud infrastructure. I can't wait and see what kind of other offers would follow.

How Alibaba presence looks from a consumer perspective? I've got a quick example - during Alibaba's 11.11 event I bought two 5000LM X800 'tactical' torches (they are great for cycling!) for a half price of one. I got them from an eBay seller who most likely proxied Alibaba transaction on that day.

If supply chain, online catalogues, shopping carts, checkouts and delivery of purchased items are all done under the same umbrella (Alibaba), what would happen to current distributors of Chinese goods in Europe and programmers that provide support for their online transactions? How would Amazon and eBay compete with Alibaba?

What kind of services (MaaS, SaaS, PaaS, etc) and APIs would Alibaba provide for its European infrastructure? I hope we'll find it out pretty soon.


Saturday, 15 October 2016

Kotlin Night in London on October 12th, 2016

Kotlin is a programming language invented by JetBrains that simplifies and even beautifies programming for Java Virtual Machine. Although syntactically it differs from Java, it provides full access to existing Java libraries.

Practically it's a wrapper around Java programming language that behind the scenes generates some boilerplate code that simply became too time-consuming to keep taking care of again and again and again... As a result, the code becomes more readable even for those Java programmers who is not really familiar with Kotlin yet. Look for more details about it on JetBrain's website.

Kotlin Night in London, Real World Kotlin, was quite interesting, informative and even surprising. The venue was almost full, some presentations were done by those practitioners who actively use Kotlin in their day work for production code already.

Takeaway points are:

1. Kotlin combines object-oriented and functional programming styles in a way when coding efficiency is the must. In other words, it was built by practitioners for practitioners.
2. Kotlin is free, it really costs nothing to start using it.
3. It does not generate any new kind of binaries. Kotlin code is translated to Java and then compiled into normal Java binaries.
4. Kotlin code looks way cleaner than Java, it's easy to read and understand what it does.
5. JetBrains seems to be a pretty decent and reliable vendor to be behind things like that.
6. If a Java programmer is familiar with some functional programming then it would be very easy for him/ her to pick up Kotlin in no time at all.
7. If I'm to start a new Java project, I'll definitely give it a go.

Recording of presentations from that night could be found here.

Saturday, 8 October 2016

IP EXPO EUROPE @ ExCel London on 5-6 October, 2016

This was quite interesting exhibition saturated with well-organised presentations. There were lots of big and small companies that offered stand-alone and cloud-based products and services for Cyber Security, Networks and Infrastructure, Data Analytics, DevOps and Open Source sectors.
Multiple presentations were going on simultaneously in different venues. I've have scheduled my own list beforehand and was jumping from place to place trying to catch as much as I could.
Unfortunately, most presentation were conducted by business/sales representative and were not very useful from technical perspective. I really liked topics discussed by SplunkASI Data ScienceAppCheck NGAlibabaFirst Base Technologies and Libelium. Below are some notes taken there.

Spunk (Reinventing IT Operations):
They put monitoring of applications and infrastructure on a new level when a monitoring solution could be plugged into existing systems literally within a day. A list of clients where they have already installed their solution was quite impressive. It would be a good idea to compare their functionality (and overall prices) with Zenoss that I used quite often in the past.

Claire (DevOps Platform for the Evolving Enterprise):
The idea: different teams (dev, business, testers, etc) use different tools (Jira, PM, etc), tools orchestration is complex, data synchronisation used by those tools is cumbersome. Continuous integration is difficult. Clarive offers Lean Application Delivery.
I liked their idea as it ticks all major check boxes: application lifecycle, release management and change request handling. Possibly these components combined under one umbrella may become a 'holy grail' knowledge management systems that I was looking for a long time.

Rubrik (Recover, Manage and Secure Data in the Enterprise Cloud):
Although a typical Enterprise favours delegation of extensive data processing (e.g. concurrent parallel calculations) elsewhere, it is quite cautious (as well as often constrained legally) about keeping its data on the Cloud. Because of Security... Rubrik tries to convince the public that they can keep the data on the Cloud and handle its security nicely:











Citrix (When Big Data meets Small Things – Secure Event Delivery for the Internet of Things):
This presentation was a clear message that Citrix is in IoT already and in regards to security it take the IoT services on the Cloud very seriously:












Puppet (Continuous Delivery: DevOps Holy Grail):
Well-known company with a great product. The speaker was good but his presentation was kinda useless - it was not exciting from business perspective and it was too shallow for techies, a pretty general talk about continuous delivery and how Puppet could help with it.

ASI Data Science (Practical machine learning for business applications):
These guys were amazing! A relatively small London-based company somehow accomplished 120 data science projects in a relatively short time! Well, some of those projects were probably small but the overall number of them is still astounding. They have developed their own framework that looks quite practical. I reckon that 'Data Science Project Cheat Sheet' is the most valuable screenshot I took on IP EXPO. I asked them for their presentation in electronic form and will upload it here as soon as (and if) they send it to me.
(TODO: publish ASI Data Science presentation slides)















AppCheck NG (Web Application Security: Challenges Old and New):
It was a good educative presentation. I didn't take any photos as they promised to make the slides available on their website. I haven't found it there yet and sent them a message. I'll upload their presentation here as soon as I get it.
(TODO: publish AppCheck NG presentation slides)

Avaya (Internet of Things – Forget the hype this is the reality...):
The presentation was given by Jean Turgeon in the biggest venue available. Hardly any chairs were left empty.  The talk hasn't been very technical though. I guess, the main message was that Avaya understands popularity of IoT and it is aggressively investing into this market sector.




Alibaba (Alibaba Cloud, More than just cloud):
Although the venue was small and many chairs stayed empty, the presentation was a Big Surprise! In short, Alibaba is The Real Deal on the Cloud and it's coming to Europe. I took few shots but the presenter, Mr. Yeming Wang accidentally (I hope that was an accident) removed them from my iPhone Notes when he was typing in his email address (apparently, he had no business cards left). He promised to provide me with his presentation in electronic format that I will happily publish here as soon as I receive it from him.
(TODO: publish Alibaba presentation slides)

First Base Technologies (Major Real World Red Team Exercise - The story you are about to hear is true...):
That was a very educative story about hacking into some sort of military or police database where important artefacts were stored in various electronic formats. The story was full of tiny and very interesting details. Apparently, technical hacking is greatly enhanced by social hacking. Below is just a part of that story:
They found all remote branches that the target databases is accessible from. They identified the less secured branch and looked for names of its employees. Once few names were known (some employees would go out for a lunch with their security badges attached to their clothes in a plain view), they collected personal information from social networks (LinkedIn, Facebook, Instagram, etc).
Now imaging that a guy has just returned from his holidays in Spain. He receives an email message with a logo of a hotel his family stayed in and an offer for a relatively good discount for the next trip. All he has to do is to open an attachment (then kinda print it, sign it and send it back). Once the attachment is opened, voila, intruders got a remote access to his machine! It's phishing, isn't it? Everybody knows about it, right? Do you know in how many cases this kind of files got opened? Well, in thrilling 50%...

Libelium (IoT Interoperability: any sensor, any protocol and any cloud):
This relatively small Spanish company happened to acquire a lot of knowledge in IoT space, particularly in various sensors design, implementation, installation and maintenance. Below is a short summary of what has been said there, some points could be invaluable for newcomers:

  • At the moment there are no unified standards for connection between IoT sensors and back-end systems.
  • New technologies keep appearing every year.
  • Everyone wants to get it into IoT.
  • There is a lack of clearly defined roles in implementation of IoT solutions.
  • Libelium works with sensors and communication only, it nothing to do with clouds, integration and analytics software,  they are trying to stay as close to the customers who use sensors as possible.
  • Solutions for Industrial IoT (IIoT) are very difficult to replicate. 

Lessons learnt:
    1. Sensors are nothing but the tracks (customers ask for higher quality sensors) meaning that they are the base of the business.
    2. Interoperability is the key - this means that any sensor on any cloud should be connectable using any communication protocol.
    3. IoT players need to quickly adapt to new technologies, tight coupling with selected technologies is deadly
    4. Installation and maintenance do matters.
    5. Don't go for quantity but for quality and accuracy.
    6. Be easy to evolve, for ex. iPhone app-sensor that helps to prototype a new system.



Thursday, 16 June 2016

Microsoft DevOps Tech Day

It was a good, educative workshop in a new Microsoft office steps away from Paddington train station. Several topics were presented by people from Microsoft as well as Microsoft partner RedGate. See below some notes taken there.

Microsoft:

Continuous Integration with TFS


  • TFS on the cloud is $8/month if there is no subscription. Any of most popular IDEs could be connect to it.
  • As part of the build, an application could be deployed on the cloud. Subscription to Azure could be identified via credentials file taken from Azure (management certificate).
  • Builds could be done against the master branch or any other selected branches.
  • Git doesn't support Gated Check-in (that are accepted only if submitted changes merge and build successfully) but TFS does support it.


Infrastructure 


  • Infrastructure deployment could be templated, versioned and then automated.
  • This could be done on Azure public or private Azure-inspired clouds.
  • To instantiate a template go to: Visual Studio > Azure Resource Group > deploymentTemplate.json: parameters/variables/resources/outputs - use wizards in Visual Studio. Create Resource Group in PS1 file and use Powershell to deploy it to Azure.
  • Go to Azure, click on Resource Group and see what has been deployed.
  • It's possible to modify some features and then export it as a template to JSON file.
  • Templates could be also deployed via Azure browser-based GUI (Azure GUI). Search there for 'Deploy Template'.
  • This is an Infrastructure as a Code (IaaC) - it could be versioned and auto-deployed on the need-to-do basis using continuous integration workflow.
  • Check azure-quickstart-templates in GitHub. Main JSON file may come with additional one that contains parameters. Those parameters could be displayed in Azure GUI as well. 
  • Check Azure QuickStart Templates on Microsoft website.
  • A single template could be split into (or consist of) multiple JSON files.
  • Powershell DSC could be used for a more customised deployments. As a matter of fact, it's very easy to create a customised deployment using Powershell.
  • Check Powershell Gallery for more resources.
  • Azure has a library of predefined images to be used for such deployments (ex SQLServer, Windows Server, etc) - this is to get started with it.
  • Azure Automaton DSC could be used with ChefPuppetAnsible.
  • See slide for Local Configuration Manager.
  • Azure Visualizer could be used for inspecting instantiated infrastructure.
  • Configuration could be tested with Powershell Pester Tests (which is a community project).


Continuous Delivery


  • Team Services (web GUI) has management for Releases - it's a new feature.
  • Release management can take artifacts from Team Services, Jenkins, Tram City, etc.
  • An application release could be deployed on different environments.
  • There could be particular approvers assigned to particular release deployments.
  • For unsuccessful releases new Bugs could be created using the same GUI (similar to Jira).
  • There is no rollback task. Instead you can use the previous release for redeployment. In this case the release could be done in manual mode.
  • A successful deployment on DEV environment could expect an approval for subsequent deployment on QA environment. Approval could be done using the same GUI by a person previously authorised for that.
  • Log output is visible in the GUI in real time.
  • See Xamarin Test Cloud and HockeyApp for beta distribution.
  • Release Management (from above) could use a task for testing an application on Xamarin Cloud.


RedGate: 

DevOps for databases


  • You can create a database project in VisualStudio.
  • How to deal with db changes - schema updates, drifts, etc.


Infrastructure and Application Monitoring


  • Infrastructure Insights:
    • Create a new dashboard in Azure.
    • Pin to the dashboard required tiles.
    • Settings for each tile could be configured.
    • Once dashboard is ready it could be shared with other people.
  • Application Insights:
  • Telemetry sources: traces, events, etc.
  • Log Analytics (OMS):
    • (Microsoft Operation Management Suite)
    • Another customizable dashboard.
    • Both for Windows and Linux.
    • It's solution based. See Solution Gallery.
    • Try OMS at www.mms.microsoft.com
    • Feedback and ideas windowsserver.uservoice.com


Thursday, 24 September 2015

Brief Overview of Java Collection Framework

Thanks to an excellent Java Concept of the Day, this is a brief description of main interfaces and classes of Java Collection Framework. Hopefully it would be handy for future references.


What is Collection Framework In Java?


Collection Framework in java is a centralized and unified theme to store and manipulate the group of objects. Java Collection Framework provides some pre-defined classes and interfaces to handle the group of objects. Using collection framework, you can store the objects as a list, set, queue or map and perform operations like adding an object or removing an object or sorting the objects without much hard work.


The entire collection framework is divided into four interfaces:

  1. List  —> It handles sequential list of objects. ArrayList, Vector and LinkedList classes implement this interface.
  2. Queue  —> It handles special list of objects in which elements are removed only from the head. LinkedList and PriorityQueue classes implement this interface.
  3. Set  —> It handles list of objects which must contain unique element. This interface is implemented by HashSet and LinkedHashSet classes and extended by SortedSet interface which in turn, is implemented by TreeSet.
  4. Map  —> This is the one interface in Collection Framework which is not inherited from Collection interface. It handles group of objects as Key/Value pairs. It is implemented by HashMap and HashTable classes and extended by SortedMap interface which in turn is implemented by TreeMap.
  5. Three of above interfaces (List, Queue and Set) inherit from Collection interface. Although, Map is included in collection framework it does not inherit from Collection interface.


Collection Interface:


Collection interface is the root level interface in the collection framework. List, Queue and Set are all sub interfaces of Collection interface. JDK does not provide any direct implementations of this interface. But, JDK provides direct implementations of it’s sub interfaces.

Collection interface extends Iterable interface which is a member of java.lang package. Iterable interface has only one method called iterator(). It returns an Iterator object, using that object you can iterate over the elements of Collection.


List Interface:


List Interface represents an ordered or sequential collection of objects. This interface has some methods which can be used to store and manipulate the ordered collection of objects. The classes which implement the List interface are called as Lists. ArrayList, Vector and LinkedList are some examples of lists. You have the control over where to insert an element and from where to remove an element in the list.

Here are some properties of lists:
  1. Elements of the lists are ordered using Zero based index.
  2. You can access the elements of lists using an integer index.
  3. Elements can be inserted at a specific position using integer index. Any pre-existing elements at or beyond that position are shifted right.
  4. Elements can be removed from a specific position. The elements beyond that position are shifted left.
  5. A list may contain duplicate elements.
  6. A list may contain multiple null elements.

List interface extends Collection interface. So, all 15 methods of Collection interface are inherited to List interface. Along with these methods, another 9 methods are included in the List interface to support the properties of lists.


Queue Interface:


The Queue Interface extends Collection interface. It defines queue data structure which is normally First-In-First-Out. Queue is a data structure in which elements are added from one end and elements are deleted from another end. But, exception being the Priority Queue in which elements are removed from one end, but elements are added according to the order defined by the supplied comparator.

Queue is a data structure where elements are added from one end called tail of the queue and elements are removed from another end called head of the queue. Queue is also first-in-first-out type of data structure (except priority queue). That means an element which is inserted first will be the first element to be removed from the queue. You can’t add or get or set elements at an arbitrary position in the queues.

Properties of queues:

  1. Null elements are not allowed in the queue. If you try to insert null object into the queue, it throws NullPointerException.
  2. Queue can have duplicate elements.
  3. Unlike a normal list, queue is not random access. i.e you can’t set or insert or get elements at an arbitrary positions.
  4. In most of cases, elements are inserted at one end called tail of the queue and elements are removed or retrieved from another end called head of the queue.
  5. In the Queue Interface, there are two methods to obtain and remove the elements from the head of the queue. They are poll() and remove(). The difference between them is, poll() returns null if the queue is empty and remove() throws an exception if the queue is empty.
  6. There are two methods in the Queue interface to obtain the elements but don’t remove. They are peek() and element(). peek() returns null if the queue is empty and element() throws an exception if the queue is empty.



Deque Interface:


The Deque Interface is the short name for “Double Ended Queue“. As the name suggest, Deque is a linear collection of objects which supports insertion and removal of elements from both the ends. The Deque interface defines the methods needed to insert, retrieve and remove the elements from both ends.

The Deque interface is introduced in Java SE 6. It extends Queue interface.

The main advantage of Deque is that you can use it as both Queue (FIFO) as well as Stack (LIFO). The Deque interface has all those methods required for FIFO and LIFO operations. Some of those methods throw an exception if operation is not possible and some methods return a special value (null or false) if operation fails. 

How Deque – Double Ended Queue Works?

As already said, Deque is nothing but the double ended queue. That means, you can insert, retrieve and remove the elements from both the ends.

Deque As Queue:

As Deque interface extends Queue interface, it inherits all methods of Queue interface. So, you can use all those inherited methods to perform Queue operations. Along with them, methods defined in the Deque interface can also be used for Queue operations.

Deque As Stack:

Deque interface has two more methods – pop() and push(). These two methods make Deque to function as a stack (Last-In-First-Out). Along with these two methods, you can also use addFirst(), peekFirst() and removeFirst() for stack operations.

Properties of dequeues:

  1. Unlike Queue, Deque can have null elements. But, it is recommended not to insert null elements as many methods return null to indicate Deque is empty.
  2. Deque can have duplicate elements.
  3. You can’t set or get or insert the elements at an arbitrary position of Deque. i.e Random access is not possible with the Deque.
  4. You can use removeFirstOccurrenec(E e), removeLastOccurrence(E e) and remove(E e) methods to delete the elements from the Deque.



Set Interface:


The Set interface defines a set. The set is a linear collection of objects with no duplicates. Duplicate elements are not allowed in a set. The Set interface extends Collection interface. Set interface does not have it’s own methods. All it’s methods are inherited from Collection interface. The only change that has been made to Set interface is that add() method will return false if you try to insert an element which is already present in the set.

Properties of sets:
  1. Set contains only unique elements. It does not allow duplicates.
  2. Set can contain only one null element.
  3. Random access of elements is not possible.
  4. Order of elements in a set is implementation dependent. HashSet elements are ordered on hash code of elements. TreeSet elements are ordered according to supplied Comparator (If no Comparator is supplied, elements will be placed in ascending order) and LinkedHashSet maintains insertion order.
  5. Set interface contains only methods inherited from Collection interface. It does not have it’s own methods. But, applies restriction on methods so that duplicate elements are always avoided.
  6. One more good thing about Set interface is that the stronger contract between equals() and hashCode() methods. According to this contract, you can compare two set instances of different implementation types (HashSet, TreeSet and LinkedHashSet).
  7. Two set instances, irrespective of their implementation types, are said to be equal if they contain same elements.



SortedSet Interface:


The SortedSet interface extends Set interface. SortedSet is a set in which elements are placed according to supplied comparator. This Comparator is supplied while creating a SortedSet. If you don’t supply comparator, elements will be placed in ascending order.

Properties of sorted sets:
  1. SortedSet can not have null elements. If you try to insert null element, it gives NullPointerException at run time.
  2. As SortedSet is a set, duplicate elements are not allowed.
  3. SortedSet elements are sorted according to supplied Comparator. If you don’t mention any Comparator while creating a SortedSet, elements will be placed in ascending order.
  4. Inserted elements must be of Comparable type and they must be mutually Comparable.
  5. You can retrieve first element and last elements of the SortedSet. You can’t access SortedSet elements randomly. i.e Random access is denied.
  6. SortedSets returned by headSet(), tailSet() and subSet() methods are just views of the original set. So, changes in the returned set are reflected in the original set and vice versa.



NavigableSet Interface:


The NavigableSet is a SortedSet with navigation facilities. The NavigableSet interface provides many methods through them you can easily find closest matches of any given element. It has the methods to find out less than, less than or equal to, greater than and greater than or equal of any element in a SortedSet.

Properties of navigable sets:
  1. NavaigableSet can’t have null elements.
  2. NavigableSet doesn’t support duplicate elements.
  3. NavigableSet can be traversed and accessed in either ascending or descending order.
  4. Methods subSet(), headSet() and tailSet() differ from SortedSet interface in taking additional arguments describing whether upper bound and lower bound are inclusive or exclusive.



ArrayList Class:


ArrayList class, in simple terms, can be defined as re-sizable array. ArrayList is same like normal array but it can grow and shrink dynamically to hold any number of elements. ArrayList is a sequential collection of objects which increases or decreases in size as we add or delete the elements.

In ArrayList, elements are positioned according to Zero-based index. That means, elements are inserted from index 0. Default initial capacity of an ArrayList is 10. This capacity increases automatically as we add more elements to arraylist. You can also specify initial capacity of an ArrayList while creating it.

ArrayList class implements List interface and extends AbstractList. It also implements 3 marker interfaces – RandomAccess, Cloneable and Serializable.

Some properties of array lists:

  1. Size of the ArrayList is not fixed. It can increase and decrease dynamically as we add or delete the elements.
  2.  ArrayList can have any number of null elements.
  3. ArrayList can have duplicate elements.
  4. As ArrayList implements RandomAccess, you can get, set, insert and remove elements of the ArrayList from  any arbitrary position.
  5. When you insert an element in the middle of the ArrayList, the elements at the right side of that position are shifted one position right and when you delete an element, they will be shifted one position left. This feature of the ArrayList causes some performance issues as shifting of elements is time consuming if ArrayList has lots of elements.
  6. Elements are placed according to Zero-based index. That means, first element will be placed at index 0 and last element at index n-1, where ‘n’ is the size of the ArrayList.
  7. ArrayList is not synchronized. That means, multiple threads can use same ArrayList simultaneously.
  8. If you know the element, you can retrieve the position of that element.



Difference Between Iterator And ListIterator:


Iterator and ListIterator are two interfaces in Java collection framework which are used to traverse the collections. Although ListIterator extends Iterator, there are some differences in the way they traverse the collections.

1. Using Iterator, you can traverse List, Set and Queue type of objects. But using ListIterator, you can traverse only List objects. In Set and Queue types, there is no method to get the ListIterator object. But, In List types, there is a method called listIterator() which returns ListIterator object.
2. Using Iterator, we can traverse the elements only in forward direction. But, using ListIterator you can traverse the elements in both the directions – forward and backward. ListIterator has those methods to support the traversing of elements in both the directions.


Vector Class:


The Vector Class is also dynamically grow-able and shrink-able collection of objects like an ArrayList class. But, the main difference between ArrayList and Vector is that Vector class is synchronized. That means, only one thread can enter into vector object at any moment of time.

Vector class is preferred over ArrayList class when you are developing a multi threaded application. But, precautions need to be taken because vector may reduce the performance of your application as it is thread safety and only one thread is allowed to have object lock at any moment of time and remaining threads have to wait until a thread releases the object lock which is held by it. So, it is always recommended that if you don’t need thread safety environment, it is better to use ArrayList class than the Vector class.

Vector class has same features as ArrayList. Vector class also extends AbstractList class and implements List interface. It also implements 3 marker interfaces – RandomAccess, Cloneable and Serializable.

Some properties of vectors:

  1. The main feature of Vector class is that it is thread safety. All methods of Vector class are synchronized so that only one thread can execute them at any given time. This feature of Vector class is useful when you need thread safety code.
  2. Thread safety property of Vector class effects the performance of an application as it makes threads to wait for object lock.
  3. Capacity Increment: Capacity increment is an amount by which the capacity of the vector is automatically incremented whenever size of the vector exceeds it’s capacity. You can pass this capacity increment while creating a vector. If you don’t pass, capacity increment will be treated as zero and capacity of the vector will be doubled whenever size exceeds capacity.
  4. Unlike an ArrayList, you can set the size of the Vector manually. If the new size is greater than the current size, the new slots will be filled with null elements. If the new size is smaller than current size, then the extra elements will be discarded.
  5. You can traverse the vector using Enumeration object. Vector class has a method called elements() which returns an Enumeration object consisting of all elements of Vector.
  6. Vector class has separate methods to retrieve first and last element of vector object. You will not find these methods in ArrayList class. firstElement() retrieves first element and lastElement()method retrieves last element of the vector.



LinkedList Class:


In general terms, LinkedList class is a data structure where each element consist of three things. First one is the reference to previous element, second one is the actual value of the element and last one is the reference to next element.

The LinkedList class in Java is an implementation of doubly linked list which can be used both as a List as well as Queue. The LinkedList in java can have any type of elements including null and duplicates. Elements can be inserted and can be removed from both the ends and can be retrieved from any arbitrary position.

The LinkedList class extends AbstractSequentialList and implements List and Deque interfaces. It also implements 2 marker interfaces – Cloneable and Serializable.

Properties of linked lists:

  1. Elements in the LinkedList are called as Nodes. Where each node consist of three parts – Reference To Previous Element, Value Of The Element and Reference To Next Element. Below diagram shows how LinkedList looks like.
  2. Reference To Previous Element of first node and Reference To Next Element of last node are null as there will be no elements before the first node and after the last node.
  3. You can insert the elements at both the ends and also in the middle of the LinkedList. Below is the list of methods for insertion operations.
  4. You can remove the elements from the head, from the tail and also from the middle of the LinkedList.
  5. You can retrieve the elements form the head, from the middle and from the tail of the LinkedList.
  6. Insertion and removal operations in LinkedList are faster than the ArrayList. Because in LinkedList, there is no need to shift the elements after each insertion and removal. only references of next and previous elements need to be changed.
  7. Retrieval of the elements is very slow in LinkedList as compared to ArrayList. Becaues in LinkedList, you have to traverse from beginning or end (whichever is closer to the element) to reach the element.
  8. The LinkedList can be used as stack. It has the methods pop() and push() which make it to function as Stack.
  9. The LinkedList can also be used as ArrayList, Queue, SIngle linked list and doubly linked list.
  10. LinkedList can have multiple null elements.
  11. LinkedList can have duplicate elements.
  12. LinkedList class in Java is not of type Random Access. i.e the elements can not be accessed randomly. To access the given element, you have to traverse the LinkedList from beginning or end (whichever is closer to the element) to reach the given element.



PriorityQueue Class:


The PriorityQueue is a queue in which elements are ordered according to specified Comparator. You have to specify this Comparator while creating a PriorityQueue itsel. If no Comparator is specified, elements will be placed in their natural order. The PriorityQueue is a special type of queue because it is not a First-In-First-Out (FIFO) as in the normal queues. But, elements are placed according to supplied Comparator.

The PriorityQueue does not allow null elements. Elements in the PriorityQueue must be of Comparable type, If you insert the elements which are not Comparable, you will get ClassCastException at run time.

PriorityQueue class extends AbstractQueue class which in turn implements Queue interface. PriorityQueue also implements one marker interface – java.io.Serializable interface.

Properties of priority queues:

  1. Elements in the PriorityQueue are ordered according to supplied Comparator. If Comparator is not supplied, elements will be placed in their natural order.
  2. The PriorityQueue is unbounded. That means the capacity of the PriorityQueue increases automatically if the size exceeds capacity. But, how it grows is not specified.
  3. The PriorityQueue can have duplicate elements but can not have null elements.
  4. All elements of the PriorityQueue must be of Comparable type. Otherwise ClassCastException will be thrown at run time.
  5. The head element of the PriorityQueue is always the least element and tail element is always the largest element according to specified Comparator.
  6. The default initial capacity of PriorityQueue is 11.
  7. You can retrieve the Comparator used to order the elements of the PriorityQueue using comparator() method.
  8. PriorityQueue is not a thread safe.



ArrayDeque Class:


The ArrayDeque class in Java is introduced from JDK 1.6. It is an implementation of Deque Interface which allows insertion of elements at both the ends. It does not have any restrictions on capacity. It expands automatically as we add more elements. The ArrayDeque class extends AbstractCollection class and implements Deque interface. It also implements Cloneable and Serializable marker interfaces.

Properties of array dequeues:

  1. ArrayDeque is a resizable-array implementation of Deque interface like ArrayList class which is a resizable-array implementation of List interface. But, ArrayDeque is not a List.
  2. ArrayDeque does not have any capacity limit. It will grow automatically as we add elements.
  3. Default initial capacity of ArrayDeque is 16. It will increase at a power of 2 (24, 25, 26 and so on) when size exceeds capacity.
  4. ArrayDeque can be used as a stack (LIFO) as well as a queue (FIFO). ArrayDeque is faster than the Stack class when used as a stack and faster than the LinkedList class when used as a queue.
  5. Performance of ArrayDeque is sometimes considered as the best among the collection framework. It gives performance of O(1) for insertion, removal and retrieval operations. ArrayDeque class is recommended instead of Stack class (when you want stack data structure) and instead of LinkedList class (when you want queue data structure).
  6. You can’t perform indexed operations on ArrayDeque. ArrayDeque doesn’t have the methods to support those operations.
  7. ArrayDeque is not a thread safe.



HashSet Class:


The HashSet class in Java is an implementation of Set interface. HashSet is a collection of objects which contains only unique elements. Duplicates are not allowed in HashSet. HashSet gives constant time performance for insertion, removal and retrieval operations. It allows only one null element.

The HashSet internally uses HashMap to store the objects. The elements you insert in HashSet will be stored as keys of that HashMap object and their values will be a constant called PRESENT. This constant is defined as private static final Object PRESENT = new Object() in the source code of HashSet class.

HashSet class extends AbstractSet class and implements Set interface. It also implements Cloneable and Serializable marker interfaces.

Properties of hash sets:

  1. HashSet class uses HashMap internally to store the objects. The keys of that HashMap object will be the elements of HashSet and their values will be a constant.
  2. HashSet does not allow duplicate elements. If you try to insert a duplicate element, older element will be overwritten.
  3. HashSet can have maximum one null element.
  4. HashSet doesn’t maintain any order. The order of the elements will be largely unpredictable. And it also doesn’t guarantee that order will remain constant over time.
  5. HashSet offers constant time performance for insertion, removal and retrieval operations.
  6. HashSet class is not synchronized. If you want synchronized HashSet, use Collections.synchronizedSet() method.



LinkedHashSet Class:


The LinkedHashSet in java is an ordered version of HashSet which internally maintains one doubly linked list running through it’s elements. This doubly linked list is responsible for maintaining the insertion order of the elements. Unlike HashSet which maintains no order, LinkedHashSet maintains insertion order of elements. i.e elements are placed in the order they are inserted. LinkedHashSet is recommended over HashSet if you want a unique collection of objects in an insertion order.

The LinkedHashSet class extends HashSet class and implements Set interface. It also implements Cloneable and Serializable marker interfaces.

Properties of linked hash sets:

  1. LinkedHashSet internally uses LinkedHashMap to store it’s elements just like HashSet which internally uses HashMap to store it’s elements.
  2. LinkedHashSet maintains insertion order. This is the main difference between LinkedHashSet and HashSet.
  3. LinkedhashSet also gives constant time performance for insertion, removal and retrieval operations. The performance of LinkedHashSet is slightly less than the Hashset as it has to maintain doubly linked list internally to order it’s elements.
  4. Iterator returned by LinkedHashSet is fail-fast. i.e if the LinkedHashSet is modified at any time after the Iterator is created, it throws ConcurrentModificationException.
  5. LinkedHashSet doesn’t allow duplicate elements and allows only one null element.
  6. LinkedHashSet is not synchronized. To get the synchronized LinkedHashSet, use Collections.synchronizedSet() method.



TreeSet Class:


The TreeSet is another popular implementation of Set interface. We have seen other two implementations of Set interface – HashSet and LinkedHashSet. HashSet doesn’t maintain any order where as LinkedHashSet maintains insertion order. The main difference between these two implementations and Treeset is, elements in TreeSet are sorted according to supplied Comparator. You need to supply this Comparator while creating a TreeSet itself. If you don’t pass any Comparator while creating a TreeSet, elements will be placed in their natural ascending order.

The TreeSet class in java is a direct implementation of NavigableSet interface which in turn extends SortedSet interface (which in turn extends Set interface). Below is the hierarchy diagram of TreeSet class.

Properties of tree sets:

  1. The elements in TreeSet are sorted according to specified Comparator. If no Comparator is specified, elements will be placed according to their natural ascending order.
  2. Elements inserted in the TreeSet must be of Comparable type and elements must be mutually comparable. If the elements are not mutually comparable, you will get ClassCastException at run time.
  3. TreeSet does not allow even a single null element.
  4. TreeSet is not synchronized. To get a synchronized TreeSet, use Collections.synchronizedSortedSet() method.
  5. TreeSet gives performance of order log(n) for insertion, removal and retrieval operations.
  6. Iterator returned by TreeSet is of fail-fast nature. That means, If TreeSet is modified after the creation of Iterator object, you will get ConcurrentModificationException.
  7. TreeSet internally uses TreeMap to store it’s elements just like HashSet and LinkedHashSet which use HashMap and LinkedHashMap respectively to store their elements.



Map Interface:


The Map interface in java is one of the four top level interfaces of Java Collection Framework along with List, Set and Queue interfaces. But, unlike others, it doesn’t inherit from Collection interface. Instead it starts it’s own interface hierarchy for maintaining the key-value associations. Map is an object of key-value pairs where each key is associated with a value. This interface is the replacement for ‘Dictionary‘ class which is an abstract class introduced in JDK 1.0.

HashMap, LinkedHashMap and TreeMap are three popular implementations of Map interface. 

Properties of maps:

  1. Map interface is a part of Java Collection Framework, but it doesn’t inherit Collection Interface.
  2. Map interface stores the data as a key-value pairs where each key is associated with a value.
  3. A map can not have duplicate keys but can have duplicate values.
  4. Each key at most must be associated with one value.
  5. Each key-value pairs of the map are stored as Map.Entry objects. Map.Entry is an inner interface of Map interface.
  6. The common implementations of Map interface are HashMap, LinkedHashMap and TreeMap.
  7. Order of elements in map is implementation dependent. HashMap doesn’t maintain any order of elements. LinkedHashMap maintains insertion order of elements. Where as TreeMap places the elements according to supplied Comparator.
  8. The Map interface provides three methods, which allows map’s contents to be viewed as a set of keys (keySet() method), collection of values (values() method), or set of key-value mappings (entrySet() method).



What is the difference between Collection and Collections in java?


This is one of the most confusing java interview question asked many a times to java freshers. Most of time, this question has been asked to java freshers to check their basic knowledge about the Java Collection Framework. 

This question seems confusing because both “Collection” and “Collections” look similar. Both are part of java collection framework, but both serve different purpose. Collection is a top level interface of java collection framework where as Collections is an utility class. In this article, we will discuss the differences between Collection and Collections in java.

Collection Interface:

Collection is a root level interface of the Java Collection Framework. Most of the classes in Java Collection Framework inherit from this interface. List, Set and Queue are main sub interfaces of this interface. JDK doesn’t provide any direct implementations of this interface. But, JDK provides direct implementations of it’s sub interfaces. ArrayList, Vector, HashSet, LinkedHashSet, PriorityQueue are some indirect implementations of Collection interface. Map interface, which is also a part of java collection framework, doesn’t inherit from Collection interface. Collection interface is a member of java.util package.

Collections Class:

Collections is an utility class in java.util package. It consists of only static methods which are used to operate on objects of type Collection. For example, it has the method to find the maximum element in a collection, it has the method to sort the collection, it has the method to search for a particular element in a collection. Below is the list of some important methods of Collections class.

Online Encyclopedia of Statistical Science (Free)

Please, click on the chart below to go to the source: