At first, convert the string into character array. Here, name is our string −char[] ch = name.toCharArray();Now, loop through and find whether the string contains only alphabets or not. Here, we are checking for not equal to a letter for every character in the string −for (char c : ch) { if(!Character.isLetter(c)) { return false; }Following is an example to check if a string contains only alphabets using RegexExample Live Demopublic class Main { public static boolean checkAlphabet(String name) { char[] ch = name.toCharArray(); for (char c : ch) { ... Read More
Let’s say our string is −String str = "Amit123";Now, using allMatch() method, get the boolean result whether the string has only alphabets or now −boolean result = str.chars().allMatch(Character::isLetter);Following is an example to check if a string contains only alphabets using Lambda Expressions −Exampleclass Main { public static void main(String[] args) { String str = "Amit123"; boolean result = str.chars().allMatch(Character::isLetter); System.out.println("String contains only alphabets? = "+result); } }OutputLet us see another example with a different input −String contains only alphabets? = falseExampleclass Main { public static void main(String[] args) ... Read More
Given with n nodes the task is to print the product of all the nodes of a singly linked list. The program must traverse all the nodes of a singly linked list starting from the initial node till the NULL is not found.ExampleInput -: 1 2 3 4 5 Output -: 120In the above example, starting from first node all the nodes are traversed i.e 1, 2 3, 4, 5, 6 and their product is 1*2*3*4*5*6 = 120Approach used below is as followsTake a temporary pointer, lets say, temp of type nodeSet this temp pointer to first node which is ... Read More
The HashMap class uses a hashtable to implement the Map interface. This allows the execution time of basic operations, such as get( ) and put( ), to remain constant even for large sets.Following is the list of constructors supported by the HashMap class.Sr.NoConstructor & Description1HashMap( )This constructor constructs a default HashMap.2HashMap(Map m)This constructor initializes the hash map by using the elements of the given Map object m.3HashMap(int capacity)This constructor initializes the capacity of the hash map to the given integer value, capacity.4HashMap(int capacity, float fillRatio)This constructor initializes both the capacity and fill ratio of the hash map by using its ... Read More
At first, create a Pair class −Pairpair = new Pair(Integer.valueOf(25), "green");Now, implement Triplet class with Pair class −Triplet triplet = pair.add("magenta");Following is an example to implement Triplet class with Pair class in Java −Exampleimport org.javatuples.Pair; import org.javatuples.Triplet; public class MyDemo { public static void main(String[] args) { Pairpair = new Pair(Integer.valueOf(25), "green"); System.out.println("Pair class = " + pair); Triplettriplet = pair.add("magenta"); System.out.println("Triplet class elements (Implemented from Pair class) = " + triplet); } }OutputPair class = [25, green] Triplet class elements (Implemented from Pair class) = [25, green, magenta]
Given with n nodes the task is to print the product of alternate node in a linked list. The program must only print the product of alternate nodes without actually changing the locations of the nodes.ExampleInput -: 10 20 30 40 50 60 Output -: 15000In the above example, starting from first node which is 10 alternate nodes are 10, 30, 50 and their product is 10*30*50 = 15000.In the above diagram, blue coloured nodes are the alternate nodes, if we start from first node and red coloured nodes are non considerable nodes.Approach used below is as followsTake a temporary ... Read More
Following is an example to implement Pair class from Unit class in Java −Exampleimport org.javatuples.Unit; import org.javatuples.Pair; public class MyDemo { public static void main(String[] args) { Unit unit = Unit.with("Tutorial"); System.out.println("Unit class element: " + unit); Pair pair = unit.addAt0("Learning"); System.out.println("Pair (Implemented from Unit Tuple): " + pair); } }OutputUnit class element: [Tutorial] Pair (Implemented from Unit): [Learning, Tutorial]Let us see another example wherein we will be implementing Pair class from Unit class −Exampleimport org.javatuples.Unit; import org.javatuples.Pair; public class MyDemo { public static void ... Read More
To sort an ArrayList, you need to use the Collections.sort() method. This sorts in ascending order, but if you want to sort the ArrayList in descending order, use the Collections.reverseOrder() method as well. This gets included as a parameter −Collections.sort(myList, Collections.reverseOrder());Following is the code to sort an ArrayList in descending order in Java −Example Live Demoimport java.util.ArrayList; import java.util.Collections; public class Demo { public static void main(String args[]) { ArrayList myList = new ArrayList(); myList.add(30); myList.add(99); myList.add(12); myList.add(23); myList.add(8); ... Read More
At first create Septet and add elements −Septet septet = new Septet( "Laptop", "Desktop", "Tablet", "Notebook", "Phone", "Reader", "LCD");Now, implement Octet from Septet −Octet octet = septet.add("LED");Following is an example to implement Octet class from Septet class in Java −Exampleimport org.javatuples.Septet; import org.javatuples.Octet; public class MyDemo { public static void main(String[] args) { Septet septet = new Septet( "Laptop", "Desktop", "Tablet", "Notebook", "Phone", "Reader", "LCD"); System.out.println("Septet elements = " + septet); Octetoctet = septet.add("LED"); System.out.println("Octet (implemented from ... Read More
Following is an example to implement Ennead class from Octet class in Java −Exampleimport org.javatuples.Octet; import org.javatuples.Ennead; public class MyDemo { public static void main(String[] args) { Octet octet = new Octet( "Jack", "Tom", "Steve", "Tim", "Nathan", "Ryan", "Kevin", "Katie"); System.out.println("Octet elements = " + octet); Enneadennead = octet.add("Scarlett"); System.out.println("Ennead (implemented from Octet): " + ennead); } }OutputOctet elements = [Jack, Tom, Steve, Tim, Nathan, Ryan, Kevin, Katie] Ennead (implemented from Octet): [Jack, Tom, Steve, Tim, Nathan, Ryan, ... Read More