Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

What is the difference between Collections and Collection in Java? Java में Collections और Collection में क्या अंतर है?

Answer
TermWhat it is
CollectionRoot interface of the framework (List, Set, Queue extend it)
CollectionsUtility class with static helper methods for working with collections
// Collection - the interface
Collection<String> collection = new ArrayList<>();
collection.add('A');
collection.size();

// Collections - the utility class, static methods
List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 3, 1, 4, 2));

Collections.sort(numbers);              // [1, 2, 3, 4, 5]
Collections.reverse(numbers);           // [5, 4, 3, 2, 1]
Collections.max(numbers);               // 5
Collections.min(numbers);               // 1
Collections.shuffle(numbers);           // random order
int idx = Collections.binarySearch(numbers, 3);  // requires sorted list

// Creating immutable/unmodifiable collections
List<Integer> unmodifiable = Collections.unmodifiableList(numbers);
// unmodifiable.add(6);  // UnsupportedOperationException

List<Integer> emptyList = Collections.emptyList();
List<Integer> singleton = Collections.singletonList(42);

// Synchronized wrapper for thread-safety
List<Integer> syncList = Collections.synchronizedList(new ArrayList<>());
Termक्या है
CollectionFramework का root interface
CollectionsStatic helper methods वाली utility class
Collection<String> collection = new ArrayList<>();
collection.add('A');
collection.size();

List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 3, 1, 4, 2));

Collections.sort(numbers);
Collections.reverse(numbers);
Collections.max(numbers);
Collections.min(numbers);
Collections.shuffle(numbers);

List<Integer> unmodifiable = Collections.unmodifiableList(numbers);
// unmodifiable.add(6);  // UnsupportedOperationException

List<Integer> emptyList = Collections.emptyList();
List<Integer> syncList = Collections.synchronizedList(new ArrayList<>());

Was this answer clear?