Category Archives: Java

Using Stream API Java

import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; import org.testng.annotations.Test; public class FilterTest { @Test public void testNotNull() { Stream<String> language=Stream.of(“java”,”python”,null); List<String> list=language.filter(x->x!=null).collect(Collectors.toList()); list.forEach(System.out::println); } @Test public void testNonZero() { Stream<Integer> numbers=Stream.of(1,2,3,0); List<Integer> list=numbers.filter(x->x!=0).collect(Collectors.toList()); list.forEach(System.out::println); } }

Posted in Java, Uncategorized | Leave a comment

Using Enums

public enum Calculate { PLUS, MINUS, DIVIDE, MULTIPLY; public long operation(long x,long y) throws Exception { switch(this) { case PLUS: return x+y; case MINUS: return x-y; case MULTIPLY: return x*y; case DIVIDE: return x/y; default: throw new Exception(“No Matching Enum”); … Continue reading

Posted in Java, Uncategorized | Leave a comment

For Each Java8

import java.util.ArrayList; import java.util.List; import org.testng.annotations.Test; public class TestLambda { @Test public void testForEach() { List<String> list1=new ArrayList<String>(); list1.add(“Sai”); list1.add(“Sainath”); list1.add(“Sairam”); list1.forEach(item->System.out.println(item)); } }

Posted in Java, Uncategorized | Leave a comment

Using Filters Java 8

import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import org.testng.annotations.Test; /** * Using Filters Java 8 * @author asharda * */ public class TestFilter { @Test public void testFilter() { List<String> list1 = Arrays.asList(“Sai”, “Hari”, “Govind”, “Ashish”); List<String> list = list1.stream().filter(output -> … Continue reading

Posted in Java, Uncategorized | Leave a comment

Print random 10 Integers Java

  import java.util.Random; import org.testng.annotations.Test; public class TestRandomInt { @Test public void testRandomInt() { Random random=new Random(); random.ints().limit(10).forEach(System.out::println); } }

Posted in Java, Uncategorized | Leave a comment

Check if an Integer is a Palindrome -Java

import org.testng.annotations.Test; /** * Test If an integer is Palindrome * @author asharda * */ public class PalindromeTest { @Test /** * Check if a given integer is a Palindrome. */ public void testPalindrome() { int num = 45; int … Continue reading

Posted in Java, Uncategorized | Leave a comment

Binary Search Test

import org.testng.annotations.Test; /** * Binary Search Test * @author asharda * */ public class BSearchTest { /** * Check if Number is Present in Array */ @Test public void testBinarySearch() { int arr[]={1,5,7,8,12,19}; boolean b=bSearch(arr,19); if(b==true) { System.out.println(“Number Found”); } … Continue reading

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