Sobes.tech
Junior — Middle

Can you list the main methods of the Collections class in Java?

sobes.tech AI

Answer from AI

The Collections class in Java provides a set of static methods for working with collections. Here are the main ones:

  • sort(List<T> list) — sorts the list in natural order.
  • sort(List<T> list, Comparator<? super T> c) — sorts using a comparator.
  • reverse(List<?> list) — reverses the order of elements in the list.
  • shuffle(List<?> list) — randomly shuffles the elements.
  • binarySearch(List<? extends Comparable<? super T>> list, T key) — performs a binary search for an element in a sorted list.
  • max(Collection<? extends T> coll) — finds the maximum element.
  • min(Collection<? extends T> coll) — finds the minimum element.
  • fill(List<? super T> list, T obj) — fills the list with a single value.
  • copy(List<? super T> dest, List<? extends T> src) — copies elements from one list to another.
  • synchronizedList(List<T> list) — returns a thread-safe wrapper for the list.

Example usage:

List<String> names = new ArrayList<>(Arrays.asList("Anna", "John", "Mike"));
Collections.sort(names);
System.out.println(names); // [Anna, John, Mike]
Collections.reverse(names);
System.out.println(names); // [Mike, John, Anna]
Can you list the main methods of the Collections… - sobes.tech