Category Archives: Java

Reverse an Integer -LeetCode

import java.util.ArrayList; import java.util.List; import org.testng.annotations.Test; /** * @author asharda * */ public class TestReverseInteger { @Test public void testReverseInteger() { int num=123; int div=num; int rem=0; List<Integer> list=new ArrayList<Integer>(); while(div>100) { div=num/10; rem=num%10; list.add(rem); } list.add(div%10); list.add(div/10); Integer [] … Continue reading

Posted in Uncategorized, Java, LeetCode Solutions | Leave a comment

Using Streams and Parallel Streams

  import java.util.ArrayList; import java.util.List; import java.util.stream.Stream; import org.testng.annotations.Test; /** * Test using Stream and Parallel Stream. * @author asharda * */ public class StreamTest { @Test public void testStreams() { List<Integer> list=new ArrayList<Integer>(); for(int i=0;i<100;i++) { list.add(i); } System.out.println(“Using … Continue reading

Posted in Java, Uncategorized | Leave a comment

NasHorn JavaScript JDK8

import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import org.testng.annotations.Test; /** * NasHorn JavaScript JDK8 * @author asharda * */ public class TestJJS { /** * Example Using NashHorn JS. * @throws ScriptException */ @Test public void testNashHorn() throws ScriptException { ScriptEngineManager … Continue reading

Posted in Java, Uncategorized | Leave a comment

Optionals JDK8

import java.util.Optional; import org.testng.annotations.Test; /** * Using Optionals JDK8 * @author asharda * */ public class OptionalTest { /** * Demo Using Optional. */ @Test public void testOptional() { Integer value1=null; Integer value2=new Integer(10); Optional<Integer> a =Optional.ofNullable(value1); Optional<Integer> b =Optional.of(value2); … Continue reading

Posted in Java, Uncategorized | Leave a comment

Using LookingAt and Matcher Java

import java.util.regex.Matcher; import java.util.regex.Pattern; import org.testng.annotations.Test; public class RegExTest { @Test public void testRegEx() { String REGEX=”foo”; String input=”foo”; Pattern pattern; pattern=Pattern.compile(REGEX); Matcher matcher=pattern.matcher(input); System.out.println(“Looking at”+ ” ” + matcher.lookingAt()); System.out.println(“Matches”+ ” ” +matcher.matches()); } }

Posted in Java, Uncategorized | Leave a comment

Sort in Descending Order using Comparator and Comparable

import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * Sort in Descending Order using Comparator and Comparable * * @author asharda * */ class Student implements Comparator<Student>, Comparable<Student> { int marks; String name; public Student() { } public Student(String … Continue reading

Posted in Java, Uncategorized | Leave a comment

Sort Arrays and Collections Java

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import org.testng.annotations.Test; /** * @author asharda * */ public class TestArray { /** * Sort Array */ @Test public void testArray() { String[] colors={“red”,”green”,”black”,”yellow”}; Arrays.sort(colors); for(String color:colors) System.out.println(color); } /** * Sort Collections. */ … Continue reading

Posted in Java, Uncategorized | Leave a comment