Found 4335 Articles for Java 8

Java Program to check if none of the string in the list matches the condition

Nancy Den
Updated on 30-Jul-2019 22:30:26

596 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 noneMatch() method to check if none of the above string in the myList begins with a specific letter:myList.stream().noneMatch((a) -> a.startsWith("f"));TRUE is returned if none of the string begins with the specific letter, else FALSE.The following is an example to check if none of the string in the list matches the condition: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");       ... Read More

Java Program to convert integer to String with Map

Nancy Den
Updated on 30-Jul-2019 22:30:26

479 Views

Let’s say we have an Integer array with the following elements:20, 50, 100, 200, 250, 300, 500, 550, 600, 700Convert it to List:Arrays.asList(20, 50, 100, 200, 250, 300, 500, 550, 600, 700)Use Map to get the values greater than 400 and convert to String:filter(val -> val > 400) .map(val -> "Value greater than 400 = " + String.valueOf(val))The following is an example to convert integer to String with Map:Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       Arrays.asList(20, 50, 100, 200, 250, 300, 500, 550, 600, 700)          .stream()   ... Read More

How to convert File into a Stream in Java?

Nancy Den
Updated on 30-Jul-2019 22:30:26

548 Views

Let’s say we have a file “input.txt” here in the directory E:/ with the following content:Open a file with Bufferedreader. We have taken the above file here which is located at E: directory;BufferedReader buffReader = Files.newBufferedReader(Paths.get("E:\input.txt"), StandardCharsets.UTF_8);Now get the stream of lines from the above file and display:buffReader.lines().forEach(System.out::println);The following is an example to convert File into a Stream in Java:Exampleimport java.io.BufferedReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; public class Demo {    public static void main(String[] argv) throws Exception {       BufferedReader buffReader = Files.newBufferedReader(Paths.get("E:\input.txt"), StandardCharsets.UTF_8);       System.out.println("Stream of lines...");       buffReader.lines().forEach(System.out::println);    } ... Read More

How to transform List string to upper case in Java?

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

3K+ Views

Let’s first create a List string:List list = Arrays.asList("David", "Tom", "Ken", "Yuvraj", "Gayle");Now transform the above list to upper case:list.stream().map(players -> players.toUpperCase())To display, use forEach():list.stream().map(players -> players.toUpperCase()) .forEach(players -> System.out.print(players + ""));The following is an example to transform List string to upper case:Exampleimport java.util.Arrays; import java.util.List; public class Demo {    public static void main(final String[] args) {       List list = Arrays.asList("David", "Tom", "Ken", "Yuvraj", "Gayle");       System.out.print("List = "+list);       System.out.print("Uppercase strings = ");       list.stream().map(players -> players.toUpperCase()) .forEach(players -> System.out.print(players + ""));    } }OutputList = [David, Tom, Ken, ... Read More

What happens when JDialog is set with Modality type MODELESS in Java

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

77 Views

Modeless dialog boxes are on the screen and are available for use. The following is an example to set JDialog with Modality type MODELESS:Exampleimport java.awt.Cursor; import java.awt.Dialog.ModalityType; import java.awt.Dimension; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setSize(new Dimension(600, 400));       JDialog dialog = new JDialog(frame, "New", ModalityType.MODELESS);       dialog.setSize(300, 300);       frame.add(new JButton(new AbstractAction("Click to generate") {          @Override          public void actionPerformed(ActionEvent ... Read More

Java Program to convert Stream to List

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

156 Views

Declare and initialize an Integer array:Integer[] arr = {50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000};Now, create a stream with the above elements:Stream stream = Arrays.stream(arr);To convert the above stream to list, use Collectors.toList():stream.collect(Collectors.toList()The following is an example to convert Stream to List:Exampleimport java.util.Arrays; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Integer[] arr = {50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000};       Stream stream = Arrays.stream(arr);       System.out.println("Stream = "+stream.collect(Collectors.toList()));    } }OutputStream = [50, 100, 150, 200, 300, 400, 500, 600, 700, 800, 1000]

Java Program to retrieve a Stream from a List

Daniol Thomas
Updated on 30-Jul-2019 22:30:26

100 Views

Let us first create a List:List list = Arrays.asList(25, 50, 100, 200, 250, 300, 400, 500);Now, create a stream from the List:Stream stream = list.stream(); Arrays.toString(stream.toArray()));The following is an example to retrieve a Stream from a ListExampleimport java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List list = Arrays.asList(25, 50, 100, 200, 250, 300, 400, 500);       System.out.println("List elements...");       for (int res : list)       {          System.out.println(res);       }       Stream stream = list.stream();       System.out.println("Stream = "+Arrays.toString(stream.toArray()));    } }OutputList elements... 25 50 100 200 250 300 400 500 Stream = [25, 50, 100, 200, 250, 300, 400, 500]

Can we disable JComboBox arrow button in Java?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

458 Views

Yes, we can do that using removeArrow() method.The following is an example to disable JComboBox arrow button:Exampleimport java.awt.Component; import java.awt.Container; import javax.swing.*; public class SwingDemo {    public static void main(String[] args) {       String[] strValues = {"One", "Two"};       JComboBox comboBox = new JComboBox(strValues);       removeArrow(comboBox);       JOptionPane.showMessageDialog(null, comboBox);    }    private static void removeArrow(Container container) {       Component[] c = container.getComponents();       for (Component res : c) {          if (res instanceof AbstractButton) {             container.remove(res);          }       }    } }Output

How to handle action event for JComboBox in Java?

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

634 Views

The following is an example to handle action event for JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JComboBox combo = new JComboBox(new String[] { "One", "Two", "Three", "Four", "Five", "Six" });       JButton add = new JButton("Add");       add.addActionListener(new ActionListener() {          @Override          public void actionPerformed(ActionEvent e) {             ... Read More

How to add items in a JComboBox on runtime in Java

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

1K+ Views

The following is an example to add items on runtime on a JComboBox in Java:Exampleimport java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; public class SwingDemo {    public static void main(String[] args) throws Exception {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JComboBox combo = new JComboBox(new String[] { "One", "Two", "Three", "Four", "Five", "Six" });       JButton add = new JButton("Add");       add.addActionListener(new ActionListener() {          @Override          public void actionPerformed(ActionEvent e) {         ... Read More

Advertisements