Skip to main content

Posts

Showing posts from November, 2020

Find and Print all duplicate characters in given string | Java example

Java program for find all duplicate characters in given String  Or Java program to display only repeated letters in a integer string Solution 1 :- import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class StreamApi {     public static void main(String[] args) {                 Scanner sc = new Scanner(System.in);         System.out.println("Enter String");         String str = sc.nextLine();                 char[] charArray = str.toCharArray();                 Map<Character, Integer> map = new HashMap<>();         for (char c : charArray) {             if (!map.containsKey(c)) {                 map.put(c, 1);             } else {                 map.put(c, map.get(c)+1);             }         }                 for (Entry<Character, Integer> c : map.entrySet()) {             if (c.getValue() > 1) {                 System.out.println(c.getKey());             }         }     } } Output :- Enter String

Stream Operations | Intermediate and Terminal Stream Tutorial

Intermediate and Terminal Operation in Java 8 Stream API   The java stream API provides a functional approach to processing collection of objects. Stream API added in java 8. Stream operations are divided into intermediate and terminal operations and are combined to form stream pipelines. A Stream pipeline is source such as array, collection, I/O channel or generator function. In Java 8, Stream API have two types of operations :- Intermediate Operation Terminal Operation 1. Intermediate Operation Intermediate operations are lazy operations means it is always returns new Stream. Intermediate operations does not produce result. Following are Intermediates methods of stream :- filter() map() sorted() flatMap() Distinct() limit() peek() Lazy operations are allows significant efficiencies. In a pipeline such as filtering, mapping and summing can be fused into a single pass on the data, with minimal intermediate state. Laziness also avoids examining the all data when it is not necessary. In

Jumping on the Clouds HackerRank Java Solution

Jumping on the Clouds HackerRank Java Solution | Programming Blog   Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from her starting postion to the last cloud. It is always possible to win the game.  For each game, Emma will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided. For example,  c = [0, 1, 0 , 0 ,0, 1, 0] indexed from 0...6. The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5.  She could follow the following two paths: 0 -> 2 -> 4 -> 6 or 0 -> 2 -> 3 -> 4 -> 6. The first path takes 3 jumps while the second takes 4. Here is the link for Hacker Rank problem : Hacker Rank Ju

What is Difference between JpaRepository and CrudRepository in Spring Data JPA

What is Difference between JpaRepository and CrudRepository? CrudRepository :- CrudRepository provides mainly CRUD (Create, Read, Update and Delete) operation.  The CrudRepository interface provides methods for CRUD operations, so it allows you to create, read, update and delete records without having to define your own methods. Interface for generic CRUD operations on a repository for a specific type. CrudRepository is base interface and extends the Repository interface. public interface CrudRepository<T, ID> extends Repository<T, ID> JpaRepository :- JpaRepository provides CRUD operation as well as provides JPA related methods such as flushing the persistence context and delete records in a batch. JpaRepository extends PagingAndSortingRepository which in turn extends CrudRepository . JpaRepository add some more functionality that is specific to JPA. The PagingAndSortingRepository provides additional methods to retrieve entities using pagination and sorting.  public int