Junior — Middle
Can you name the main data collections used in Java?
sobes.tech AI
Answer from AI
In Java, the main data collections are presented in the java.util package and are divided into several groups:
-
List — an ordered list of elements, allows duplicates.
- Implementations:
ArrayList,LinkedList,Vector.
- Implementations:
-
Set — a collection of unique elements, without order or with a specific order.
- Implementations:
HashSet,LinkedHashSet,TreeSet.
- Implementations:
-
Queue — a queue, elements are processed according to a certain rule (e.g., FIFO).
- Implementations:
LinkedList,PriorityQueue.
- Implementations:
-
Map — a key-value mapping, keys are unique.
- Implementations:
HashMap,LinkedHashMap,TreeMap,Hashtable.
- Implementations:
Example of using ArrayList:
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
for (String fruit : list) {
System.out.println(fruit);
}
These collections are widely used for storing and processing data in Java applications.