Category Archives: Java 8

Rotate Array

/**Program to rotate array * @author asharda * */ public class RotateArray { /** * @param args */ public static void main(String[] args) { RotateArray r =new RotateArray(); int arr[]= {1,2,3,4,5,6,7}; int b[]=r.rotateArray(arr,2); for(int i:b) { System.out.println(i); } } /** … Continue reading

Posted in Data Structures, Java, Java 8 | Leave a comment

Find minimum in ArrayList using Stream API

import java.util.ArrayList; import java.util.Optional; import java.util.stream.Stream; import org.testng.annotations.Test; public class StreamMin { /** * Find min of arraylist using stream */ @Test public void testStreams() { ArrayList<Integer> list=new ArrayList<Integer>(); list.add(10); list.add(20); Stream<Integer> stream=list.stream(); Optional<Integer>min=stream.min(Integer::compare); System.out.println(“Minimum element seen is “+min); } … Continue reading

Posted in Data Structures, Java, Java 8, Uncategorized | Tagged | Leave a comment

Find Unique Chars in a word

package test5; import java.util.ArrayList; import org.testng.Assert; import org.testng.annotations.Test; /** * Code to validate if String contains unique characters * @author asharda * */ public class UniqueChars { @Test public void testUnique() { UniqueChars w=new UniqueChars(); Assert.assertTrue(w.isUnique(“word”), “Chars are not unique”); … Continue reading

Posted in Java, Java 8, Uncategorized | Leave a comment

Find depth of binary tree

import java.util.Stack; /** * Find Depth of Binary Tree * @author asharda * */ class TreeNodes { int data; TreeNodes left; TreeNodes right; TreeNodes(int data) { this.data=data; } } public class TreeDepth { static int count=0; public void depth(Stack<TreeNodes> st) … Continue reading

Posted in Data Structures, Java, Java 8, Uncategorized | Tagged , | Leave a comment

Fibonnaic series using Java

  /** * Fibonnaci Series * @author asharda * */ public class Fibonnaci { public int getFib(int num) { if(num<2) { return num; } return getFib(num-1)+getFib(num-2); //return f; } public static void main(String[] args) { Fibonnaci f =new Fibonnaci(); System.out.println(“Fibonaci … Continue reading

Posted in Data Structures, Design Pattern, Java, Java 8, Uncategorized | Tagged , | Leave a comment

Find Peak Element in an array Java

  import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; /** * @author asharda * */ public class Peak { public int findPeakElement(int[] nums) { Integer[] num = new Integer[nums.length]; for (int i = 0; i < nums.length; i++) { num[i] = Integer.valueOf(nums[i]); … Continue reading

Posted in Data Structures, Java, Java 8, Uncategorized | Tagged | Leave a comment

Reverse Singly LinkedList using Java

/** * Reverse LinkedList * @author asharda * */ class Node { int data; Node next; Node(int data) { this.data=data; } } public class ReverseLinkedList { static Node root; Node temp=null; public void insert(int data) { if(root==null) { root=new Node(data); … Continue reading

Posted in Data Structures, Java, Java 8, Uncategorized | Tagged | Leave a comment