- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Krantik Chavan has Published 308 Articles
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
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.*; ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
568 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 { ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
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 ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
488 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 ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
662 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 ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
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 ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
335 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 in the end with nullsLast:list.sort(Comparator.nullsLast(String::compareTo));The following is an example to sort a list placing nulls ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
144 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 ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
603 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() ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
Krantik Chavan
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 ... Read More