Skip to content

Instantly share code, notes, and snippets.

View h-hub's full-sized avatar
🤖
implementing something

Harsha Jayamanna h-hub

🤖
implementing something
View GitHub Profile
static void clientCode2() {
Item user = findItem2("99").get();
}
static Optional<Item> findItem2(String id) {
return Arrays.stream(users)
.filter(item -> item.id.equals(id))
.findFirst();
}
@h-hub
h-hub / oneliner.java
Last active September 17, 2023 09:07
Arrays.stream(new int[]{1,2,3,4}).anyMatch(num -> num == 3)
static boolean isElementExistProper(int[] ints, int b){
int i=0;
while (true){
if(ints.length==i) return false;
if(ints[i++]==b) return true;
}
}
//Please do not use this
static boolean isElementExistUgly(int[] ints, int b){
try{
int i=0;
while (true){
if(ints[i++]==b) return true;
}
} catch(ArrayIndexOutOfBoundsException ex){
return false;
}
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MyRequestContext {
private Jwt jwt;
public MyRequestContext(){
this.jwt = (Jwt) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
public class CallableExample {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(10);
List<Future<String>> list = new ArrayList<>();
Callable<String> callable = new HelloCallable();
@h-hub
h-hub / main.java
Last active November 2, 2021 05:21
main.java
public class Main {
public static void main(String[] args) {
Thread thread1 = new Thread(new HelloRunnable());
thread1.start();
Thread thread2 = new Thread(new HelloThread());
thread2.start();
}
@h-hub
h-hub / ItrExample.java
Created September 10, 2021 02:18
Iterator example
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class ItrExample {
public static void main(String[] args) {
List<String> list2 = List.of("item11","item22","item33"); //create a list
@h-hub
h-hub / ImageService.java
Last active October 18, 2020 05:00
ImageService.java
public String uploadImage(MultipartFile image, String bucketName) {
File imageFile;
try {
imageFile = convertMultiPartToFile(image);
} catch (IOException e) {
throw new FileUploadException("Unable to convert input stream to file.");
}
@h-hub
h-hub / FileUploadController.java
Last active October 18, 2020 04:57
FileUploadController.java
@PreAuthorize("hasRole('ROLE_USER')")
@RequestMapping(value = "/image/upload", method = RequestMethod.POST, produces = { "application/json",
"application/xml" })
@ResponseBody
@ResponseStatus(code = HttpStatus.CREATED)
public ImageDto uploadImage(@RequestParam(value = "imageFile", required = true) @ValidateImg MultipartFile image) {
String bucketName = "public-bucket-for-files";
String imageUrl = imageService.uploadImage(image, bucketName);