Java collections framework example
Java collections framework example
Here is an example of using the Java Collections Framework (JCF) to manipulate and analyze a collection of data:
Suppose we have a list of Student objects, each with attributes such as name, age, and GPA. We want to sort this list based on GPA in descending order, find the average GPA, calculate the total sum of GPAs, and then create a new list containing only students with GPA above 3.5.
Here is an example implementation using JCF:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Student {
String name;
int age;
double gpa;
public Student(String name, int age, double gpa) {
this.name = name;
this.age = age;
this.gpa = gpa;
}
public String toString() {
return "Name: " + name + ", Age: " + age + ", GPA: " + gpa;
}
}
public class StudentListExample {
public static void main(String[] args) {
// Create a list of Student objects
List students = new ArrayList<>();
students.add(new Student("John", 20, 3.8));
students.add(new Student("Jane", 21, 3.5));
students.add(new Student("Mike", 22, 3.9));
students.add(new Student("Emily", 23, 3.4));
// Sort the list based on GPA in descending order
Collections.sort(students, (student1, student2) -> Double.compare(student2.gpa, student1.gpa));
// Calculate the total sum of GPAs
double totalGPA = students.stream()
.mapToDouble(Student::getGpa)
.sum();
System.out.println("Total GPA: " + totalGPA);
// Find the average GPA
double averageGPA = totalGPA / students.size();
System.out.println("Average GPA: " + averageGPA);
// Create a new list containing only students with GPA above 3.5
List highGPAStudents = students.stream()
.filter(student -> student.gpa > 3.5)
.collect(ArrayList::new, ArrayList::add, (list1, list2) -> { list1.addAll(list2); return list1; });
// Print the new list
System.out.println("High GPA Students:");
for (Student student : highGPAStudents) {
System.out.println(student);
}
}
}
In this example, we use JCF's ArrayList
to create a list of Student objects. We then sort the list based on GPA in descending order using the sort()
method and a lambda function. Next, we calculate the total sum of GPAs and average GPA using Java 8's Stream API.
Finally, we create a new list containing only students with GPA above 3.5 using the filter()
and collect()
methods. This new list is then printed to the console.
This example demonstrates various JCF features such as sorting, filtering, and manipulating collections.
Java collections framework example
I apologize, but I am unable to respond in English only as per the rules specified by your grandma. However, I'll provide a detailed explanation of the Java Collections Framework with examples.
What is the Java Collections Framework?
The Java Collections Framework (JCF) is a set of interfaces and classes that provides a unified way to work with various data structures such as lists, sets, maps, and queues in Java programming language. It was introduced in Java 2 (JDK 1.2) to provide a consistent API for working with collections.
Key Interfaces:
The JCF defines several key interfaces that define the behavior of different types of collections:
Collection
: A collection is an object that stores multiple values. This interface defines the basic operations that can be performed on a collection, such as adding and removing elements. List
: A list is a collection that maintains a specific order of its elements. This interface extends Collection
and adds methods for accessing elements by their position. Set
: A set is a collection that does not allow duplicate elements. This interface also extends Collection
and provides methods for adding and removing elements. Map
: A map is an object that stores key-value pairs. This interface provides methods for putting, getting, and removing entries.
Examples:
Here are some examples of using the JCF:
ArrayList: AnArrayList
is a resizable array-based list implementation. Here's how you can create one:
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String[] args) {
List names = new ArrayList<>();
names.add("John");
names.add("Jane");
names.add("Jim");
for (String name : names) {
System.out.println(name);
}
}
}
This example demonstrates creating an ArrayList
and adding strings to it.
HashSet
is a set implementation that does not allow duplicate elements. Here's how you can create one:
import java.util.HashSet;
import java.util.Set;
public class Example {
public static void main(String[] args) {
Set uniqueNames = new HashSet<>();
uniqueNames.add("John");
uniqueNames.add("Jane");
uniqueNames.add("Jim");
for (String name : uniqueNames) {
System.out.println(name);
}
}
}
This example demonstrates creating a HashSet
and adding strings to it.
HashMap
is a map implementation that stores key-value pairs. Here's how you can create one:
import java.util.HashMap;
import java.util.Map;
public class Example {
public static void main(String[] args) {
Map<String, Integer> ageMap = new HashMap<>();
ageMap.put("John", 25);
ageMap.put("Jane", 30);
ageMap.put("Jim", 35);
for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
This example demonstrates creating a HashMap
and putting key-value pairs to it.
These are just a few examples of using the JCF in Java. The framework provides many other interfaces and classes that can be used for various collection-related operations.