Found 9150 Articles for Object Oriented Programming

How to convert string Stream to join them in Java?

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

582 Views

Let us create string Stream:Stream stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");Convert the above string stream and join them with Collectors:final String str = stream.collect(Collectors.joining(" "));The following is an example to convert string Stream to join them:Exampleimport java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");       final String str = stream.collect(Collectors.joining(" "));       System.out.println("Join result..."+str);    } }OutputJoin result... Bing Bang Theory Vampire Diaries Game of Thrones HomecomingRead More

Java program to convert stream to typed array

Nancy Den
Updated on 15-Nov-2024 18:45:03

361 Views

In this article, we will learn how to change a Java Stream into a typed array in Java. By using the toArray() method with a constructor reference, we can ensure that the array has the right type. Problem StatementGiven a Stream of strings, write a Java program to convert it into a typed array and display the elements.Input Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming")Output Array...Bing Bang TheoryVampire DiariesGame of ThronesHomecoming Steps to convert the stream to a typed array The following are the steps to covert the stream to a typed array − ... Read More

How to right align the text in a ComboBox in Java

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

444 Views

To right align the text in a JComboBox, use the following:ComponentOrientation.RIGHT_TO_LEFTThe following is an example to right align the text in a ComboBox:Exampleimport java.awt.Component; import java.awt.ComponentOrientation; import javax.swing.DefaultListCellRenderer; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JList; public class SwingDemo extends JFrame {    public SwingDemo() {       JComboBox combo = new JComboBox();       combo.setRenderer(new MyListCellRenderer());       combo.addItem("One");       combo.addItem("Two");       combo.addItem("Three");       combo.addItem("Four");       combo.addItem("Five");       getContentPane().add(combo, "North");       setSize(600, 400);       setDefaultCloseOperation(EXIT_ON_CLOSE);    }    public static void main(String[] args) { ... Read More

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

Nancy Den
Updated on 29-Sep-2024 02:47:46

847 Views

In this article, you will learn how to check if none of the strings in a list start with a specific letter using Java. This approach is useful for validating or filtering data. We will use the Stream API to evaluate the condition and return a boolean result based on whether any string starts with the given letter. Problem Statement Write a program in Java to check if none of the strings in the list matches the condition. Input pqr, stu, vwx, yza, vwxy Output No match for the starting letter as f? = true Steps to check if none of ... Read More

Java Program to convert integer to String with Map

Alshifa Hasnain
Updated on 07-Jan-2025 18:55:45

706 Views

In this article, we will learn to convert integers to String with Map in Java. The Stream API has become an essential tool for processing collections of data in a more declarative and readable way. Instead of using traditional loops, streams allow developers to filter, map, and perform other operations on data with minimal code Problem Statement Given an integer, we need to convert it to its string representation where the integer is treated as a character (e.g., 5 becomes "5", 10 becomes "10"). We'll use a Map to define the integer-to-string mappings and retrieve the string equivalent for a ... Read More

How to convert File into a Stream in Java?

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

732 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

4K+ 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

130 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

231 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

164 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]

Advertisements