
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 7442 Articles for Java

1K+ Views
To change display mode with Java Swings, use the setDisplayMode() method. Here, we have set the Display mode as:new DisplayMode(800, 600, 32, 60));Now, when you will run the program, the frame would be visible in a different resolution than the actual set resolution of your system.The following is an example to change display mode with Java Swings:Exampleimport java.awt.DisplayMode; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(800, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GraphicsDevice graphics = GraphicsEnvironment.getLocalGraphicsEnvironment() ... Read More

636 Views
In this article, we will learn how to sort a stream of strings using a reversed comparator in Java. Java 8 introduced the Stream API, which allows powerful operations like sorting using custom comparators. Java Comparator A Comparator is a functional interface in Java that defines custom sorting logic. It compares two objects and returns a result based on the comparison. Java Stream A Stream is a sequence of elements that can be processed in parallel or sequentially, supporting methods like sorting, filtering, and mapping. Sorting string stream with a reversed comparator The following are the steps for sorting a ... Read More

2K+ Views
Here, we are sorting string array alphabetically by the initial character i.e. ‘J’ for ‘John’ will come after ‘Chris’ since the first character of ‘Chris’ is ‘C’.Let us first create a String array:String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" };Now, sort the string array based on the first character:Arrays.sort(strArr, (str1, str2) -> str1.charAt(0) - str2.charAt(0));The following is an example to sort String Array alphabetically by the initial character only:Exampleimport java.util.Arrays; public class Demo { public static void main(String[] args) { String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" }; ... Read More

946 Views
At first, let us create and array of strings:String[] strArr = { "ABCD", "AB", "ABCDEFG", "ABC", "A", "ABCDE", "ABCDEF", "ABCDEFGHIJ" };Now, for shortest to longest pattern, for example A, AB, ABC, ABCD, etc.; get the length of both the string arrays and work them like this:Arrays.sort(strArr, (str1, str2) -> str1.length() - str2.length());The following is an example to sort array of strings by their lengths with shortest to longest pattern:Exampleimport java.util.Arrays; public class Demo { public static void main(String[] args) { String[] strArr = { "ABCD", "AB", "ABCDEFG", "ABC", "A", "ABCDE", "ABCDEF", "ABCDEFGHIJ" }; ... Read More

244 Views
Following is our integer array:Integer[] arr = {20, 50, 100, 150, 200, 250, 300, 350, 400, 500};Now convert the above Integer array to List:List list = new ArrayList(Arrays.asList(arr));Now, to sort the above Integer list in reversed order:Comparator initialComp = Integer::compare; Comparator revComp = initialComp.reversed(); Collections.sort(list, revComp);The following is an example to sort Integer list in reversed order:Exampleimport java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo { public static void main(String[] args) { Integer[] arr = {20, 50, 100, 150, 200, 250, 300, 350, 400, 500}; List list = new ... Read More

719 Views
In this article, we will learn how to sort lists containing null values in Java and ensure these nulls are kept at the bottom while upper elements remain in order. This can be done using Comparator.nullsLast, which sorts the non-null elements and places all null elements at last. Problem Statement A list of strings with some string values and nulls is given. Write a Java program to sort a list placing nulls in the end. Input Initial List = ("Jack", null, "Thor", null, "Loki", "Peter", null, "Hulk") Output Initial List = [Jack, null, Thor, null, Loki, Peter, null, Hulk] List ... Read More

576 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

806 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

638 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

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