├── Mapmethods.java ├── Setmethods.java ├── arraylistmethods.java ├── stringmethods with example.java └── vectormethods with example.java /Mapmethods.java: -------------------------------------------------------------------------------- 1 | Methods of Map 2 | The Map interface includes the following methods: 3 | 4 | put(K, V) - Inserts the association of a key K and a value V into the map. If the key is already present, the new value replaces the old value. 5 | putAll() - Inserts all the entries from the specified map to this map. 6 | putIfAbsent(K, V) - Inserts the association if the key K is not already associated with the value V. 7 | get(K) - Returns the value associated with the specified key K. If the key is not found, it returns null. 8 | getOrDefault(K, defaultValue) - Returns the value associated with the specified key K. If the key is not found, it returns the defaultValue. 9 | containsKey(K) - Checks if the specified key K is present in the map or not. 10 | containsValue(V) - Checks if the specified value V is present in the map or not. 11 | replace(K, V) - Replace the value of the key K with the new specified value V. 12 | replace(K, oldValue, newValue) - Replaces the value of the key K with the new value newValue only if the key K is associated with the value oldValue. 13 | remove(K) - Removes the entry from the map represented by the key K. 14 | remove(K, V) - Removes the entry from the map that has key K associated with value V. 15 | keySet() - Returns a set of all the keys present in a map. 16 | values() - Returns a set of all the values present in a map. 17 | entrySet() - Returns a set of all the key/value mapping present in a map. 18 | -------------------------------------------------------------------------------- /Setmethods.java: -------------------------------------------------------------------------------- 1 | set methods 2 | add() - adds the specified element to the set 3 | addAll() - adds all the elements of the specified collection to the set 4 | iterator() - returns an iterator that can be used to access elements of the set sequentially 5 | remove() - removes the specified element from the set 6 | removeAll() - removes all the elements from the set that is present in another specified set 7 | retainAll() - retains all the elements in the set that are also present in another specified set 8 | clear() - removes all the elements from the set 9 | size() - returns the length (number of elements) of the set 10 | toArray() - returns an array containing all the elements of the set 11 | contains() - returns true if the set contains the specified element 12 | containsAll() - returns true if the set contains all the elements of the specified collection 13 | hashCode() - returns a hash code value (address of the element in the set) 14 | -------------------------------------------------------------------------------- /arraylistmethods.java: -------------------------------------------------------------------------------- 1 | Method Description 2 | add(E e) -Appends the specified element to the end of the list. 3 | add(int index, E element)- Inserts the specified element at the specified position in the list. 4 | addAll(Collection c)- Appends all elements from the specified collection to the list. 5 | addAll(int index, Collection c)- Inserts all elements of the collection starting at the specified index. 6 | get(int index)- Returns the element at the specified position. 7 | set(int index, E element) -Replaces the element at the specified position with the specified element. 8 | remove(Object o)- Removes the first occurrence of the specified element. 9 | remove(int index) -Removes the element at the specified position. 10 | clear() -Removes all elements from the list. 11 | contains(Object o) -Returns true if the list contains the specified element. 12 | isEmpty()- Returns true if the list has no elements. 13 | size()- Returns the number of elements in the list. 14 | indexOf(Object o) -Returns the index of the first occurrence of the specified element. 15 | lastIndexOf(Object o) -Returns the index of the last occurrence of the specified element. 16 | iterator()- Returns an iterator over the elements in this list. 17 | listIterator()- Returns a list iterator over the elements in this list. 18 | forEach(Consumer action)- Performs the specified action for each element of the list. 19 | toArray()- Converts the list to an array. 20 | toArray(T[] a)- Converts the list to an array of the specified type. 21 | -------------------------------------------------------------------------------- /stringmethods with example.java: -------------------------------------------------------------------------------- 1 | public class StringMethodsExample { 2 | public static void main(String[] args) { 3 | // 1. substring(int beginIndex) 4 | String str = "Hello, World!"; 5 | System.out.println("substring(7): " + str.substring(7)); // Output: "World!" 6 | 7 | // 2. substring(int beginIndex, int endIndex) 8 | System.out.println("substring(7, 12): " + str.substring(7, 12)); // Output: "World" 9 | 10 | // 3. isEmpty() 11 | String emptyStr = ""; 12 | System.out.println("isEmpty(): " + emptyStr.isEmpty()); // Output: true 13 | System.out.println("isEmpty(): " + str.isEmpty()); // Output: false 14 | 15 | // 4. concat(String str) 16 | String concatStr = str.concat(" Welcome!"); 17 | System.out.println("concat(): " + concatStr); // Output: "Hello, World! Welcome!" 18 | 19 | // 5. replace(char old, char new) 20 | System.out.println("replace('o', 'a'): " + str.replace('o', 'a')); // Output: "Hella, Warld!" 21 | 22 | // 6. replace(CharSequence old, CharSequence new) 23 | System.out.println("replace(\"World\", \"Java\"): " + str.replace("World", "Java")); // Output: "Hello, Java!" 24 | 25 | // 7. split(String regex) 26 | String csv = "apple,banana,grape"; 27 | String[] fruits = csv.split(","); 28 | System.out.println("split(\",\"): "); 29 | for (String fruit : fruits) { 30 | System.out.println(fruit); 31 | } 32 | 33 | // 8. split(String regex, int limit) 34 | String[] limitedFruits = csv.split(",", 2); 35 | System.out.println("split(\",\", 2): "); 36 | for (String fruit : limitedFruits) { 37 | System.out.println(fruit); 38 | } 39 | 40 | // 9. indexOf(int ch) 41 | System.out.println("indexOf('o'): " + str.indexOf('o')); // Output: 4 42 | 43 | // 10. indexOf(int ch, int fromIndex) 44 | System.out.println("indexOf('o', 5): " + str.indexOf('o', 5)); // Output: 8 45 | 46 | // 11. indexOf(String substring) 47 | System.out.println("indexOf(\"World\"): " + str.indexOf("World")); // Output: 7 48 | 49 | // 12. indexOf(String substring, int fromIndex) 50 | System.out.println("indexOf(\"o\", 5): " + str.indexOf("o", 5)); // Output: 8 51 | 52 | // 13. trim() 53 | String spacedStr = " Hello, Java! "; 54 | System.out.println("trim(): " + "\"" + spacedStr.trim() + "\""); // Output: "Hello, Java!" 55 | 56 | // 14. static String valueOf(int value) 57 | int number = 123; 58 | String numberStr = String.valueOf(number); 59 | System.out.println("valueOf(123): " + numberStr); // Output: "123" 60 | 61 | // Using valueOf for other types 62 | char ch = 'A'; 63 | double pi = 3.14159; 64 | System.out.println("valueOf('A'): " + String.valueOf(ch)); // Output: "A" 65 | System.out.println("valueOf(3.14159): " + String.valueOf(pi)); // Output: "3.14159" 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /vectormethods with example.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class VectorExample { 4 | public static void main(String[] args) { 5 | // 1. Vector Initialization 6 | Vector vector = new Vector<>(); 7 | 8 | // Add methods 9 | vector.add(10); 10 | vector.add(20); 11 | vector.add(30); 12 | vector.add(40); 13 | System.out.println("Vector after adding elements: " + vector); 14 | 15 | vector.add(2, 25); // Insert at index 2 16 | System.out.println("Vector after inserting 25 at index 2: " + vector); 17 | 18 | vector.addAll(Arrays.asList(50, 60, 70)); // Append a collection 19 | System.out.println("Vector after adding collection: " + vector); 20 | 21 | vector.addAll(1, Arrays.asList(5, 15)); // Insert a collection at index 1 22 | System.out.println("Vector after inserting collection at index 1: " + vector); 23 | 24 | // Get and Set methods 25 | System.out.println("Element at index 3: " + vector.get(3)); 26 | vector.set(3, 35); // Replace element at index 3 27 | System.out.println("Vector after setting 35 at index 3: " + vector); 28 | 29 | // Remove methods 30 | vector.remove(Integer.valueOf(25)); // Remove first occurrence of 25 31 | System.out.println("Vector after removing 25: " + vector); 32 | 33 | vector.remove(4); // Remove element at index 4 34 | System.out.println("Vector after removing element at index 4: " + vector); 35 | 36 | // Clear, contains, and isEmpty 37 | System.out.println("Vector contains 40: " + vector.contains(40)); 38 | System.out.println("Is the vector empty? " + vector.isEmpty()); 39 | 40 | // Size and Capacity 41 | System.out.println("Vector size: " + vector.size()); 42 | System.out.println("Vector capacity: " + vector.capacity()); 43 | 44 | // Enumeration and Iteration 45 | System.out.println("\nEnumeration of vector elements:"); 46 | Enumeration enumeration = vector.elements(); 47 | while (enumeration.hasMoreElements()) { 48 | System.out.println(enumeration.nextElement()); 49 | } 50 | 51 | System.out.println("\nIterator over vector elements:"); 52 | Iterator iterator = vector.iterator(); 53 | while (iterator.hasNext()) { 54 | System.out.println(iterator.next()); 55 | } 56 | 57 | System.out.println("\nListIterator over vector elements:"); 58 | ListIterator listIterator = vector.listIterator(); 59 | while (listIterator.hasNext()) { 60 | System.out.println(listIterator.next()); 61 | } 62 | 63 | // Searching and Sorting 64 | System.out.println("Index of 40: " + vector.indexOf(40)); 65 | System.out.println("Last index of 40: " + vector.lastIndexOf(40)); 66 | vector.sort(Comparator.naturalOrder()); 67 | System.out.println("Vector after sorting: " + vector); 68 | 69 | // Capacity Management 70 | vector.ensureCapacity(20); 71 | System.out.println("Vector capacity after ensureCapacity(20): " + vector.capacity()); 72 | 73 | vector.trimToSize(); 74 | System.out.println("Vector capacity after trimToSize(): " + vector.capacity()); 75 | 76 | vector.setSize(10); 77 | System.out.println("Vector after setting size to 10: " + vector); 78 | 79 | // Synchronization and Copy 80 | Vector clonedVector = (Vector) vector.clone(); 81 | System.out.println("Cloned Vector: " + clonedVector); 82 | 83 | Integer[] array = vector.toArray(new Integer[0]); 84 | System.out.println("Array converted from Vector: " + Arrays.toString(array)); 85 | 86 | // Clear all elements 87 | vector.clear(); 88 | System.out.println("Vector after clearing: " + vector); 89 | } 90 | } 91 | --------------------------------------------------------------------------------