String s = "0123456789";
// Returns a new string which is the substring of a specified string
String sub = s.substring(1,4)
// sub -> "123" (1-inclusive, 4-exclusive)
sub = s.substring(6)
//sub -> "6789"
compareTo returns:
- An int value: 0 if the string is equal to the other string.
- < 0 if the string is lexicographically less than the other string
- > 0 if the string is lexicographically greater than the other string (more characters)
s1.compareTo(s2);
s1.compareToIgnoreCase(s2);
s1.equals(s2);
s1.equalsIgnoreCase(s2);
char[] chars = s1.toCharArray();
// Returns the position of the last found occurrence of
// specified characters in a string
lastIndexOf()
// Returns the position of the first found occurrence of
// specified characters in a string
indexOf(int char, int fromIndex)
indexOf(String str
// returns A new String, where the specified character
// has been replaced by the new character(s)
String myStr = "Hello";
System.out.println(myStr.replace('l', 'p'));
//Replaces the first occurrence of a substring that matches the
// given regular expression with the given replacement
replaceFirst()
//Replaces each substring of this string that matches the given regular expression
// with the given replacement
replaceAll()
String replaceString=s1.replaceAll("a","e");//replaces all occurrences of "a" to "e"
String s = "Welcome to Baeldung";
String[] expected1 = new String[] { "Welcome", "to", "Baeldung" };
String[] expected2 = new String[] { "Welcome", "to Baeldung" };
assertArrayEquals(expected1, s.split(" "));
assertArrayEquals(expected2, s.split(" ", 2));
(Arrays class)
String a[] = new String[] { "A", "B", "C", "D" };
// Getting the list view of Array
List<String> list = Arrays.asList(a);
int ar[] = {2, 2, 1, 8, 3, 2, 2, 4, 2};
// To fill complete array with a particular value
Arrays.fill(ar, 10);
int numbers[] = {2, 2, 1, 8, 3, 2, 2, 4, 2};
// Using Arrays.parallelSort()
Arrays.parallelSort(numbers);
//or
Arrays.sort(numbers)
cars.set(0, "Opel");
cars.remove(0);
cars.clear();
Stack<E> stack = new Stack<E>();
stack.push(x);
Integer y = stack.pop();
Displaying element on the top of the stack
Integer element = (Integer) stack.peek();
Integer pos = (Integer) stack.search(element);
if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position: " + pos);
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(1);
System.out.println("head: "+pq.peek());
Iterator itr=pq.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
kinda like pop but from the other side
System.out.println("queue poll: " + pq.poll());
HashMap<String, String> capitalCities = new HashMap<String, String>();
// Add keys and values (Country, City)
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities);
capitalCities.get("England");
capitalCities.remove("England");
capitalCities.clear();
for (String i : people.keySet()) {
System.out.println("key: " + i + " value: " + people.get(i));
}
// returns true or false
map.containsKey(4);
map.containsValue("aaaa");
map.put(key, map.get(key) + 1);
A HashSet is a collection of items where every item is unique
public class Main {
public static void main(String[] args) {
HashSet<String> cars = new HashSet<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("BMW");
cars.add("Mazda");
System.out.println(cars);
}
}
cars.contains("Mazda");
cars.remove("Volvo");