Hackerrank Solution: 1D Array – Part 2 using Kotlin

This tutorial will show you Hackerrank 1D Array Part 2 using Kotlin. Let’s play a game on an array! You’re standing at index 0 of an n-element array named game. From some index i (where 0 <= i < n), you can perform one of the following moves: Move Backward: If cell i-1 exists and contains a 0, you can walk back to cell i-1. Move Forward: If cell i+1 contains a zero, you can walk to cell i+1. If cell i+leap contains a zero, you can jump to cell…

Continue

CopyOnWriteArrayList in Java

What is CopyOnWriteArrayList CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrent API. It implements List interface like ArrayList, Vector and LinkedList but it is a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class. CopyOnWriteArrayList creates a fresh copy of the underlying array to implement all mutative operations like add, set etc. Creating a fresh copy is generally too expensive but may be more efficient than alternatives when traversal operations, i.e., iterations, vastly outnumber mutations, and is…

Continue

Java forEach example using Lambda Expression

This tutorial will show you how to use Java 8 forEach loop to iterate Collection using Lambda Expression. Prior to Java 8 or JDK 8 we used to use the for loop though it was for-each style but in Java 8 the inclusion of forEach loop simplifies the iteration process in mainly one line. Let’s look at the below example to see how it can be used and how simple it is using Java forEach loop.

Continue

Remove Duplicate Objects from a List using Java

In this post, I will show you how to remove duplicate objects from a List using Java’s Comparator interface implementation based on multiple fields in a POJO. You can also check the duplicate objects in a list by overriding hashCode() and equals() methods in the POJO class. In other words, you are going to get the unique objects from the list and discard the duplicate objects.

Continue

Find Duplicate Objects in a List using Java

In this post, I will show you how to find duplicate objects in a List using Java’s Comparator interface implementation based on multiple fields in a POJO. Prerequisites The following configurations are required in order to run the application Eclipse JDK 1.8 Have maven installed and configured Junit, Mockito, PowerMockito dependency in pom.xml Now we will see the below steps how to create a maven based Java project in Eclipse

Continue

LRU Cache implementation in Java

A cache is an amount of faster memory used to improve data access by storing portions of a data set the whole of which is slower to access. Sometimes the total data set are not actually stored at all; instead, each data item is calculated as necessary, in which case the cache stores results from the calculations. When we need a datum then first we check the cache if it contains the required datum. If it exists, the datum is used from the cache, without having to access the main…

Continue

Convert List, Map to JSON in REST Response using Jersey

Introduction In this post we will see how to convert List, Map to JSON in REST Response using Jersey API. We will create three separate REST methods to convert List to JSOn, Map to JSON and List of Map to JSON string. We will annotate the REST resource class with @Path for having the base URI of the REST resources. We will use @Produces annotations to indicate what type of data the REST resource methods will produce in the response. We also use @GET annotation for http GET verb. The…

Continue

Cucumber data table – convert one column table to a List

Introduction In this post we will see an example on cucumber data table – convert one column table to a list. Cucumber is a tool for running automated acceptance tests written in a behavior-driven development (BDD) style. Cucumber is written in the Ruby programming language. Cucumber projects are available for other platforms beyond Ruby. Cucumber works with Ruby, Java, .NET, Flex or web applications written in any language. It has been translated to over 40 spoken languages. – http://cukes.info/ The language that Cucumber understands is called Gherkin.

Continue

Comparator interface in Java

We will see what is comparator interface and how to use comparator interface in Java programming language. For more information please go through http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html A comparison function, which imposes a total ordering on some collection of objects. Comparator can be passed to a sort method (such as Collections.sort() or Arrays.sort()) to allow precise control over the sort order. Comparator can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don’t have…

Continue