
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9150 Articles for Object Oriented Programming

806 Views
In this article, we will learn how to select all the items in a JList in Java. The program creates a simple graphical user interface with a list of sports. It uses the setSelectionInterval() method to select all items in the list. This ensures that from the first item to the last item in the list, everything is selected when the program runs. Problem Statement Write a Java program to select all the items in a JList. Below is the demonstration of the same − Input sports[]= {"Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"} Output Steps to select all ... Read More

564 Views
Let’s say the following is our int array elements:10, 50, 100, 200, 250, 300, 400, 500Here, we are mapping and creating a new value by incrementing each int element with 1:Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1)Now find the average:Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1).average()The following is an example to Map and create new value from int array:Exampleimport java.util.Arrays; public class Demo { public static void main(String[] args) throws Exception { Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1).average() .ifPresent(System.out::println); } }Output227.25

546 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

511 Views
To always display the scroll bar in a JList, use the properties HORIZONTAL_SCROLLBAR_ALWAYS and VERTICAL_SCROLLBAR_ALWAYS:JList list = new JList(sports); JScrollPane scrollPane = new JScrollPane(list); scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);The following is an example to create JList and display the scroll bar: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"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String sports[]= {"Tennis", "Archery", "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"}; list = new JList(sports); ... Read More

153 Views
To check if the second item is selected i.e. index 1, use the method isSelectedIndex():list.isSelectedIndex(1);Above, we have set the list with string values:String sports[]= { "Squash", "Fencing", "Cricket", "Football", "Hockey", "Rugby"}; JList list = new JList(sports);The following is an example to check if the second item is selected 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 = ... Read More

757 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

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

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

516 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

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 USRead More