Learn about the list and set in the Ka Commons Collections framework
Ka Commons Collections is part of the Apache Commons project, providing many tools and interfaces for operation and management.In this framework, list and set are one of the commonly used collection types.This article will introduce the list and set in the Ka Commons Collection frame, and provide the corresponding Java code example.
List (list) is an orderly collection that can accommodate multiple elements and can be preserved repeatedly.It provides many methods to add, delete, obtain, and modify the elements.The List interface in Ka Commons Collections inherits the List interface of Java and provides more flexible and powerful functions.
Below is an example code that uses Ka Commons Collections framework:
import org.apache.commons.collections4.ListUtils;
public class ListExample {
public static void main(String[] args) {
// Create a list
List<String> list = ListUtils.newArrayList();
// Add elements
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Get elements
String firstElement = list.get(0);
System.out.println ("The first element is:" + Firsttelement);
// Modify elements
list.set(2, "Grape");
System.out.println ("Modified List:" + List);
// Delete elements
list.remove(1);
System.out.println ("Deleted list:" + list);
}
}
SET (set) is a collection that does not allow repetitive elements to store disorderly elements.The SET interface in Ka Commons Collections inherits the SET interface of Java and provides additional functions and methods.
Below is a SET code that uses Ka Commons Collections framework:
import org.apache.commons.collections4.SetUtils;
public class SetExample {
public static void main(String[] args) {
// Create a set
Set<String> set = SetUtils.newHashSet();
// Add elements
set.add("Apple");
set.add("Banana");
set.add("Orange");
// Find elements
boolean containsApple = set.contains("Apple");
System.out.println ("Whether it contains Apple:" + ContainsApple);
// Delete elements
set.remove("Banana");
System.out.println ("The collection after deleting:" + set);
// Get the size of the collection
int size = set.size();
System.out.println ("The size of the collection:" + size);
}
}
By using the list and set in the Ka Commons Collections framework, we can more conveniently operate and manage the collection to improve the efficiency and readability of the code.
Note: In order to run the above example code, you need to add the relevant dependencies of the KA Commons Collection frame to the construction path of the project.You can find related documents and download links on the official website of Apache Commons.