(predicate))\n | std::ranges::to (predicate))\n | std::ranges::to Typerwriter Animation (predicate))\n | std::ranges::to (predicate))\n | std::ranges::to> zip(List list1, List list2) {\n // Create pairs by iterating through the indices of both lists\n return IntStream.range(0, Math.min(list1.size(), list2.size())) // Limit the range to the smaller list\n .mapToObj(i -> Arrays.asList(list1.get(i), list2.get(i))) // Pair elements from both lists at index i\n .collect(Collectors.toList()); // Collect the pairs into a List\n}\n\n// Usage:\nList
> zipped = zip(arr1, arr2);\n\nSystem.out.println(zipped); // Output: [[a, 1], [b, 2], [c, 3]]\n",
- "extension": "java"
- }
- ]
- },
- {
- "name": "Basics",
- "snippets": [
- {
- "title": "Hello-World",
- "description": "Prints Hello world in the console",
- "author": "SarvariHarshitha",
- "tags": [
- "java",
- "console",
- "printing"
- ],
- "contributors": [],
- "code": "// This is the main class of the Java program\npublic class Main {\n // The main method is the entry point of the program\n public static void main(String args[]) {\n // This statement prints \"Hello, World!\" to the console\n System.out.println(\"Hello, World!\");\n }\n}\n\n",
- "extension": "java"
- }
- ]
- },
- {
- "name": "Bit Manipulation",
- "snippets": [
- {
- "title": "Bit Counting",
- "description": "Counts the set bits in the binary representation of an integer",
- "author": "Mcbencrafter",
- "tags": [
- "math",
- "number",
- "bits",
- "bit-counting"
- ],
- "contributors": [],
- "code": "public static int countBits(int number) {\n int bits = 0;\n \n while (number > 0) {\n bits += number & 1;\n number >>= 1;\n }\n\n return bits;\n}\n\n// Usage:\nint number = 5;\nSystem.out.println(countBits(5)); // 2 (101)\n",
- "extension": "java"
- },
- {
- "title": "Is Power Of Two",
- "description": "Checks if a number is a power of two",
- "author": "Mcbencrafter",
- "tags": [
- "math",
- "number",
- "bit",
- "power-of-two"
- ],
- "contributors": [],
- "code": "public static boolean isPowerOfTwo(int number) {\n return (number > 0) && ((number & (number - 1)) == 0);\n}\n\n// Usage:\nint number = 16;\nSystem.out.println(isPowerOfTwo(number)); // true (2^4)\n",
- "extension": "java"
- }
- ]
- },
- {
- "name": "Date Time",
- "snippets": [
- {
- "title": "Date Time Formatting American",
- "description": "Formats a timestamp to a human-readable date-time string in the format \"MM/dd/yyyy hh:mm:ss a\"",
- "author": "Mcbencrafter",
- "tags": [
- "date",
- "time",
- "date-time",
- "formatting",
- "american"
- ],
- "contributors": [],
- "code": "import java.time.Instant;\nimport java.time.ZoneId;\nimport java.time.format.DateTimeFormatter;\nimport java.util.concurrent.TimeUnit;\n\n// using the system default time zone\npublic static String formatDateTimeAmerican(long time, TimeUnit timeUnit) {\n return formatDateTimeAmerican(time, timeUnit, ZoneId.systemDefault());\n}\n\npublic static String formatDateTimeAmerican(long time, TimeUnit timeUnit, ZoneId timeZone) {\n return DateTimeFormatter.ofPattern(\"MM/dd/yyyy hh:mm:ss a\")\n .withZone(\n timeZone != null ? timeZone : ZoneId.systemDefault()\n )\n .format(Instant.ofEpochSecond(\n timeUnit.toSeconds(time)\n ));\n}\n\n// Usage:\nSystem.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS)); // \"12/31/2024 | 11:59:59 PM\" for GMT+0000\nSystem.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS, ZoneId.of(\"GMT+0000\"))); // \"12/31/2024 | 11:59:59 PM\"\n",
- "extension": "java"
- },
- {
- "title": "Date Time Formatting European",
- "description": "Formats a timestamp to a human-readable date-time string in the format \"dd.MM.yyyy HH:mm:ss\"",
- "author": "Mcbencrafter",
- "tags": [
- "date",
- "time",
- "date-time",
- "formatting",
- "european"
- ],
- "contributors": [],
- "code": "import java.time.Instant;\nimport java.time.ZoneId;\nimport java.time.format.DateTimeFormatter;\nimport java.util.concurrent.TimeUnit;\n\n// using the system default time zone\npublic static String formatDateTimeEuropean(long time, TimeUnit timeUnit) {\n return formatDateTimeEuropean(time, timeUnit, ZoneId.systemDefault());\n}\n\npublic static String formatDateTimeEuropean(long time, TimeUnit timeUnit, ZoneId timeZone) {\n return DateTimeFormatter.ofPattern(\"dd.MM.yyyy HH:mm:ss\")\n .withZone(\n timeZone != null ? timeZone : ZoneId.systemDefault()\n )\n .format(Instant.ofEpochSecond(\n timeUnit.toSeconds(time)\n ));\n}\n\n// Usage:\nSystem.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS)); // \"31.12.2024 | 23:59:59\" for GMT+0000\nSystem.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS, ZoneId.of(\"GMT+0000\"))); // \"31.12.2024 | 23:59:59\"\n",
- "extension": "java"
- },
- {
- "title": "Duration Formatting Hours Minutes Seconds",
- "description": "Converts a given time duration to a human-readable string in the format \"hh:mm(:ss)\"",
- "author": "Mcbencrafter",
- "tags": [
- "time",
- "formatting",
- "hours",
- "minutes",
- "seconds"
- ],
- "contributors": [],
- "code": "import java.util.concurrent.TimeUnit;\n \npublic static String formatDurationToHoursMinutesAndSeconds(int time, TimeUnit timeUnit, boolean showSeconds) {\n long totalSeconds = timeUnit.toSeconds(time);\n\n if (totalSeconds < 0)\n throw new IllegalArgumentException(\"Duration must be a non-negative value.\");\n\n // These variables can be directly used in the return statement,\n // but are kept as separate variables here for better readability.\n long hours = totalSeconds / 3600;\n long minutes = (totalSeconds % 3600) / 60;\n long seconds = totalSeconds % 60;\n\n if (showSeconds) {\n return String.format(\"%02d:%02d:%02d\", hours, minutes, seconds);\n } else {\n return String.format(\"%02d:%02d\", hours, minutes);\n }\n}\n\n// Usage:\nSystem.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, true)); // \"01:03:30\"\nSystem.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, false)); // \"01:03\"\n",
- "extension": "java"
- },
- {
- "title": "Duration Formatting Minutes Seconds",
- "description": "Converts a given time duration to a human-readable string in the format \"mm:ss\"",
- "author": "Mcbencrafter",
- "tags": [
- "time",
- "formatting",
- "minutes",
- "seconds"
- ],
- "contributors": [],
- "code": "import java.util.concurrent.TimeUnit;\n\npublic static String formatDurationToMinutesAndSeconds(int time, TimeUnit timeUnit) {\n long totalSeconds = timeUnit.toSeconds(time);\n\n if (totalSeconds < 0)\n throw new IllegalArgumentException(\"Duration must be a non-negative value.\");\n\n // These variables can be directly used in the return statement,\n // but are kept here as separate variables for better readability.\n long minutes = totalSeconds / 60;\n long seconds = totalSeconds % 60;\n\n return String.format(\"%02d:%02d\", minutes, seconds);\n}\n\n// Usage:\nSystem.out.println(formatDurationToMinutesAndSeconds(120, TimeUnit.SECONDS)); // \"02:00\"\nSystem.out.println(formatDurationToMinutesAndSeconds(75, TimeUnit.SECONDS)); // \"01:15\"\n",
- "extension": "java"
- }
- ]
- },
- {
- "name": "Math",
- "snippets": [
- {
- "title": "Checksum",
- "description": "Calculates the checksum of an int",
- "author": "Mcbencrafter",
- "tags": [
- "math",
- "number",
- "checksum"
- ],
- "contributors": [],
- "code": "public static int checksum(int number) {\n number = Math.abs(number);\n int sum = 0;\n\n while (number != 0) {\n sum += number % 10;\n number /= 10;\n }\n\n return sum;\n}\n\n// Usage:\nint number = 12345;\nSystem.out.println(checksum(number)); // 15 = 1+2+3+4+5\n",
- "extension": "java"
- },
- {
- "title": "Factorial",
- "description": "Computes the factorial of a given number",
- "author": "Mcbencrafter",
- "tags": [
- "math",
- "number",
- "factorial"
- ],
- "contributors": [],
- "code": "import java.math.BigInteger;\n\npublic static BigInteger factorial(int number) {\n BigInteger result = BigInteger.ONE;\n\n for (int currentNumber = 1; currentNumber <= number; currentNumber++) {\n result = result.multiply(BigInteger.valueOf(currentNumber));\n }\n\n return result;\n}\n\n// Usage:\nint number = 6;\nSystem.out.println(factorial(number)); // 720 = 6*5*4*3*2\n",
- "extension": "java"
- },
- {
- "title": "Fibonacci",
- "description": "Calculates the nth fibonacci number",
- "author": "Mcbencrafter",
- "tags": [
- "math",
- "number",
- "fibonacci"
- ],
- "contributors": [],
- "code": "public static int fibonacci(int number) {\n if (number <= 1) \n return number;\n \n return fibonacci(number - 1) + fibonacci(number - 2);\n}\n\n// Usage:\nint number = 5;\nSystem.out.println(fibonacci(number)) // 3 (0, 1, 1, 2, 3)\n",
- "extension": "java"
- },
- {
- "title": "Greatest Common Divisor",
- "description": "Calculates the greatest common divisor (gcd) of two numbers",
- "author": "Mcbencrafter",
- "tags": [
- "math",
- "number",
- "greatest-common-devisor",
- "gcd",
- "euclidean-algorithm"
- ],
- "contributors": [],
- "code": "public static int gcd(int number1, int number2) {\n while (number2 != 0) {\n int remainder = number2;\n number2 = number1 % number2;\n number1 = remainder;\n }\n\n return number1;\n}\n\n// Usage:\nint a = 16;\nint b = 12;\nSystem.out.println(gcd(a, b)); // 4\n",
- "extension": "java"
- },
- {
- "title": "Least Common Multiple",
- "description": "Calculates the least common multiple (lcm) of two numbers",
- "author": "Mcbencrafter",
- "tags": [
- "math",
- "number",
- "least-common-multiple",
- "lcm",
- "euclidean-algorithm"
- ],
- "contributors": [],
- "code": "public static int lcm(int number1, int number2) {\n int gcdNumber1 = number1;\n int gcdNumber2 = number2;\n \n while (gcdNumber2 != 0) {\n int remainder = gcdNumber2;\n gcdNumber2 = gcdNumber1 % gcdNumber2;\n gcdNumber1 = remainder;\n }\n \n return (number1 / gcdNumber1) * number2;\n}\n\n// Usage:\nint a = 16;\nint b = 12;\nSystem.out.println(lcm(a, b)); // 48\n",
- "extension": "java"
- },
- {
- "title": "Prime Check",
- "description": "Checks if a number is a prime",
- "author": "Mcbencrafter",
- "tags": [
- "math",
- "number",
- "prime"
- ],
- "contributors": [],
- "code": "public static boolean isPrime(int number) {\n if (number <= 1) \n return false;\n\n if (number <= 3) \n return true;\n\n boolean prime = true;\n for (int divisor = 3; divisor < number; divisor++) {\n if (number % divisor != 0)\n continue;\n\n prime = false;\n break;\n }\n\n return prime;\n}\n\n// Usage:\nint number = 31;\nSystem.out.println(isPrime(number)); // true\n",
- "extension": "java"
- }
- ]
- },
- {
- "name": "String Manipulation",
- "snippets": [
- {
- "title": "Ascii To String",
- "description": "Converts a list of ascii numbers into a string",
- "author": "Mcbencrafter",
- "tags": [
- "string",
- "ascii",
- "encoding",
- "decode",
- "conversion"
- ],
- "contributors": [],
- "code": "import java.util.List;\n\npublic static String asciiToString(List
Hello, World!
\n