Category Archives: Java

Using unmodifiable list Java 9

package test;package test; import java.util.Collections;import java.util.Iterator;import java.util.List; public class listtest { public static void main(String[] args) { // TODO Auto-generated method stub List im=List.of(“one”,”two”,”three”); List imlist=Collections.unmodifiableList(im); imlist.add(“four”); Iterator itr=imlist.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); } } } In this case if you … Continue reading

Posted in Java, Uncategorized | Leave a comment

Using List.of Java 9

package test;package test; import java.util.Iterator;import java.util.List; public class ListTest { public static void main(String[] args) { List im=List.of(); Iterator itr=im.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); } } }

Posted in Java, Uncategorized | Leave a comment

Using Mockito and Testng in Java

I am using maven and have imported maven dependency for Mockito . import static org.mockito.Mockito.*; import java.util.List; import org.testng.annotations.Test; public class TestMockito { /** * Test Mockito using Java and TestNG. */ @Test public void testmock() { @SuppressWarnings(“unchecked”) List<String> list … Continue reading

Posted in Java, TestNG, Uncategorized | Leave a comment

Print Factorial of Number using Lambda Expression Java

import org.testng.annotations.Test; /** * Print Factorial of a Number using Lambda Expression Java * @author asharda * */ public class Factorial { interface Fact { int result(int n); } @Test public void testFactorial() { Fact f= (n) -> { int … Continue reading

Posted in Java, Uncategorized | Leave a comment

How to check if number is an even number using Lambda Expression -Java

import org.testng.annotations.Test; /** * Check if number is an even number using Lambda expression Java * @author asharda * */ public class TestLambdaExpression { interface MyNumber { boolean getVal(int n); } @Test public void testLambda() { MyNumber myNum; myNum= (n)-> … Continue reading

Posted in Java, Uncategorized | Leave a comment

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