Java Collections Framework: Working with Lists, Sets, and Maps

Java Collections Framework: Working with Lists, Sets, and Maps

The #Java #Collections #Framework is a set of classes and interfaces that implement commonly reusable collection data structures. Although referred to as a framework, it works in a manner of a library. The collections framework provides both interfaces that define various collections and classes that implement them.

Lists

A list is a collection that maintains a linear order of its elements. Lists are indexed, so you can access elements by their index. Lists are also mutable, so you can add, remove, and change elements in a list.

The most common implementation of a list is the ArrayList class. ArrayList is a dynamically sized array, so it can grow or shrink as needed.

Here is an example of how to create a list and add elements to it:

List<String> names = new ArrayList<>();
names.add("John");
names.add("Mary");
names.add("Susan");        

You can access elements in a list by their index:

String name = names.get(0); // "John"        

You can also iterate over a list using a for-each loop:

for (String name : names) {
  System.out.println(name);
}        

Sets

A set is a collection that does not allow duplicate elements. Sets are also unordered, so you cannot access elements by their index.

The most common implementation of a set is the HashSet class. HashSet uses a hash table to store elements, so it can be very efficient for lookup operations.

Here is an example of how to create a set and add elements to it:

Set<String> names = new HashSet<>();
names.add("John");
names.add("Mary");
names.add("Susan");        

You can check if an element exists in a set using the contains method:

boolean containsJohn = names.contains("John"); // true        

You can also iterate over a set using a for-each loop:

for (String name : names) {
  System.out.println(name);
}        

Maps

A map is a collection that associates keys with values. Maps are also unordered, so you cannot access elements by their index.

The most common implementation of a map is the HashMap class. HashMap uses a hash table to store elements, so it can be very efficient for lookup operations.

Here is an example of how to create a map and add elements to it:

Map<String, String> names = new HashMap<>();
names.put("John", "Doe");
names.put("Mary", "Smith");
names.put("Susan", "Jones");        

You can get the value associated with a key using the get method:

String johnsName = names.get("John"); // "Doe"        

You can also iterate over a map using a for-each loop:

for (String key : names.keySet()) {
  String value = names.get(key);
  System.out.println(key + " = " + value);
}        

Conclusion

The Java Collections Framework provides a wide variety of collection classes and interfaces that can be used to store and manipulate data. The framework is designed to be flexible and extensible, so you can choose the right collection for your needs.

I hope this blog post has been helpful. If you have any questions, please feel free to leave a comment below.

Check Here for Java Course

要查看或添加评论,请登录

Manjeet Singh的更多文章

社区洞察

其他会员也浏览了