java 2 3
fdsa
fdsa
-
- 1 / 496
-
Flashcards
Are duplicates allowed in lists?
yes
What happens with remove() on a list and a index passed which does not exists?
IndexOutOfBoundsException
When is ArrayList a good choice?
when you are reading more often than(or the same amount as) writing to the ArrayLList
When is LinkedList a good choice?
when yo will be using it as a queue
What are the factorymethods to create a list?
- Arrays.asList(varargs)
- List.of(varargs)
- List.copyOf(collection)
Is it possible to add / replace / delete elements on a list which is created by Arrays.asList(varargs)?
No, Yes, No
Is it possible to add / replace / delete elements on a list which is created by List.of(varargs)?
No, No, No
Is it possible to add / replace / delete elements on a list which is created by List.copyOf(collection)
No, No, No
Is Map.of with two or more key-value-pairs possible?
Yes but key and value per set has to exist.
F.e. Valud: Map.of("key1","value1","key2","value2");
invalid: Map.of("key1","value1", "key2");
Can the type List contain duplicate elements / elements always ordered / has keys and values / must add/remove in specific order?
Yes / Yes / No / No
Can the type Map contain duplicate elements / elements always ordered / has keys and values / must add/remove in specific order?
Yes / No / Yes / No
Can the type queue contain duplicate elements / elements always ordered / has keys and values / must add/remove in specific order?
Yes / Yes / No / Yes
Can the type Set contain duplicate elements / elements always ordered / has keys and values / must add/remove in specific order?
No / No / No / No
Is a type ArrayList sorted? / calls hashcode? / calls compareTo?
No / No / No
Is a type HashMap sorted? / calls hashcode? / calls compareTo?
No / Yes / No
Is a type HashSet sorted? / calls hashcode? / calls compareTo?
No / Yes / No
Is a type LinkedList sorted? / calls hashcode? / calls compareTo?
No / No / No
Is a type TreeMap sorted? / calls hashcode? / calls compareTo?
Yes / No / Yes
Is a type TreeSet sorted? / calls hashcode? / calls compareTo?
Yes / No / Yes
Sorting: Whats the order (numbers/letters, upercases)?
numbers before letters, uppercase before lowercase
When can classes be used for datastructures that require comparision?
if it implements comparable
When to use a comparator?
to specify that you want to use a different order than the object itslefs provides
Whats the signature of Comparable-interface?
public interface Comparable<T> {
int compareTo( T o);
}
When is a Cast needed within the compareTo-method?
when dealing with legacy code or code that does not use generics
When implementing compareTo, where do we have to pay attention to?
keeping compareTo and equals consistent
Can comparable or comparator be used without a import statement?
Comparator yes, comparable not
Whats the package name, whats the method name, whats the number of parameters, has the interface to be implemented by class compaing in class Comparable?
java.lang / compareTo() / 1 / Yes
Whats the package name, whats the method name, whats the number of parameters, has the interface to be implemented by class compaing in class Comparator?
java.util / compare() / 2 / no
Whats the static comparator-method for comparing by results of a function that returns any object?
comparing(function)
Whats the static comparator-method for comparing by the results of a function that returns a double?
comparingDouble(function)
Whats the static comparator-method for comparing by the results of a function that returns an int?
comparingInt(function)
Whats the static comparator-method for comparing by the results of a function that returns a long?
comparingLong(function)
Whats the static comparator-method for sorting using the order specified by the comparable implementation on the object itself?
naturalOrder()
Whats the static comparator-method for sorting using the reverse of the order specified by the Comparable implementation on the object itself?
reverseOrder()
Whats the default-method in comparator for reversing the order of the chained comparator?
reversed()
Whats the default-method in comparator if the previous comparator returns 0 se this comparator that returns a double/int/long , otherwise return the value form the previous comparator
thenComparingDouble(function) / thenComparingInt(function) / thenComparingLong(function)
When can we use Collections.sort()?
When the objects to be sorted are Comparable
What are the preconditions for Collections.binarySearch?
Must be sorted
What cant be done with genericTypes?
- calling a constructor
- creating an array of that generic type
- calling instanceof
- using a primitive type as a generic type parameter
- creating a static variable as a generic type parameter
What happns if you add list.removeIf on a list which was created by list.of()?
unsupportedOperation