Skip to content

Latest commit

 

History

History
441 lines (381 loc) · 8.68 KB

java_snippets.md

File metadata and controls

441 lines (381 loc) · 8.68 KB

Java

Hello World

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Variables

String name = "John";
int age = 30;

Conditional Statements

if (age >= 18) {
    System.out.println("You are an adult.");
} else {
    System.out.println("You are a minor.");
}

For loop

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

While loop

int count = 0;
while (count < 10) {
    System.out.println(count);
    count++;
}

Arrays

int[] myArray = {1, 2, 3, 4, 5};

Array access

int firstElement = myArray[0];

List (ArrayList)

import java.util.ArrayList;
ArrayList<String> names = new ArrayList<>();

List (ArrayList) Operations

names.add("Alice");
names.remove("Bob");

Functions (Methods)

public static int add(int a, int b) {
    return a + b;
}

File reading(Java 7+)

import java.nio.file.*;
String content = Files.readString(Paths.get("file.txt"));

File writing(Java 7+)

import java.nio.file.*;
Files.write(Paths.get("output.txt"), "Hello, World!".getBytes());

Exeption Handling

try {
    int result = 10 / 0;
} catch (ArithmeticException e) {
    System.out.println("Error: Division by zero");
}

Classes and Objects

public class Person {
    String name;
    int age;
}

Object creation

Person person = new Person();
person.name = "Alice";
person.age = 25;

Object methods

public void greet() {
    System.out.println("Hello, my name is " + name);
}

Inheritance

public class Student extends Person {
    String school;
}

Interfaces

public interface Drawable {
    void draw();
}

Lambda Expressions (Java 8+)

Runnable runnable = () -> {
    System.out.println("Running a task");
};

Sorting arrays

import java.util.Arrays;
int[] sortedNumbers = Arrays.copyOf(numbers, numbers.length);
Arrays.sort(sortedNumbers);

String Manipulation

String text = "Hello, World!";
String upperText = text.toUpperCase();

List reversal

Collections.reverse(numbersList);

Finding Maximum

int max = Collections.max(numbersList);

Finding Minimum

int min = Collections.min(numbersList);

List Length

int listSize = numbersList.size();

List removal

numbersList.remove(3);

List Appending

numbersList.add(6);

List copying

ArrayList<Integer> copiedList = new ArrayList<>(numbersList);

List clearing

numbersList.clear();

String Splitting

String[] words = text.split(", ");

String Joining

String joinedText = String.join(", ", words);

String Formatting

String formattedText = String.format("My name is %s and I am %d years old.", name, age);

Math Operations

int absoluteValue = Math.abs(-5);

Random number generation

import java.util.Random;
Random random = new Random();
int randomNumber = random.nextInt(100);

Date and Time (Java 8+)

import java.time.LocalDateTime;
LocalDateTime currentTime = LocalDateTime.now();

Set (HashSet)

import java.util.HashSet;
HashSet<String> uniqueNames = new HashSet<>();

Set operations

uniqueNames.add("Alice");
uniqueNames.remove("Bob");

Map (HashMap)

import java.util.HashMap;
HashMap<String, Integer> scoreMap = new HashMap<>();

Map operations

scoreMap.put("Alice", 95);
int aliceScore = scoreMap.get("Alice");

List Comprehension with Streams (Java 8+)

List<Integer> evenNumbers = numbersList.stream()
    .filter(n -> n % 2 == 0)
    .collect(Collectors.toList());

Multithreading with Thread class

Thread thread1 = new Thread(() -> {
    for (int i = 0; i < 5; i++) {
        System.out.println("Thread 1: " + i);
    }
});
thread1.start();

Multithreading with ExecutorService

ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(() -> System.out.println("Task 1"));
executorService.submit(() -> System.out.println("Task 2"));
executorService.shutdown();

Exception Handling with Custom Exceptions:

class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

try {
    throw new CustomException("This is a custom exception.");
} catch (CustomException e) {
    System.out.println(e.getMessage());
}

Working with Date and Time using the java.time package:

import java.time.LocalDate;
LocalDate today = LocalDate.now();

File reading with buffered reader

import java.io.BufferedReader;
import java.io.FileReader;
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}

File writing with buffered writer

import java.io.BufferedWriter;
import java.io.FileWriter;
try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
    bw.write("This is some text.");
}

Serializing and Deserializing Objects

import java.io.*;
class Person implements Serializable {
    String name;
    int age;
}
// Serialization
Person person = new Person();
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
    out.writeObject(person);
}
// Deserialization
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"))) {
    Person deserializedPerson = (Person) in.readObject();
}

Database connection with JDBC

import java.sql.*;
String url = "jdbc:mysql://localhost:3306/mydb";
String user = "username";
String password = "password";
try (Connection connection = DriverManager.getConnection(url, user, password);
     Statement statement = connection.createStatement()) {
    ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
    while (resultSet.next()) {
        String name = resultSet.getString("name");
        int age = resultSet.getInt("age");
    }
}

Working with regular expressions

import java.util.regex.*;
Pattern pattern = Pattern.compile("\\b\\d{3}-\\d{2}-\\d{4}\\b");
Matcher matcher = pattern.matcher("My SSN is 123-45-6789.");
boolean found = matcher.find();

Sending email with JavaMail API

import javax.mail.*;
import javax.mail.internet.*;
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.example.com");
Session session = Session.getInstance(properties);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@example.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("recipient@example.com"));
message.setSubject("Test Email");
message.setText("This is a test email.");
Transport.send(message);

Working with JSON using Gson (Google JSON library)

import com.google.gson.*;
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "Alice");
jsonObject.addProperty("age", 25);
String jsonString = jsonObject.toString();

Creating and using Enums:

enum DaysOfWeek {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

Working with Collections (Java Collections Framework)

List<String> names = new ArrayList<>();
Map<String, Integer> scores = new HashMap<>();
Set<String> uniqueNames = new HashSet<>();

Using try-with-resources for Auto-Closable Resources

try (BufferedReader br = new BufferedReader(new FileReader("file.txt"));
     BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        bw.write(line);
    }
}

Sorting Collections (e.g.,'Collections.sort()')

List<Integer> numbers = new ArrayList<>();
Collections.sort(numbers);

Using Java Streams and Collectors (Java 8+)

List<String> fruits = Arrays.asList("apple", "banana", "cherry");
List<String> filteredFruits = fruits.stream()
    .filter(fruit -> !fruit.equals("banana"))
    .collect(Collectors.toList());

Functional interfaces and Lambdas (Java8+)

@FunctionalInterface
interface MathOperation {
    int operate(int a, int b);
}
MathOperation addition = (a, b) -> a + b;

Working with threads and synchronization

class Counter {
    private int count = 0;
    public synchronized void increment() {
        count++;
    }
}

Handling Date and Time Zones (Java 8+)

import java.time.ZoneId;
ZoneId zoneId = ZoneId.of("America/New_York");