Collections in java
Collections are a group of classes in Java that provide a way to store and manipulate groups of objects. There are many different types of collections in Java, including lists, sets, maps, and queues. Here is an example of how to use a List collection in Java:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("apple");
fruits.add("banana");
fruits.add("orange");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
In this example, we import the ArrayList and List classes from the java.util package. We then create a new ArrayList object called fruits and add three strings to the list. Finally, we use a for loop to iterate over the list and print each item.
No comments:
Post a Comment