|
| 1 | +# JavaIterables Library Documentation |
| 2 | + |
| 3 | +The **JavaIterables** library provides two main classes, `List` and `Dict`, which offer flexible and powerful data structures for managing collections of data. These classes are designed to be like Python's `list` and `dict` while being implemented in Java. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +1. Building from source |
| 10 | +2. Usage |
| 11 | +3. Classes |
| 12 | + - List |
| 13 | + - Dict |
| 14 | +4. Examples |
| 15 | + - Using `List` |
| 16 | + - Using `Dict` |
| 17 | +5. Testing |
| 18 | +6. License |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Building from source |
| 23 | + |
| 24 | +1. Compile the library and package it into a `.jar` file: |
| 25 | + |
| 26 | + ```bash |
| 27 | + cd javaiterables-main/ |
| 28 | + ./build |
| 29 | + ``` |
| 30 | + |
| 31 | + It will generate a `javaiterables.jar` on `out` directory. |
| 32 | + |
| 33 | +2. Add the `javaiterables.jar` file to your project's classpath. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +You can use the library in two ways: |
| 40 | + |
| 41 | +1. **As a module**: |
| 42 | + Add the following to your `module-info.java`: |
| 43 | + |
| 44 | + ```java |
| 45 | + module yourmodule { |
| 46 | + requires javaiterables; |
| 47 | + } |
| 48 | + ``` |
| 49 | + |
| 50 | +2. **Without modules**: |
| 51 | + Add the `.jar` file to your classpath and import the classes directly: |
| 52 | + |
| 53 | + ```java |
| 54 | + import javaiterables.List; |
| 55 | + import javaiterables.Dict; |
| 56 | + ``` |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Classes |
| 61 | + |
| 62 | +### **List** |
| 63 | + |
| 64 | +The `List` class is a flexible data structure for managing ordered collections of elements. It supports various data types and provides methods for manipulation, iteration, and analysis. |
| 65 | + |
| 66 | +#### **List Methods** |
| 67 | + |
| 68 | +| Method | Description | |
| 69 | +|---------------------------------|-----------------------------------------------------------------------------| |
| 70 | +| `void append(Object val)` | Adds an element to the end of the list. | |
| 71 | +| `void remove(int index)` | Removes the element at the specified index. | |
| 72 | +| `Object get(int index)` | Retrieves the element at the specified index. | |
| 73 | +| `void set(int index, Object val)` | Replaces the element at the specified index with a new value. | |
| 74 | +| `int len()` | Returns the number of elements in the list. | |
| 75 | +| `void reverse()` | Reverses the order of elements in the list. | |
| 76 | +| `boolean isEqualTo(List list)` | Checks if the current list is equal to another list. | |
| 77 | +| `void clear()` | Removes all elements from the list. | |
| 78 | +| `List copyList()` | Creates a copy of the current list. | |
| 79 | +| `boolean contains(Object val)` | Checks if the list contains a specific value. | |
| 80 | +| `void extend(List list)` | Appends all elements from another list to the current list. | |
| 81 | +| `void insert(int index, Object val)` | Inserts an element at the specified index. | |
| 82 | +| `void show()` | Prints the list in a readable format. | |
| 83 | +| `double sum()` | Returns the sum of all numeric elements in the list. | |
| 84 | +| `double avg()` | Returns the average of all numeric elements in the list. | |
| 85 | +| `double min()` | Returns the smallest numeric element in the list. | |
| 86 | +| `double max()` | Returns the largest numeric element in the list. | |
| 87 | +| `String uniteListString(String sep)` | Joins all elements into a single string, separated by the given delimiter. | |
| 88 | +| `java.util.List<Object> listIterate()` | Returns a Java `List` containing all elements. | |
| 89 | +| `Object[] arrayIterate()` | Returns an array containing all elements. | |
| 90 | +| `List sort(boolean ascending)` | Returns a sorted version of the list (ascending or descending). | |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +### **Dict** |
| 95 | + |
| 96 | +The `Dict` class is a key-value data structure similar to Python's `dict`. It allows for efficient storage and retrieval of data based on unique keys. |
| 97 | + |
| 98 | +#### **Dict Methods** |
| 99 | + |
| 100 | +| Method | Description | |
| 101 | +|---------------------------------|-----------------------------------------------------------------------------| |
| 102 | +| `void add(String key, Object value)` | Adds a key-value pair to the dictionary. | |
| 103 | +| `void set(String key, Object value)` | Updates the value associated with a key. | |
| 104 | +| `Object get(String key)` | Retrieves the value associated with a key. | |
| 105 | +| `void remove(String key)` | Removes a key-value pair from the dictionary. | |
| 106 | +| `void anullValue(String key)` | Sets the value of a key to `null`. | |
| 107 | +| `void sortByKeys(boolean ascending)` | Sorts the dictionary by its keys (ascending or descending). | |
| 108 | +| `void sortByValues(boolean ascending)` | Sorts the dictionary by its values (ascending or descending). | |
| 109 | +| `List keys()` | Returns a `List` of all keys in the dictionary. | |
| 110 | +| `List values()` | Returns a `List` of all values in the dictionary. | |
| 111 | +| `void restart()` | Resets the internal iterator for the dictionary. | |
| 112 | +| `void clear()` | Removes all key-value pairs from the dictionary. | |
| 113 | +| `List next(int step)` | Retrieves the next key-value pair based on the internal iterator. | |
| 114 | +| `int size()` | Returns the number of key-value pairs in the dictionary. | |
| 115 | +| `boolean containsKey(String key)` | Checks if the dictionary contains a specific key. | |
| 116 | +| `boolean containsValue(Object value)` | Checks if the dictionary contains a specific value. | |
| 117 | +| `boolean contains(Object any)` | Checks if the dictionary contains a specific key or value. | |
| 118 | +| `boolean isEqualTo(Dict other)` | Checks if the current dictionary is equal to another dictionary. | |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## Examples |
| 123 | + |
| 124 | +### **Using `List`** |
| 125 | + |
| 126 | +```java |
| 127 | +import javaiterables.List; |
| 128 | + |
| 129 | +public class Main { |
| 130 | + public static void main(String[] args) { |
| 131 | + List myList = new List(); |
| 132 | + myList.append("Hello"); |
| 133 | + myList.append(42); |
| 134 | + myList.append(3.14); |
| 135 | + |
| 136 | + System.out.println("List contents:"); |
| 137 | + for (int i = 0; i < myList.len(); i++) { |
| 138 | + System.out.println(myList.get(i)); |
| 139 | + } |
| 140 | + |
| 141 | + myList.sort(true); // Ascending is the variable which is given as true |
| 142 | + System.out.println("Sorted list:"); |
| 143 | + myList.show(); |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +### **Using `Dict`** |
| 151 | + |
| 152 | +```java |
| 153 | +import javaiterables.Dict; |
| 154 | + |
| 155 | +public class Main { |
| 156 | + public static void main(String[] args) { |
| 157 | + Dict myDict = new Dict(); |
| 158 | + myDict.add("name", "Alice"); |
| 159 | + myDict.add("age", 30); |
| 160 | + myDict.add("height", 5.5); |
| 161 | + |
| 162 | + System.out.println("Dictionary contents:"); |
| 163 | + System.out.println("Name: " + myDict.get("name")); |
| 164 | + System.out.println("Age: " + myDict.get("age")); |
| 165 | + System.out.println("Height: " + myDict.get("height")); |
| 166 | + |
| 167 | + myDict.remove("age"); |
| 168 | + System.out.println("After removing 'age':"); |
| 169 | + System.out.println("Contains 'age': " + myDict.containsKey("age")); |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +## Testing |
| 177 | + |
| 178 | +The library includes a `Test` class in the tests package to validate the functionality of `List` and `Dict`. To run the tests: |
| 179 | + |
| 180 | +1. Compile the tests: |
| 181 | + |
| 182 | + ```bash |
| 183 | + javac -cp ./out/javaiterables.jar -d out ./tests/Test.java |
| 184 | + ``` |
| 185 | + |
| 186 | +2. Run the tests: |
| 187 | + |
| 188 | + ```bash |
| 189 | + java -cp ./out/javaiterables.jar:out ./tests.Test |
| 190 | + ``` |
| 191 | + |
| 192 | +3. Log file: |
| 193 | + |
| 194 | + Look into the file test.log in the folder tests to see the last results of the tests. |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## License |
| 199 | + |
| 200 | +This library is open-source and available under the [MIT License](https://opensource.org/licenses/MIT). Feel free to use, modify, and distribute it as needed. |
0 commit comments