Using Java To find the number of occurrences of each character in a given string, we have used HashMap with character as a key and it’s occurrences as a value. First, we convert the given string to char array and check each character one by one. And update it’s count in HashMap.
Continue reading How To Count Occurrences Of Each Character In StringHow To Count Occurrences Of Each Character In String
Using Java
To find the number of occurrences of each character in a given string, we have used HashMap with character as a key and it’s occurrences as a value. First, we convert the given string to char array and check each character one by one. And update it’s count in HashMap.
import java.util.HashMap;
public class EachCharCountInString
{
private static void characterCount(String inputString)
{
//Creating a HashMap containing char as a key and occurrences as a value
HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
//Converting given string to char array
char[] strArray = inputString.toCharArray();
//checking each char of strArray
for (char c : strArray)
{
if(charCountMap.containsKey(c))
{
//If char 'c' is present in charCountMap, incrementing it's count by 1
charCountMap.put(c, charCountMap.get(c)+1);
}
else
{
//If char 'c' is not present in charCountMap,
//putting 'c' into charCountMap with 1 as it's value
charCountMap.put(c, 1);
}
}
//Printing inputString and charCountMap
System.out.println(inputString+" : "+charCountMap);
}
public static void main(String[] args)
{
characterCount("Java J2EE Java JSP J2EE");
characterCount("All Is Well");
characterCount("Done And Gone");
}
}
Output :
Java J2EE Java JSP J2EE : { =4, P=1, a=4, 2=2, S=1, E=4, v=2, J=5}
All Is Well : { =2, A=1, s=1, e=1, W=1, I=1, l=4}
Done And Gone : { =2, A=1, D=1, d=1, e=2, G=1, n=3, o=2}
Using Java In this method, we use HashSet to remove duplicate elements from an ArrayList. As you know, HashSet doesn’t allow duplicate elements. We use this property of HashSet to remove duplicate elements from already constructed ArrayList. But, there is one disadvantage of this method. That is, it erases the insertion order of ArrayList elements. That means, …
Continue reading How To Remove Duplicate Elements From ArrayListUsing Java Split the given inputString into words using split() method. Then take each individual word, reverse it and append to reverseString. Finally print reverseString. Below image shows code snippet of the same.
Continue reading How To Reverse Each Word Of A Stringport is a communication endpoint where a service is available. For example if we install Jenkins on a server or local , we start it at a specific port and using that port we access Jenkins. So if you have some services running on a specific port, you need to pass that in URI so …
Continue reading Default Host And Port In Rest AssuredWhy RequestSpecification? Observe the below lines of codes carefully. You will find that we have some common statements in both @Test methods after given() method call. Above we have only two tests but in real-time you may have many. A group of tests will have some common specifications to create a request. Following the DRY …
Continue reading RequestSpecification – How The Request Will Look LikeWe need to pass booking id which you want to delete in URI. E.g. https://restful-booker.herokuapp.com/booking/1 where “1” is booking id. Authentication token need to pass as cookie. Cookie name is “token” and value is generated auth token. ‘Cookie: token=<generatedToken>’. You don’t required to pass body to DELETE request. Let’s see existing details of a Booking …
Continue reading Write First DELETE Request In REST AssuredWe need to pass booking id for which you want to update details in URI. E.g. https://restful-booker.herokuapp.com/booking/1 where “1” is booking id. Authentication token need to pass as cookie. Cookie name is “token” and value is generated auth token. ‘Cookie: token=<generatedToken>’. Pass the body with all details. Remember it is a PUT request which takes …
Continue reading Write First PUT Request In REST AssuredPassing body to POST request:- We know that we need to pass a payload ( JSON or XML) to POST request. REST Assured makes it very handy to pass body to request. We can pass body as a String or a JSON file or a XML file or a Java Object or a byte array. …
Continue reading Write First POST Request In REST AssuredRestAssured is a class which consists many static fields and methods.It supports POST, GET, PUT, DELETE, HEAD, PATCH and OPTIONS requests and to verify the response of these requests. RestAssured has a static overloaded method named get() which returns a reference to the Response interface. In fact return type of all http methods in RestAssured …
Continue reading Write First GET REST Assured TestAdd Maven dependency of REST Assured in pom.xml Add Maven dependency of JSON Schema Validator in pom.xml ( Needed for JSON schema validator) Add Maven dependency of Jackson JSON Java parser ( Needed for mapping Java objects to and from JSON ) Using Eclipse IDE Using IntelliJ
Continue reading Setup a Basic REST Assured Maven Project