Java Articles

Page 374 of 450

How to transform List string to upper case in Java?

Daniol Thomas
Daniol Thomas
Updated on 30-Jul-2019 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

How to convert File into a Stream in Java?

Nancy Den
Nancy Den
Updated on 30-Jul-2019 801 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 right align the text in a ComboBox in Java

Nancy Den
Nancy Den
Updated on 30-Jul-2019 537 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

How to convert string Stream to join them in Java?

Nancy Den
Nancy Den
Updated on 30-Jul-2019 640 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 Homecoming

Read More

How to convert Stream to TreeSet in Java?

Nancy Den
Nancy Den
Updated on 30-Jul-2019 1K+ Views

Let us first create a Stream:Stream stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland");Now convert Stream to TreeSet:Set set = stream.collect(Collectors.toCollection(TreeSet::new));The following is an example to convert String to TreeSet in Java:Exampleimport java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("UK", "US", "India", "Australia", "Armenia", "Canada", "Poland");       Set set = stream.collect(Collectors.toCollection(TreeSet::new));       set.forEach(val -> System.out.println(val));    } }OutputArmenia Australia Canada India Poland UK US

Read More

How to count element after filtering in Java?

Nancy Den
Nancy Den
Updated on 30-Jul-2019 567 Views

Let’s say the following is the String List:List list = new ArrayList(); list.add("Tom"); list.add("John"); list.add("David"); list.add("Paul"); list.add("Gayle"); list.add("Narine"); list.add("Joseph");Now, let’s say you need to filter elements beginning with a specific letter. For that, use filter() and startsWith():long res = list    .stream()    .filter((s) -> s.startsWith("J"))    .count();We have also counted the elements above after filtering using count().The following is an example to count element after filtering in Java:Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(final String[] args) {       List list = new ArrayList();       list.add("Tom");       list.add("John"); ...

Read More

How to filter empty string values from a Java List?

Nancy Den
Nancy Den
Updated on 30-Jul-2019 1K+ Views

Let’s say we have a String List with an empty value. Here, we have empty array elements before Football and after Squash:List sports = Arrays.asList("", "Football", "Cricket", "Tennis", "Squash", "", "Fencing", "Rugby");Now filter the empty string values. At first, we have used Predicate to negate values:Stream stream = sports.stream(); Predicate empty = String::isEmpty; Predicate emptyRev = empty.negate(); stream.filter(emptyRev).collect(Collectors.toList()));The following is an example to filter empty string values from a List:Exampleimport java.util.Arrays; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List sports = Arrays.asList("", "Football", "Cricket", "Tennis", ...

Read More

How to filter non-null value in Java?

Nancy Den
Nancy Den
Updated on 30-Jul-2019 2K+ Views

Let’s say the following is our List with string elements:List leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL", "NBA", "NFL");Now, create a stream and filter elements that end with a specific letter:Stream stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));Now, use Objects::nonnull for non-null values:List list = stream.filter(Objects::nonNull).collect(Collectors.toList());The following is an example to filter non-null value in Java:Exampleimport java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       List leagues = Arrays.asList("BBL", "IPL", "MLB", "FPL", "NBA", "NFL");       Stream stream = leagues.stream().filter(leagueName -> leagueName.endsWith("L"));       List list = ...

Read More

How can I check if there are any selected items in Java JList

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 839 Views

To check if there are any selected items, use the following:boolean res = !list.isSelectionEmpty();The value of res would be TRUE IF we have a selected item in the JList.The following is an example to check if there are any selected items in JList:Examplepackage my; import java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       SwingDemo s = new SwingDemo();       JPanel panel = new JPanel();       String sports[]= {"Squash", ...

Read More

How to display row count in Java Swing JList

Krantik Chavan
Krantik Chavan
Updated on 30-Jul-2019 639 Views

Use JList getVisibleRowCount() method to display the row count in a JList:String sports[]= {"Tennis", "Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"}; Jlist list = new JList(sports); int rowCount = list.getVisibleRowCount(); System.out.println("Row Count = "+rowCount);The following is an example to display row count in JList:Exampleimport java.awt.event.*; import java.awt.*; import javax.swing.*; class SwingDemo extends JFrame {    static JFrame frame;    static JList list;    public static void main(String[] args) {       frame = new JFrame("JList Demo");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       String sports[]= {"Tennis", Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"};       list = ...

Read More
Showing 3731–3740 of 4,498 articles
« Prev 1 372 373 374 375 376 450 Next »
Advertisements