Category Archives: Java 8

Queue in Java

package test5; import java.util.LinkedList; /** * Queues using LinkedList * @author asharda * */ public class Queues { LinkedList<Integer> list=new LinkedList<Integer>(); /** * Enqueue * @param data */ public void enqueue(int data) { list.add(data); } /** * Dequeue * @return … Continue reading

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

Stack using LinkedList in Java

package test5; import java.util.LinkedList; /** * Stack using LinkedList * @author asharda * */ public class Stack { LinkedList<Integer> list=new LinkedList<Integer>(); /** * Push Operation * @param data */ public void push(int data) { list.addFirst(data); } /** * Display contents … Continue reading

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

Check if string is isomorphic

package test5; import java.util.TreeMap; import org.testng.annotations.Test; /** * Check if a string is isomporpic * @author asharda * */ public class TestIsomorphic { public boolean validate(String s,String t) { char []sc=s.toCharArray(); char []tc=t.toCharArray(); TreeMap<Character,Character> sMap=new TreeMap<Character,Character>(); TreeMap<Character,Character> tMap=new TreeMap<Character,Character>(); for(Character … Continue reading

Posted in Data Structures, Java 8, LeetCode Solutions, TestNG, Uncategorized | Leave a comment

Simple Binary Search using Java

/** * Simple Binary Search * @author asharda * */ public class BinSearch { public static void main(String[] args) { BinSearch bin=new BinSearch(); int arr[]= {2,4,6,8,10,12,14,16}; boolean b=bin.binSearch(arr,0,arr.length-1,16); System.out.println(“Number found “+b); } public boolean binSearch(int arr[],int low,int high,int val) { … Continue reading

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

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