0% found this document useful (0 votes)
16 views5 pages

Methods

The document provides an overview of various Java utility classes and their methods, including java.util.Arrays for array manipulation, java.lang.String for string operations, java.lang.StringBuilder for mutable strings, ArrayList for dynamic array functionality, HashSet for unique collections, and HashMap for key-value pair storage. Each section includes examples demonstrating the usage of methods such as sorting, searching, modifying, and iterating over collections. The document serves as a quick reference for common operations in Java programming.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Methods

The document provides an overview of various Java utility classes and their methods, including java.util.Arrays for array manipulation, java.lang.String for string operations, java.lang.StringBuilder for mutable strings, ArrayList for dynamic array functionality, HashSet for unique collections, and HashMap for key-value pair storage. Each section includes examples demonstrating the usage of methods such as sorting, searching, modifying, and iterating over collections. The document serves as a quick reference for common operations in Java programming.

Uploaded by

venkat Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. java.util.

Arrays (utility methods)


import java.util.Arrays;

int[] nums = {3, 1, 4};

// sort()
Arrays.sort(nums); // [1, 3, 4]

// binarySearch()
int idx = Arrays.binarySearch(nums, 3); // 1

// copyOfRange()
int[] part = Arrays.copyOfRange(nums, 1, 3); // [3, 4]

// fill()
int[] filled = new int[3];
Arrays.fill(filled, 7); // [7, 7, 7]

// toString()
System.out.println(Arrays.toString(nums)); // "[1, 3, 4]"

2. java.lang.String
String s = " Hello Java! ";

// length()
int len = s.length(); // 12

// trim()
String t = s.trim(); // "Hello Java!"
// substring()
String sub = t.substring(6); // "Java!"

// charAt()
char c = t.charAt(0); // 'H'

// contains(), startsWith(), endsWith()


boolean hasJava = t.contains("Java"); // true
boolean starts = t.startsWith("He"); // true
boolean ends = t.endsWith("!"); // true

// replace()
String rep = t.replace("Java", "World"); // "Hello World!"

// split()
String[] parts = t.split(" ");
System.out.println(Arrays.toString(parts)); // ["Hello", "Java!"]

3. java.lang.StringBuilder
StringBuilder sb = new StringBuilder("Hi");

// append(), insert()
sb.append(" there"); // "Hi there"
sb.insert(3, " Java"); // "Hi Java there"

// replace(), delete()
sb.replace(3, 7, "C#"); // "Hi C# there"
sb.delete(3, 6); // "Hi there"
// reverse()
sb.reverse(); // "ereht iH"

// toString()
String result = sb.toString(); // "ereht iH"

4. ArrayList<E>
import java.util.ArrayList;
import java.util.Collections;

ArrayList<String> list = new ArrayList<>();


list.add("A");
list.add("B");
list.add(1, "C"); // ["A", "C", "B"]

System.out.println(list.get(1)); // "C"
list.set(1, "D"); // ["A", "D", "B"]
list.remove("B"); // ["A", "D"]
list.addAll(List.of("X","Y")); // ["A","D","X","Y"]

System.out.println(list.contains("D")); // true
System.out.println(list.size()); // 4

list.sort(Collections.reverseOrder()); // ["Y","X","D","A"]
list.removeIf(e -> e.startsWith("X")); // ["Y","D","A"]

String[] arr = list.toArray(new String[0]);


5. HashSet<E>
import java.util.HashSet;

HashSet<Integer> set = new HashSet<>();


set.add(10);
set.add(20);
set.add(10); // duplicates ignored
System.out.println(set.contains(10)); // true
set.remove(20);
System.out.println(set.size()); // 1

set.forEach(System.out::println);

6. HashMap<K,V>
import java.util.HashMap;
import java.util.Map;

HashMap<String,Integer> map = new HashMap<>();


map.put("A", 1);
map.put("B", 2);

// get(), containsKey(), containsValue()


int a = map.get("A"); // 1
boolean hasB = map.containsKey("B"); // true

// getOrDefault()
int c = map.getOrDefault("C", 0); // 0

// computeIfAbsent()
int newVal = map.computeIfAbsent("C", k -> k.length());
System.out.println(map); // e.g. {A=1, B=2, C=1} :contentReference[oaicite:3]{index=3}

// replace(), remove()
map.replace("A", 10);
map.remove("B");

// keySet(), values(), entrySet()


for (String key: map.keySet()) System.out.println(key + ":" + map.get(key));

// putAll(), clear()
map.putAll(Map.of("X", 100, "Y", 200));
map.clear();

You might also like