Collections in JAVA
Collections are a powerful part of the Java language and provide a way to store and manage data. They allow us to store, retrieve, and manipulate data in a variety of ways. In this article, we will explore the different types of collections available in Java and how to use them. We will also discuss some of the common operations that can be performed on collections and provide a short sample code to demonstrate how to use them.
Keywords: Collections, Java, Data, Operations, Sample Code
Introduction to Java Collections
Java collections are a set of classes and interfaces that allow us to store and manage data in a variety of ways. They provide a way to store, retrieve, and manipulate data in an efficient and organized manner. The Java Collections Framework (JCF) provides a set of classes and interfaces that allow us to work with collections of objects.
The JCF is divided into four main parts: the Collection interface, the List interface, the Set interface, and the Map interface. The Collection interface is the root interface of the JCF and is used to represent a group of objects. The List interface is used to represent a collection of objects that can be accessed in a specific order. The Set interface is used to represent a collection of objects that cannot contain duplicate elements. The Map interface is used to represent a collection of key-value pairs.
Common Operations on Collections
There are a number of common operations that can be performed on collections. These operations include adding, removing, and searching for elements, as well as sorting and filtering the collection.
Adding Elements
Adding elements to a collection is a common operation. This can be done using the add()
method. This method takes an element as an argument and adds it to the collection.
List<String> list = new ArrayList<>(); list.add("Hello"); list.add("World");
Removing Elements
Removing elements from a collection is also a common operation. This can be done using the remove()
method. This method takes an element as an argument and removes it from the collection.
List<String> list = new ArrayList<>(); list.add("Hello"); list.add("World"); list.remove("Hello");
Searching for Elements
Searching for elements in a collection is another common operation. This can be done using the contains()
method. This method takes an element as an argument and returns true
if the element is found in the collection, or false
if it is not.
List<String> list = new ArrayList<>(); list.add("Hello"); list.add("World"); boolean containsHello = list.contains("Hello"); // returns true boolean containsFoo = list.contains("Foo"); // returns false
Sorting and Filtering
Sorting and filtering are also common operations that can be performed on collections. The sort()
method can be used to sort a collection according to a given criteria. The filter()
method can be used to filter a collection according to a given criteria.
List<Integer> list = new ArrayList<>(); list.add(3); list.add(1); list.add(2); // sort the list in ascending order list.sort(Comparator.naturalOrder()); // filter the list to only include even numbers list.filter(i -> i % 2 == 0);
Conclusion
In this article, we explored the different types of collections available in Java and how to use them. We discussed some of the common operations that can be performed on collections and provided a short sample code to demonstrate how to use them. Collections are a powerful part of the Java language and provide a way to store and manage data in an efficient and organized manner.
'Development' 카테고리의 다른 글
Enum class in JAVA (0) | 2023.02.19 |
---|---|
Using JSON library in JAVA (0) | 2023.02.19 |
RESTApi of SpringBoot (0) | 2023.02.19 |
Using Spring Data MongoDB (0) | 2023.02.19 |
Creating a Simple RESTful Web Service (0) | 2023.02.19 |