Java coding test: String/Array/Collections

An algorithm problem's input is often a string, array or collections. You may write the code on the white board. If you know better way to write code in Java 8 it give you additional advantages. 

Array / String

toCharArray() //get char array of a String
charAt< >(int x< >) //get a char at the specific index
length< >(< >) //string length
length //array size 
substring< >(int beginIndex< >) 
substring< >(int beginIndex, int endIndex< >)
Integer.valueOf< >(< >)//string to integer
String.valueOf< >(< >)/integer to string
Arrays.sort< >(< >)  //sort an array
Arrays.toString< >(char< >[< >] a< >) //convert to string
Arrays.copyOf< >(T< >[< >] original, int newLength< >)
System.arraycopy< >(Object src, int srcPos, Object dest, int destPos, int length< >)

Collections

List, Map, Set.

ArrayList

get() //get object from List
add() //add object into the List
size() //number of list elements
for(Object obj : list){} //loop 
Collections.sort(list) // sort your list 
public void calculate_average_from_list_with_java () {
    
    Double sum = 0d;
    for (Double vals : NUMBERS_FOR_AVERAGE) {
        sum += vals;
    }
        
    sum =  sum / NUMBERS_FOR_AVERAGE.size();
    
    assertEquals(new Double(10), sum);
}

Java 8

@Test
public void calculate_average_from_list_with_java8_lambda () {
    
    OptionalDouble average = NUMBERS_FOR_AVERAGE
            .stream()
            .mapToDouble(a -> a)
            .average();

    assertEquals(10, average.getAsDouble(), 0);
}

Collections.max(arrayList);
daysOfWeekAsList.removeIf(p -> p.equalsIgnoreCase("Monday"));

Classic problems: