Nancy Den has Published 330 Articles

How to right align the text in a ComboBox in Java

Nancy Den

Nancy Den

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

300 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 = ... Read More

How to set tooltip text for JCheckBox in Java?

Nancy Den

Nancy Den

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

254 Views

For JCheckBox, use the following to set tooltip text:checkBox1.setToolTipText("Sports Football"); checkBox2.setToolTipText("Sports Tennis");Tooltip text is visible whenever you will place cursor on that particular text.The following is an example. Here, we have set the tooltip text for both the sports:Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingDemo {    private ... Read More

How to display the first element in a JComboBox in Java

Nancy Den

Nancy Den

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

565 Views

To display the first element in a JComboBox, use the getSelectedIndex():comboBox.setSelectedIndex(0);The following is an example to display the first element in 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; import javax.swing.JPanel; import javax.swing.JTextArea; public class SwingDemo {    public static void main(String[] args) ... Read More

Java Program to convert Stream to typed array

Nancy Den

Nancy Den

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

207 Views

Let us first create a Stream:Stream stream = Stream.of("Bing Bang Theory", "Vampire Diaries", "Game of Thrones", "Homecoming");Now convert the above stream to typed array:final String[] strArr = stream.toArray(String[]::new);The following is an example to convert Stream to typed array in Java:Exampleimport java.util.Arrays; import java.util.stream.Stream; public class Demo {    public static ... Read More

How to convert string Stream to join them in Java?

Nancy Den

Nancy Den

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

411 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 ... Read More

How to convert Stream to TreeSet in Java?

Nancy Den

Nancy Den

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

762 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) ... Read More

How to count element after filtering in Java?

Nancy Den

Nancy Den

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

357 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 ... Read More

How to filter empty string values from a Java List?

Nancy Den

Nancy Den

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

752 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 ... Read More

How to filter non-null value in Java?

Nancy Den

Nancy Den

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

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 ... Read More

How to query MongoDB with “like”?

Nancy Den

Nancy Den

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

2K+ Views

You can easily query MongoDB with “like”:db.yourCollectionName.find({"yourFieldName" : /.*yourMatchingValue.*/}).pretty();To understand the above syntax, let us create a collection with some documents. Here, we have a collection with the name ‘employee’. The query is as follows:> db.employee.insert({"EmployeeName":"John", "EmployeeSalary":450000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Carol", "EmployeeSalary":150000}); WriteResult({ "nInserted" : 1 }) ... Read More

Advertisements