Found 4336 Articles for Java 8

Sort a list that places nulls first in Java

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

488 Views

Let us create a list first with string elements. Some of the elements are null in the List:List list = Arrays.asList("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk");Now, sort the above list and place nulls first with nullsFirst:list.sort(Comparator.nullsFirst(String::compareTo));The following is an example to sort a List that places nulls first:Exampleimport java.util.Arrays; import java.util.Comparator; import java.util.List; public class Demo {    public static void main(String... args) {       List list = Arrays.asList("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk");       System.out.println("Initial List = "+list);       list.sort(Comparator.nullsFirst(String::compareTo));       System.out.println("List placing nulls first = "+list); ... Read More

Can we sort a list with Lambda in Java?

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

663 Views

Yes, we can sort a list with Lambda. Let us first create a String List:List list = Arrays.asList("LCD", "Laptop", "Mobile", "Device", "LED", "Tablet");Now, sort using Lambda, wherein we will be using compareTo():Collections.sort(list, (String str1, String str2) -> str2.compareTo(str1));The following is an example to sort a list with Lambda in Java:Exampleimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String... args) {         List list = Arrays.asList("LCD", "Laptop", "Mobile", "Device", "LED", "Tablet");       System.out.println("List = "+list);       Collections.sort(list, (String str1, String str2) -> str2.compareTo(str1));       System.out.println("Sorted List ... Read More

Map to get substring and convert to int in Java

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

491 Views

Let’s say the following is our stream:Stream.of("u2", "h9", "s8", "l3")Now, map to get the substring:.map(s -> s.substring(1))Convert to int and find the minimum:.mapToInt(Integer::parseInt) .min()The following is an example to Map and get substring and convert to int:Exampleimport java.util.stream.Stream; public class Demo {    public static void main(String[] args) throws Exception {    Stream.of("u2", "h9", "s8", "l3")       .map(s -> s.substring(1))       .mapToInt(Integer::parseInt)       .min()       .ifPresent(System.out::println);    } }Output2

How to add JList to Scroll pane in Java?

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

1K+ Views

To add JList to Scroll pane in Java, use JScrollPane:JList list = new JList(sports); JScrollPane scrollPane = new JScrollPane(list);After that set it to Container:Container contentPane = frame.getContentPane(); contentPane.add(scrollPane, BorderLayout.CENTER);The following is an example to add JList to Scroll pane: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

How to store string array in Java JList?

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

569 Views

To store string array in JList, at first create String array list:String sports[]= { "Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"};Now, set it to JList:JList list = new JList(sports);The following is an example to store string array 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[]= {"Football", "Fencing", "Cricket", "Squash", "Hockey", "Rugby"}; ... Read More

Java Program to set Selection Mode for JList only for single selection

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

268 Views

To set single selection for JList, use DefaultListSelectionModel and set it to SINGLE_SELECTION:String values[]= { "One", "Two", "Three", "Four", "Five", "Six"}; JList list = new JList(values); list.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);The following is an example to set the selection mode for JList only for single selection: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 values[]= { ... Read More

Java Program to select the first item in JList

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

300 Views

To select the first item in JList, use setSelectionInterval() method:String values[]= { "One", "Two", "Three", "Four", "Five", "Six"}; JList list = new JList(values); int begn = 0; int end = 0; list.setSelectionInterval(begn, end);The following is an example to select the first item 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 values[]= ... Read More

Java Program to select all the items in a JList

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

507 Views

To select all the items in a JList, use setSelectionInterval() and set a range:JList list = new JList(sports); int begn = 0; int end = list.getModel().getSize() - 1; if (end >= 0) {    list.setSelectionInterval(begn, end); }The following is an example to select all the items in a 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(); ... Read More

Map to create new value from int array in Java

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

410 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

How to display row count in Java Swing JList

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

380 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

Advertisements