Java Articles

Page 24 of 450

Iterator vs Collection in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 525 Views

IteratorIt is used in Collection Framework so as to retrieve elements as and when they are required.public interface IteratorIt can be used with the ‘next’ function to move and access the next elements. The ‘remove’ function can be used to remove an element from the data structure.It is quicker in comparison to Collections, since the number of operations associated with Iterator is less.Below is an example of an iterator working with a list −Examplemport java.io.*; import java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_list = new ArrayList();       my_list.add("Its");     ...

Read More

Java Concurrency – sleep() method

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 484 Views

The sleep functionThis sleep function is used to ensure that the currently executing thread goes to sleep for a specific amount of milliseconds which is passed as a parameter to the function. The thread stops executing for that number of milliseconds.Let us see an exampleExampleimport java.lang.*; public class Demo implements Runnable{    Thread my_t;    public void run(){       for (int i = 0; i < 3; i++){          System.out.println(Thread.currentThread().getName()+ " " + i);          try{             Thread.sleep(100);          }         ...

Read More

Java Lambda Expression with Collections

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 986 Views

Sorting the elements of a list using lambda expression −Exampleimport java.util.*; public class Demo{    public static void main(String[] args){       ArrayList my_arr = new ArrayList();       my_arr.add(190);       my_arr.add(267);       my_arr.add(12);       my_arr.add(0);       System.out.println("Before sorting, elements in the array list are : " + my_arr);       Collections.sort(my_arr, (o1, o2) -> (o1 > o2) ? -1 : (o1 < o2) ? 1 : 0);       System.out.println("After sorting, elements in the array list are : " + my_arr);    } }OutputBefore sorting, elements in ...

Read More

Java Numeric Promotion in Conditional Expression

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 218 Views

The conditional operator (? :) leverages the output of one value (which is a bool) to decide which expression has to be evaluated next. Let us see an example −Exampleimport java.io.*; public class Demo{    public static void main (String[] args){       Object my_obj = true ? new Integer(91) : new Float(89);       System.out.println(my_obj);    } }Output91.0A class named Demo contains the main function. Here, an object instance is defined and if it is true, an integer value is displayed otherwise a float value is displayed. Next, they are printed on the console.When promotional expression is ...

Read More

Java Program for credit card number validation

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 6K+ Views

Given a long number containing digits of a credit card number; the task is to find whether the credit card number is valid or not with a program.For checking a credit card is valid or not, the following are the validations we have to be sure for declaring the result.A credit card’s number must have 13 to 16 digits, it must start with the following digits.All the visa cards start from 4 All the master cards start from 537 is the starting for American express cardsAll the discover cards start from 6Steps to check whether the credit card is valid or ...

Read More

Custom UnaryOperator implementation in java.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 514 Views

The java.util.function.UnaryOperator interface and can be used as assignment target for lambda expressions, it represents operation on a single operand whose result will be of same type as the input. We can create our own UnaryOperator by implementing this interface.The replaceAll() method of the List interface accept an object of the UnaryOperator representing a particular operation performs the specified operation on all the elements of the current list and replaces the existing values with the resultant values.In the following example we are implementing the UnaryOperator interface and creating a custom unary operator object and trying to pass it as an ...

Read More

Regular expression for a hexadecimal number greater than 10 and should be even in length in java.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 441 Views

Following is the regular expression to match hexadecimal number greater than 10 with even length −^(?=.{10,255}$)(?:0x)?\p{XDigit}{2}(?:\p{XDigit}{2})*$Where,^ − Matches the starting of the sentence.(?=.{10,255}$) − String ending with characters with 10 to 255.\p{XDigit}{2} − Two hexa-decimal characters.(?:\p{XDigit}{2})* − 0 or more sequences of double hexa-decimal characters.$ − Matches the end of the sentence.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class JavaExample51 {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       String nums[] = new String[5];       for(int i=0; i

Read More

Regular Expression E Metacharacter in Java.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 649 Views

The subexpression/metacharacter “\E” ends the quoting begun with \Q. i.e. you can escape metacharacters in the regular expressions by placing them in between \Q and \E. For example, the expression [aeiou] matches the strings with vowel letters in it.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleProgram {    public static void main( String args[] ) {       String regex = "[aeiou]";       Scanner sc = new Scanner(System.in);       System.out.println("Enter input string: ");       String input = sc.nextLine();       //Creating a Pattern object       Pattern pattern = Pattern.compile(regex); ...

Read More

List.replaceAll(UnaryOperator operator) method in Java

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

The replaceAll() method of the List interface accept an object of the UnaryOperator representing a particular operation, performs the specified operation on all the elements of the current list and replaces the existing values in the list with their respective results.Exampleimport java.util.ArrayList; import java.util.function.UnaryOperator; class Op implements UnaryOperator {    public String apply(String str) {       return str.toUpperCase();    } } public class Test {    public static void main(String[] args) throws CloneNotSupportedException {       ArrayList list = new ArrayList();       list.add("Java");       list.add("JavaScript");       list.add("CoffeeScript");       list.add("HBase"); ...

Read More

Java regular expression program to validate an email including blank field valid as well

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 347 Views

Following regular expression matches given e-mail id including the blank input −^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2, 6})?$Where, ^ matches the starting of the sentence.[a-zA-Z0-9._%+-] matches one character from English alphabet (both cases), digits, "+", "_", ".", "" and, "-" before the @ symbol.+ indicates the repetition of the above mentioned set of characters one or more times.@ matches itself[a-zA-Z0-9.-] matches one character from English alphabet (both cases), digits, "." and "-" after the @ symbol\.[a-zA-Z]{2, 6} two to 6 letter for email domain after "."$ indicates the end of the sentenceExample 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleTest {    public static ...

Read More
Showing 231–240 of 4,498 articles
« Prev 1 22 23 24 25 26 450 Next »
Advertisements