Skip to main content

Posts

Showing posts from December, 2020

Sort List of Object property in Java with check null values

Sort java List by Object property with null check using Comparator Example 1 :- Sorting a list without null check using Comparator inline anonymous class Programming.java public class Programming {     private int id;     private String language;     public Programming(int id, String language) {         this.id = id;         this.language = language;     }          public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }     public String getLanguage() {         return language;     }     public void setLanguage(String language) {         this.language = language;     }          @Override     public String toString() {         return "Programming [id=" + id + ", language=" + language + "]";     } } ComparatorDemo.java import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class ComparatorDemo {     public static void main(String[] args) {         

Hash Tables: Ransom Note HackerRank Solution | Java Solution

Java Solution for Hash Tables: Ransom Note HackerRank Problem Problem Description :- Harold is a kidnapper who wrote a ransom note, but now he is worried it will be traced back to him through his handwriting. He found a magazine and wants to know if he can cut out whole words from it and use them to create an untraceable replica of his ransom note. The words in his note are case-sensitive and he must use only whole words available in the magazine. He cannot use substrings or concatenation to create the words he needs. Given the words in the magazine and the words in the ransom note, print Yes  if he can replicate his ransom note exactly using whole words from the magazine; otherwise, print No . For example, the note is "Attack at dawn". The magazine contains only "attack at dawn". The magazine has all the right words, but there's a case mismatch. The answer is No . Function Description Complete the checkMagazine function in the editor below. It m

Spring Boot CRUD operation with Rest API, JPA and MySql | Programming Blog

Spring Boot CRUD with MySql, JPA, Hibernate and Rest API     In this Spring boot CRUD example, you will learn how to develop Spring boot application with REST full web service and Create, Read, Update and Delete data from MySql. 1. Start creating project in Spring Boot We uses Spring Initializr for creating our application. Go to following link, add artifact and name of your project. We uses PostMan for test our REST API. Downlaod PostMan from following link :- Postman Download In this tutorial, following MySql version used. 5.0.27 Add Three dependencies :- Spring Web - Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container. Spring Data JPA - Persist data in SQL stores with Java Persistence API using Spring Data and Hibernate. MySql Driver - MySQL JDBC and R2DBC driver. https://start.spring.io/   In this tutorial we used following Project Structure. Check out following image After all done, click on GENERATE button it wi

Minimum Swaps 2 HackerRank Solution | Java Solution

Minimum Swaps Two HackerRank Solution in Java You are given an unordered array consisting of consecutive integers [1,2,3,....n] without any duplicates. You are allowed to swap any two elements. You need to find the minimum number of swaps required to sort the array in ascending order.  For example, given the array arr = [7,1,3,2,4,5,6] we perform the following steps:  i arr swap (indices) 0 [7, 1, 3, 2, 4, 5, 6] swap (0,3) 1 [2, 1, 3, 7, 4, 5, 6] swap (0,1) 2 [1, 2, 3, 7, 4, 5, 6] swap (3,4) 3 [1, 2, 3, 4, 7, 5, 6] swap (4,5) 4 [1, 2, 3, 4, 5, 7, 6] swap (5,6) 5 [1, 2, 3, 4, 5, 6, 7] it took 5 swaps to sort the array. See full description in HackerRank Website :- Minimum Swaps 2 full description     public class MinimumSwaps2 {     public static void main(String[] args) {                 Scanner sc = new Scanner(System.in);         System.out.println("Enter array size");         int arraySize = sc.nextInt();                 i

New Year Chaos HackerRank in Java | Programming Blog

New Year Chaos HackerRank solution in Java It is New Year's Day and people are in line for the Wonderland rollercoaster ride. Each person wears a sticker indicating their initial position in the queue. Initial positions increment by 1 from 1 at the front of the line to n at the back. Any person in the queue can bribe the person directly in front of them to swap positions. If two people swap positions, they still wear the same sticker denoting their original places in line. One person can bribe at most two others . For example, if n=8 and person5 bribes person4 the queue will look like this: 1,2,3,5,4,6,7,8. Fascinated by this chaotic queue, you decide you must know the minimum number of bribes that took place to get the queue into its current state. If anyone has bribed more than two people, the line is too chaotic to compute the answer. See full description in HackerRank :- New Year Chos HackerRank Probelm Description   Solution 1 public class NewYearChaos {     public

Object Oriented Programming (OOP) concept in Java | Programming blog

OOPs Concept in java programming OOP :- OOP refers as Object Oriented Programming. OOP is a concept that is based on objects. As a name suggests it is Object oriented. Java is Object oriented programming language.  There are mainly four concepts in java :- Abstraction Encapsulation Inheritance Polymorphism Let's discuss all of above one by one. 1. Abstraction :- In simple term, Abstraction means hiding complexity and showing essential details to the user. Means using the things without knowing background details about it.  Abstraction is the technique of hiding implementation. Like, we use smartphone very efficiently and easy way, we don't need background details how it works. we simply use. Why should we use Abstraction? Reduce complexity Force users to extend implementation rather than modify Support cross platform by changing the implementation per platform In Java, we can use abstraction in two ways :- Using Abstract class Using Interface Learn more about Abstract class and