Author: alegru

import org.apache.http.HttpResponse; import org.apache.http.HttpHeaders; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpUriRequest request = RequestBuilder.get() .setUri(“https://jsonplaceholder.typicode.com/todos/1”) // adding headers .setHeader(HttpHeaders.CONTENT_TYPE, “application/json”) .setHeader(“Authorization”, “Bearer 123token”) .build(); System.out.println(“Executing GET request…”); HttpResponse response = httpclient.execute(request);…

Read More Add a Custom HTTP Header with the HttpClient

<dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.5.13</version> </dependency> import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpDelete; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpDelete httpget = new HttpDelete(“https://jsonplaceholder.typicode.com/todos/1”); System.out.println(“Executing DELETE request…”); HttpResponse response = httpclient.execute(httpget); System.out.println(“Status code: ” + response.getStatusLine().getStatusCode()); String…

Read More How to Execute an HTTP DELETE Request in Java (Solved!)

<dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.5.13</version> </dependency> import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPut; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPut httpPut = new HttpPut(“https://jsonplaceholder.typicode.com/posts/1”); // specify the PUT body to send to the server as part of…

Read More How to Execute an HTTP PUT Request in Java

<dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.5.13</version> </dependency> import org.apache.http.HttpResponse; import org.apache.http.entity.StringEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(“https://jsonplaceholder.typicode.com/posts”); // specify the POST body to send to the server as part of…

Read More Execute an HTTP POST Request in Java

<dependency> <groupid>org.apache.httpcomponents</groupid> <artifactid>httpclient</artifactid> <version>4.5.13</version> </dependency> import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import java.io.IOException; public class Test { public static void main(String[] args) throws IOException { try (CloseableHttpClient httpclient = HttpClients.createDefault()) { HttpGet httpget = new HttpGet(“https://jsonplaceholder.typicode.com/todos/1”); System.out.println(“Executing GET request…”); HttpResponse response = httpclient.execute(httpget); String responseBody = new BasicResponseHandler().handleResponse(response); System.out.println(“Response: ” +…

Read More Execute HTTP GET Request in Java

public class Test { public static void main(String[] args) { Integer[] array = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // reverse an array Integer[] reversedArray = reverse(array); // print the elements Arrays.stream(reversedArray).forEach(item -> System.out.print(item + ” “)); } public static Integer[] reverse(Integer[] array) { List arrayList = Arrays.asList(array); Collections.reverse(arrayList); return…

Read More Reverse an Array in Java

import java.util.*; public class Test { public static void main(String[] args) { List<String> names = new ArrayList<>(Arrays.asList(“Megan”, “Tom”, “Steve”, “Megan”, “Meliisa”, “Tom”, “John”)); Set<String> uniqueNames = new HashSet<>(names); // creating a Set from the List System.out.println(uniqueNames); } } Output: [Megan, Tom, Steve, John, Meliisa] Remove duplicates from a List in Java using the Java 8…

Read More Remove Duplicates from a List in Java

import java.util.*; public class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); System.out.println(numbers.stream().mapToInt(Integer::intValue).average().getAsDouble()); } } Output: 5.5   Here, we used the mapToInt(Integer::intValue) to convert the Stream<Integer> which is the result of the stream() method, to IntStream. import java.util.*; public class…

Read More Find the Average of Numbers in Java

public class Test { public static void main(String[] args) { int[] array = new int[]{7, 1, 9, 2, 5}; Arrays.sort(array); Arrays.stream(array).forEach(System.out::print); } } Output: 1 2 5 7 9   Note: values of type int will be automatically converted to the corresponding Wrapper class. And since the Integer Wrapper class implements Comparable, we can sort…

Read More Sort an Array in Java

import java.util.*; public class Test { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(Arrays.asList(7, 12, 5, 28, 97, 14, 2, 7, 8, 12, 19, 22)); Collections.sort(numbers); System.out.println(numbers); } } Output: [2, 5, 7, 7, 8, 12, 12, 14, 19, 22, 28, 97] Using the sort() method from ArrayList class public class Test…

Read More Sort a List in Java

class Test { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, “Java”); map.put(7, “Python”); map.put(10, “Ruby”); map.put(3, “Kotlin”); for (Map.Entry<Integer, String> entry : map.entrySet()) { System.out.println(“Key = ” + entry.getKey() + “, Value = ” + entry.getValue()); } } } Output: Key = 1, Value = Java Key = 3,…

Read More Iterate over a Map in Java

public class Test { public static void main(String[] args) { int num = 8; int num2 = 9; if(num % 2 == 0) { System.out.println(“num is an even number!”); } else { System.out.println(“num is odd number!”); } if(num2 % 2 == 0) { System.out.println(“num2 is an even number!”); } else { System.out.println(“num2 is odd number!”);…

Read More Check if the Number is Even or Odd in Java