Found 4335 Articles for Java 8

Java Program to check if any String in the list starts with a letter

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

4K+ Views

First, create a List with String elements:List myList = new ArrayList(); myList.add("pqr"); myList.add("stu"); myList.add("vwx"); myList.add("yza"); myList.add("bcd"); myList.add("efg"); myList.add("vwxy");Use the startsWith() method to check if any of the above string in the myList begins with a specific letter:myList.stream().anyMatch((a) -> a.startsWith("v"));TRUE is returned if any of the string begins with the specific letter, else FALSE.The following is an example to check if any String in the list starts with a letter:Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(final String[] args) {       List myList = new ArrayList();       myList.add("pqr");       myList.add("stu"); ... Read More

Java Program to get the reverse of an Integer array with Lambda Expressions

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

422 Views

Let us first declare and initialize an Integer array:Integer[] arr = {20, 50, 75, 100, 120, 150, 170, 200};To find the reverse of the Integer array with Lambda, use sort and the Lamda:Arrays.sort(arr, (Integer one, Integer two) -> { return (two- one); });The following is an example to reverse an Integer array with Lambda Expressions:Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       Integer[] arr = {20, 50, 75, 100, 120, 150, 170, 200};       System.out.println("Integer Array elements...");       for (int res : arr)       {     ... Read More

How to sort array of strings by their lengths following longest to shortest pattern in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

248 Views

At first, let us create and array of strings:String[] strArr = { "ABCD", "AB", "ABCDEFG", "ABC", "A", "ABCDE", "ABCDEF", "ABCDEFGHIJ" }Now, for longest to shortest pattern, for example ABCDEFGHIJ, ABCDEFG, ABCDEF, etc.; get the length of both the string arrays and work them like this:Arrays.sort(strArr, (str1, str2) → str2.length() - str1.length());The following is an example to sort array of strings by their lengths with longest to shortest pattern in Java:Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = { "ABCD", "AB", "ABCDEFG", "ABC", "A", "ABCDE", "ABCDEF", "ABCDEFGHIJ" };     ... Read More

Java Program to display a webpage in JEditorPane

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

508 Views

Use the setPage() method of the JEditorPane to display a webpage:JEditorPane editorPane = new JEditorPane(); editorPane.setPage("https://www.tutorialspoint.com");The following is an example to display a webpage in JEditorPane:Exampleimport java.io.IOException; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; public class SwingDemo {    public static void main(String[] args) {       JEditorPane editorPane = new JEditorPane();       try {          editorPane.setPage("https://www.tutorialspoint.com");       } catch (IOException e) {          editorPane.setContentType("text/html");          editorPane.setText("Connection issues!");       }       JScrollPane pane = new JScrollPane(editorPane);       JFrame frame = new ... Read More

How to approach String as int stream in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

85 Views

Let’s say we have the following string:String str = "YuBM787Nm";Now to display it as IntStream, use filter() and map() as shown below:int res = str.chars() .filter(Character::isDigit) .map(ch → Character.valueOf((char) ch)).sum();The following is an example to display string as IntStream:Examplepublic class Demo {    public static void main(String[] args) {       String str = "YuBM787Nm";       int res = str.chars() .filter(Character::isDigit) .map(ch -> Character.valueOf((char) ch)).sum();       System.out.println("String as IntStream = "+res);    } }OutputString as IntStream = 166

How to change font size with HTML in Java Swing JEditorPane?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

701 Views

Use HTMLEditorKitt to change the font size with HTML. With that, use the JEditorPane setText() method to set HTML:HTMLEditorKit kit = new HTMLEditorKit(); editorPane.setEditorKit(kit); editorPane.setSize(size); editorPane.setOpaque(true); editorPane.setText(" This is a demo text with a different font!");The following is an example to change font size with HTML in Java Swing JEditorPane:Exampleimport java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.text.html.HTMLEditorKit; public class SwingDemo extends JFrame {    public static void main(String[] args) {       SwingDemo s = new SwingDemo();       s.setSize(600, 300);       Container container = s.getContentPane();       s.demo(container, container.getSize());   ... Read More

How to create IntSummaryStatistics from Collectors in Java?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

206 Views

Let us first create a List:List emp = Arrays.asList(    new Employee("John", "Marketing", 5),    new Employee("David", "Operations", 10));We have class Employee with name, department and rank of employees.Now, create summary for the rank like count, average, sum, etc:IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p -> p.rank));The following is an example to create IntSummaryStatistics from Collectors in Java:Exampleimport java.util.Arrays; import java.util.IntSummaryStatistics; import java.util.List; import java.util.stream.Collectors; public class Demo {    public static void main(String[] args) throws Exception {       List emp = Arrays.asList(new Employee("John", "Marketing", 5), new Employee("David", "Operations", 10));       IntSummaryStatistics summary = emp .stream() .collect(Collectors.summarizingInt(p → ... Read More

Java Program to convert int array to IntStream

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

2K+ Views

To convert int array to IntStream, let us first create an int array:int[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};Now, create IntStream and convert the above array to IntStream:IntStream stream = Arrays.stream(arr);Now limit some elements and find the sum of those elements in the stream:IntStream stream = Arrays.stream(arr); stream = stream.limit(7); System.out.println("Sum of first 7 elements = "+stream.sum());The following is an example to convert int array to IntStream:Exampleimport java.util.Arrays; import java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       int[] arr = {10, 20, 30, 40, 50, 60, ... Read More

Java Program to get the difference between two time zones by seconds

Samual Sam
Updated on 30-Jul-2019 22:30:25

721 Views

Here are the two timezones −ZoneId zone1 = ZoneId.of("America/Panama"); ZoneId zone2 = ZoneId.of("Asia/Taipei");Set the date −LocalDateTime dateTime = LocalDateTime.of(2019, 04, 11, 10, 5);Now, set the first timezone −ZonedDateTime panamaDateTime = ZonedDateTime.of(dateTime, zone1);Set the second timezone −ZonedDateTime taipeiDateTime = panamaDateTime.withZoneSameInstant(zone2);Get the timezone differences in seconds −taipeiDateTime.getOffset().getTotalSeconds()Exampleimport java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class Demo {    public static void main(String[] args) {       ZoneId zone1 = ZoneId.of("America/Panama");       ZoneId zone2 = ZoneId.of("Asia/Taipei");       LocalDateTime dateTime = LocalDateTime.of(2019, 04, 11, 10, 5);       ZonedDateTime panamaDateTime = ZonedDateTime.of(dateTime, zone1);       ZonedDateTime taipeiDateTime = ... Read More

Java Program to create custom DateTime formatter

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

141 Views

To create custom DateTime formatter, use DateTimeFormatter. Let us first see for Time −DateTimeFormatter dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.HOUR_OF_DAY) .appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR) .appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE) .toFormatter();For Date −dtFormat = new DateTimeFormatterBuilder() .appendValue(ChronoField.YEAR) .appendLiteral("/") .appendValue(ChronoField.MONTH_OF_YEAR) .appendLiteral("/") .appendValue(ChronoField.DAY_OF_MONTH) .toFormatter();Exampleimport java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; public class Demo {    public static void main(String[] args) {       DateTimeFormatter dtFormat = new DateTimeFormatterBuilder()       .appendValue(ChronoField.HOUR_OF_DAY)       .appendLiteral(":")       .appendValue(ChronoField.MINUTE_OF_HOUR)       .appendLiteral(":")       .appendValue(ChronoField.SECOND_OF_MINUTE)       .toFormatter();       System.out.println("Time = "+dtFormat.format(LocalDateTime.now()));       dtFormat = new DateTimeFormatterBuilder()     ... Read More

Advertisements