Search This Blog

Showing posts with label java api. Show all posts
Showing posts with label java api. Show all posts

Saturday, 16 January 2016

try with resource - dealing with failures

In the previous post we saw the try-with-resource statements and how the execution flows. Here I decided to look at the java.sql.Connection class which also implements the AutoCloseable interface.

Wednesday, 6 January 2016

Java Streams

We have been exploring Lambda expressions and Functional Interfaces in the past few posts. Now is the time to move to a related new  feature called Streams.

try with resource

I have often used the try-with-resources Statement that was introduced in Java 7. Today I decided to look a little deep into it.
In earlier versions of java when dealing with resources that need closing, the code something like below:

Wednesday, 4 November 2015

Primitives and Lambdas

II have been playing with the lambda expressions and they have all involved working with objects. There are also some classes in the SDK that have been created for working with primitives.

Wednesday, 28 October 2015

Interfaces and Default Methods

In an earlier post, I covered one of Java 8's new features - default methods in interfaces. While I did the pros there were some corner cases to be considered when implementing this feature.

Tuesday, 20 October 2015

The BiVersions of Java FunctionalInterfaces

In the previous post, we saw a version of the Function class that took two parameters into apply instead of one. Similar to the BiFunction interface, we also have BiPredicate, BinaryOperator, and BiSupplier .

Wednesday, 7 October 2015

Variations in java functions

In the previous post, we saw the Function Interface that takes an object of one type and transforms it into an object of another type. Consider the user case where we need to split a string and retain only the second half.

Thursday, 24 September 2015

The Function Interface

I worked with using lambdas for Comparator interface in my first post on Lambdas. We saw how a Function interface could be used to convert an object of one type into an object of another.

Thursday, 3 September 2015

Predicates to determine the truth !

We have seen some of the functional interfaces like Consumer and Supplier in the earlier posts. The next one I wanted to explore is the Predicate interface. This one is used for testing certain conditions on a passed instance and returns a boolean value.

Tuesday, 25 August 2015

Suppliers in Java Lambda World

In the previous post I had a look at Consumer function objects and how they were used in the forEach method of the Iterable class. An exactly opposite kind of interface to the Consumer is the Supplier interface. (like the name doesn't give it away)

Friday, 21 August 2015

Consumers in Java Lambda World

A I mentioned in the previous post, Lambda expressions have helped simplify Collection use cases. We saw that with sorting a Collection. Let me explore some more simplifications in this post.

Wednesday, 19 August 2015

Lambda

This is kind of late - very late, but then like always - better late than never. So while java lovers world over have dug deep, praised, criticized and in some cases confused the developer community about Lambda expressions in Java 8, I have finally woken and decided to throw my two cents into the pit.
I was kinda too busy/lazy until I got a code review that used Lambdas. With no escape but to review it, I had to start learning Lambdas.  This post is a collection of thoughts I found online and like always just my ready notes to be available for me to refer back when the next code review comes around. With that time to get started.....

Sunday, 3 August 2014

The ReadWriteLock

The lock interface in Java has a specialized version - ReadWriteLock. As per the interface documentation:

Thursday, 24 July 2014

Conditions and Locks

In the previous post we saw how the Lock API could be used as an alternative to synchronized block. We also saw how the Lock API allowed us to do chain locking. There is also a condition object closely associated with Locks:

Saturday, 12 July 2014

Locks in Java

After semaphores I decided to look at the Lock interface in Java. The first question that comes is "Why use locks when wait notify works well enough ?" I looked in the documentation for the class and the below lines stand out:

Tuesday, 1 April 2014

How HashMap works in Java

I was looking at the HashMap source code the other day and felt like writing down what I learned. Consider the start point to using the HashMap:
Map<String, Integer> map = new HashMap<String, Integer>();
This will Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

Monday, 24 February 2014

Reference Queues

In the previous posts we had a look at weak and soft references. The java.lang.ref package also includes a class ReferenceQueue. As per the class documentation:
Reference queues, to which registered reference objects are appended by the garbage 
collector after the appropriate reachability changes are detected.

Tuesday, 18 February 2014

Soft References in Java

In the last post I looked at using weak references in my java code. As we saw Java has not just strong and weak but also soft and phantom references.
Firstly, what are soft references ?

Sunday, 9 February 2014

Weak References in Java

I have been looking at the java.lang.ref package with a view to understanding references in java. It all started when I tried to understand WeakHashMap and its use in string pools. The various related terms mentioned totally flummoxed me and made some additional study essential. Below is a summary of my attempts at understanding.
Java objects in the memory are classified as:

Monday, 27 January 2014

Semaphores in Java

What exactly is a semaphore ?
To use the wiki definition:
In computer science, particularly in operating systems, a semaphore is a variable 
or abstract data type that is used for controlling access, by multiple processes, 
to a common resource in a parallel programming or a multi-user environment.