1. How to find the minimum value in a list?
Primitive:
List<Integer> lst = Arrays.asList(1, 2, 3, 4, 56, 7, 89, 10);
System.out.println(Collections.min(lst));
Object:
List<Employee> lst = new ArrayList<Employee>();
lst.add(new Employee(10, "Raghu", 25000));
lst.add(new Employee(120, "Krish", 45000));
lst.add(new Employee(210, "John", 14000));
Employee minSal = Collections.min(lst, new EmpComp());
System.out.println(minSal);
class EmpComp implements Comparator<Employee>{
public int compare(Employee e1, Employee e2) {
return e1.getSalary().compareTo(e2.getSalary());
}
}
Stream
lst.stream.min((e1, e2) -> e1.getSalary().compareTo(e2.getSalary())).get();
2. How to get a list sorted?
Primitive:
Collections.sort(lst));
Object:
Collections.sort(lst, new EmpComp());
Stream:
lst.stream.sorted((e1, e2) -> e1.getSalary().compareTo(e2.getSalary())).collect(Collectors.toList());
Reverse Order:
Collections.sort(lst, Collections.reverseOrder());
lst.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
3. How to reverse a String?
Char Array:
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
char[] arr = str.toCharArray();
for (int i = arr.length - 1; i >= 0; i--)
System.out.print(arr[i]);
StringBuilder:
StringBuilder sb = new StringBuilder(str);
sb.reverse().toString();
Arraylist:
List<Character> lst = new ArrayList<>();
for (char c : str)
lst.add(c);
Collections.reverse(lst);
Stream:
Stream.of(str)
.map(word->new StringBuilder(word).reverse().toString())
.collect(Collectors.joining(" ")).get(0);
Stream with index:
char[] arr = str.toCharArray();
IntStream.range(0, arr.length)
.mapToObj(i -> arr[(arr.length - 1) - i])
.forEach(System.out::print);
A String is a palindrome if the String is same as the Reversed String.
4. How to search an element?
In String:
if(str.indexOf("word") > -1)
In Arraylist of Primitives:
boolean found = lst.contains(2);
In Arraylist of Objects:
for (Employee emp : emplist) {
if (emp.getName().equals(name)) {
return emp;
}
}
Using Stream:
Employee empl = emplist.stream()
.filter(emp -> name.equals(emp.getName()))
.findAny()
.orElse(null);
5. How to count the occurrences?
String:
Linear search:
for (int i=0; i<str.length(); i++){
if (s.charAt(i) == c)
count++;
}
return count;
Binary Search:
while (low <= high) {
mid = (low + high) / 2;
if (a[mid].compareTo(x) < 0) {
low = mid + 1;
} else if (a[mid].compareTo(x) > 0) {
high = mid - 1;
} else {
return mid;
}
}
Arraylist:
Collections.frequency(list, new Employee(321222, "John", 29));
public boolean equals(Employee e) {
if(this.id.equals(e.getId()) && this.name.equals(e.getName())) {
return true;
}
return false;
}
Stream:
lst.stream().filter(str -> str.indexOf("ch") > -1).count();
To get count of each item,
lst.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
6. How to replace an element?
String:
str.replace('h', 'm');
Arraylist
Collections.replaceAll(lst, null, " ");
Stream:
List<String> newList = lst.stream()
.map(s -> s != null ? s : "")
.collect(Collectors.toList());
No comments:
Post a Comment