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
Programming Articles - Page 2464 of 3366
651 Views
In this tutorial, we are going to find a solution to a problem. Let's see what the problem is. We have a list of strings and an element. We have to find strings from a list in which they must closely match to the given element. See the example.Inputs strings = ["Lion", "Li", "Tiger", "Tig"] element = "Lion" Ouput Lion LiWe can achieve this by using the startswith built-in method. See the steps to find the strings.Initialize string list and a string.Loop through the list.If string from list startswith element or element startswith the string from the listPrint the stringExample## initializing ... Read More
978 Views
In this tutorial, we will check whether all the elements in a list are greater than a number or not. For example, we have a list [1, 2, 3, 4, 5] and a number 0. If every value in the list is greater than the given value then, we return True else False.It's a simple program. We write it in less than 3 minutes. Try it yourself first. If you are not able to find the solution, then, follow the below steps to write the program.Initialise a list and any numberLoop through the list.If yes, return **False**Return True.Example## initializing the list values ... Read More
3K+ Views
In this tutorial, we are going to find the permutation of a string using the inbuilt function of Python called permutations. The method permutations is present in the itertools module.Procedure To Find The Permutation Of A StringImport the itertools module.Initialize the string.Use the itertools.permutations method to find the permutation of the string.In the third step, the method returns an object and convert it into a list.List contains a permutation of string as tuples.ExampleLet's see the program.## importing the module import itertools ## initializing a string string = "XYZ" ## itertools.permutations method permutaion_list = list(itertools.permutations(string)) ## printing the obj in list print("-----------Permutations Of String ... Read More
731 Views
A JList is a subclass of JComponent class and it can be used to display a list of objects that allows the user to select one or more items. A JList can generate a ListSelectiionListener interface and need to implement the abstract method valueChanged(). A DefaultListModel class provides a simple implementation of a list model, which can be used to manage items displayed by a JList control. We can add the items to a JList using the addElement() method of the DefaultListModel class, we can also add items with different fonts to JList using HTML tags like for bold style text, for italic style ... Read More
6K+ Views
Both Object level lock and Class level lock are used to achieve synchronization mechanisms in a multi-threaded application. Object Level Lock Every object in Java has a unique lock. If a thread wants to execute a synchronized method on a given object, first it has to get a lock of that object. Once thread got the lock then it is allowed to execute any synchronized method on that object. Once method execution completes automatically thread releases the lock. Acquiring and release lock internally is taken care of by the JVM. Object level lock is a mechanism when we want to synchronize a non-static ... Read More
3K+ Views
Yes! we can override the start() method of the Thread class in Java. But, if we do so, we must call it using super.start(), which will create a new thread; otherwise, no new thread will be created, and the run() method will not execute in parallel for the other threads. Overriding the start() Method In Java, the start() method is used to begin (start) the execution of a new Thread. When this method is called, it invokes the run() method, which executes in parallel with other threads. Example In the following example, we override the start() method within the subclass of ... Read More
565 Views
No, we cannot call the wait() method without acquiring the lock. In Java, once the lock has been acquired then we need to call wait() method (with timeout or without timeout) on that object. If we are trying to call the wait() method without acquiring a lock, it can throw java.lang.IllegalMonitorStateException. Example public class ThreadStateTest extends Thread { public void run() { try { wait(1000); } catch(InterruptedException ie) { ie.printStackTrace(); ... Read More
452 Views
A JPasswordField is a subclass of JTextField and each character entered in a JPasswordField can be replaced by an echo character. This allows confidential input for passwords. The important methods of JPasswordField are getPassword(), getText(), getAccessibleContext() and etc. By default, we can enter any number of digits inside JPasswordField. If we want to restrict the digits entered by a user by implementing a DocumentFilter class and need to override the replace() method.Syntaxpublic void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationExceptionExampleimport java.awt.*; import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class JPasswordFieldDigitLimitTest extends JFrame { private JPasswordField passwordField; private JPanel ... Read More
2K+ Views
No, a constructor cannot be synchronized in Java. If you try to use the synchronized keyword before a constructor, it may throw a compile-time error in the IDE. Synchronization is a mechanism used to control the access of multiple threads to any shared resource. When multiple threads access any shared resources, the synchronization prevents data corruption by coordinating access, avoiding race conditions (i.e., occurs when multiple threads concurrently access and modify shared data), and ensuring that operations are performed atomically. The JVM ensures that only one thread can invoke a constructor call at a given point in time. That is ... Read More
16K+ Views
The threads can communicate with each other through wait(), notify(), and notifyAll() methods in Java. These are the final methods defined in the Object class and can be called only from within a synchronized context. The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. These methods will throw an IllegalMonitorStateException if the current thread is not the owner of the object's monitor. The wait() Method In Java, the wait() method of the Object class allows threads to pause their execution and wait for a specific condition to be ... Read More