Skip to main content

Posts

Showing posts from January, 2022

What is Collection framework in Java? Why we need Collection? Collection hierarchy in Java

Guide to Collection framework in Java with Examples | Guide for choose which collection to use In simple term, The Java Collections Framework is a collection of interfaces and classes which helps in storing and processing the data efficiently. Java collections refer to a collection of individual objects that are represented as a single unit. Whoever class or interface implements or extends Collection interface, they can use all method of collection interface. Some of frequently used methods : add() addAll() remove() removeAll() size() clear() isEmpty() contains()  Why we need Collection in Java? As we all know we can use java Arrays for store, alter, delete, sort data then why we still need Collection? Arrays are not resizable and Collection is resizable. Java Collections Framework provides lots of different useful data types, like Linkedlist allows insertion anywhere in constant time. There are different implementation you can choose from for the same set of services : like, ArrayLi

How to print Pascal's Triangle by given index | Pascal's Triangle II LeetCode Solution

Pascal's Triangle II LeetCode Solution in Java Problem description : Return particular row index of Pascal's Triangle based on given index. Sample input and output : Input 1 : 4 Output 1 : [1, 4, 6, 4, 1] Input 2 : 7 Output 2 : [1, 7, 21, 35, 35, 21, 7, 1] In Pascal's triangle, each number is the sum of the two numbers directly above. You can see in above image. Lets see solution. Solution 1 : Using Java List import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PascalsTriangle {     public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         System.out.println("Enter row");         int row = sc.nextInt();         int[] array = new int[row];         print(row);     }          public static void print(int row) {         List<Integer> list = new ArrayList<>();         for (int i = 0; i <= row; i++) {             list.add(1);             for (int j = i-1; j > 0; j--) {