Welcome to the Java Printer Library! This library provides a simple and flexible way to print any data type in Java. Whether you're dealing with primitive types, complex objects, or custom data structures like binary trees, this library makes it easy to display data in a readable format.
- Print int/Integer: Support for printing both
intandIntegertypes. See the example here
(value: 1234)- Print int[]/int[][] array: The ArrayPrinter class is designed to print arrays of integers in a formatted string. It supports both one-dimensional and two-dimensional arrays, making it versatile for various use cases. The output is formatted according to a specified format string defined in the PrinterType interface. See the example here
(value: [1, 2, 3, 4, 5])
(value: [[1, 2, 3], [4, 5, 6], [7, 8, 9]])- Print TreeNode: Support for printing
TreeNodein a vertical format. See the example here
1
┌─────┴─────┐
2 2
┌──┘
3- Helper Classes: Includes utility classes like
TreeNodeandPrefixTreeNodefor working with binary and prefix trees. See the example of TreeNode here and PrefixTreeNode here
// Example for one-dimensional array
int[] values = {1, 2, 3, 4, 5};
ArrayPrinter printer1D = new ArrayPrinter(values);
printer1D.print(); // Output: (value: [1, 2, 3, 4, 5])
// Example for two-dimensional array
int[][] values2D = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
ArrayPrinter printer2D = new ArrayPrinter(values2D);
printer2D.print(); // Output: (value: [[1, 2, 3], [4, 5, 6], [7, 8, 9]])public static void main(String[] args) {
int intValue = 42;
Printer.prettyPrint(intValue);
Printer.prettyPrint(Integer.valueOf(intValue));
}Output:
(value: 42)
(value: 42)TreeNode root = new TreeNode(1, new TreeNode(2, new TreeNode(3)), new TreeNode(2));
Printer.prettyPrint(root);Output:
1
┌─────┴─────┐
2 2
┌──┘
3The TreeNode class is a simple helper class for creating and managing binary trees. Each TreeNode object has an integer value, and references to its left and right children.
TreeNode root = new TreeNode(10);
root.left = new TreeNode(5);
root.right = new TreeNode(15);You can also define it like that.
TreeNode root = new TreeNode(10, new TreeNode(5), new TreeNode(15));The PrefixTreeNode class represents a node in a prefix tree, also known as a Trie. This data structure is particularly useful for efficient retrieval of strings based on their prefixes, making it ideal for applications such as autocomplete, spell checking, and IP routing.
// Create the root node of the Trie
PrefixTreeNode rootNode = new PrefixTreeNode(null);
// Add a word to the Trie
String word = "apple";
PrefixTreeNode currentNode = rootNode;
for (char ch : word.toCharArray()) {
currentNode.children.putIfAbsent(ch, new PrefixTreeNode(ch));
currentNode = currentNode.children.get(ch);
}
// The word "apple" is now added to the TrieTo use this library in your project, you can either clone the repository or include it as a dependency in your build tool.
- JDK11+
Contributions are welcome! If you have suggestions for new features, optimizations, or find a bug, please open an issue or submit a pull request. Make sure your code follows the project's coding standards.