Sunday, January 28, 2024

Rest API Post Example

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.URL;

public class RestApiPostExample {

public static void main(String[] args) {

try {

// Replace this URL with the actual endpoint you want to call

String apiUrl = "https://api.example.com/create";

// Create a URL object

URL url = new URL(apiUrl);

// Open a connection to the URL

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// Set the request method to POST

connection.setRequestMethod("POST");

// Enable input/output streams

connection.setDoOutput(true);

// Set the content type to JSON

connection.setRequestProperty("Content-Type", "application/json");

// Create JSON data to send in the request body

String jsonData = "{ \"key\": \"value\" }";

// Get the output stream of the connection

try (OutputStream outputStream = connection.getOutputStream()) {

// Write the JSON data to the output stream

outputStream.write(jsonData.getBytes("UTF-8"));

}

// Get the response code

int responseCode = connection.getResponseCode();

// Read the response from the API

if (responseCode == HttpURLConnection.HTTP_OK) {

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String line;

StringBuilder response = new StringBuilder();

while ((line = reader.readLine()) != null) {

response.append(line);

}

reader.close();

// Print the response

System.out.println("Response: " + response.toString());

} else {

System.out.println("API call failed. Response Code: " + responseCode);

}

// Close the connection

connection.disconnect();

} catch (Exception e) {

e.printStackTrace();

}

}

}


Rest Get Api Example

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class RestApiExample {

public static void main(String[] args) {

try {

// Replace this URL with the actual endpoint you want to call

String apiUrl = "https://api.example.com/data";

// Create a URL object

URL url = new URL(apiUrl);

// Open a connection to the URL

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// Set the request method to GET

connection.setRequestMethod("GET");

// Get the response code

int responseCode = connection.getResponseCode();

// Read the response from the API

if (responseCode == HttpURLConnection.HTTP_OK) {

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

String line;

StringBuilder response = new StringBuilder();

while ((line = reader.readLine()) != null) {

response.append(line);

}

reader.close();

// Print the response

System.out.println("Response: " + response.toString());

} else {

System.out.println("API call failed. Response Code: " + responseCode);

}

// Close the connection

connection.disconnect();

} catch (Exception e) {

e.printStackTrace();

}

}

}


Tuesday, August 8, 2023

Java Map Hierarchy

 


Saturday, May 21, 2022

Two Sum

 


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class TwoSum {

@Test
public void test() {

int[] array = new int[]{2, 7, 11, 15};
int target = 9;
int[] result = new int[]{0, 1};
Assertions.assertTrue(Arrays.equals(twoSum(array, target), result));

array = new int[]{3, 2, 4};
target = 6;
result = new int[]{1, 2};
Assertions.assertTrue(Arrays.equals(twoSum(array, target), result));

array = new int[]{3, 3};
target = 6;
result = new int[]{0, 1};
Assertions.assertTrue(Arrays.equals(twoSum(array, target), result));
}

public int[] twoSum(int[] array, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < array.length; i++) {
if (map.containsKey(array[i])) {
return new int[]{map.get(array[i]), i};
}
map.put(target - array[i], i);
}
return null;
}
}

Valid Anagram



import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Arrays;

public class ValidAnagram {

@Test
public void test() {
String s = "anagram";
String t = "nagaram";
Assertions.assertTrue(isAnagram(s, t));
Assertions.assertTrue(isAnagramWithKey(s, t));

String a = "rat";
String b = "cat";
Assertions.assertFalse(isAnagram(a, b));
Assertions.assertFalse(isAnagramWithKey(a, b));

}

public boolean isAnagram(String s, String t) {
int[] array = new int[128];
for (char c : s.toCharArray()) {
array[c]++;
}
for (char c : t.toCharArray()) {
array[c]--;
}
for (int a : array) {
if (a != 0) {
return false;
}
}
return true;
}

public boolean isAnagramWithKey(String s, String t) {
return key(s).equals(key(t));
}

public String key(String s) {
int[] array = new int[128];
for (char c : s.toCharArray()) {
array[c]++;
}
return Arrays.toString(array);
}
}