Home >Java >javaTutorial >Introduction to container applications in Java language
Java is a widely used programming language that provides many flexible tools and processes to help developers build efficient applications. In Java, containers are some important tools that help developers store and manage data.
Container is one of the most common concepts in Java, which refers to the data structure used to store and manage objects. Java's container library contains many different container types and classes, each container provides some different functions and usage methods. In this article, we will introduce several common container classes in Java.
ArrayList is one of the most commonly used containers in Java. It is a dynamic array that can store different types of objects. It is different from ordinary arrays in Java in that ArrayList can automatically adjust its size as needed, so it is very convenient when using it.
Using ArrayList is very simple. First, we need to create an ArrayList object and then add elements to it. Here is some sample code:
ArrayList<String> list = new ArrayList<String>(); list.add("Java"); list.add("C++"); list.add("Python"); for(String s : list) { System.out.println(s); }
In this example, we create an ArrayList object and add three elements of type String to it. We can then use a for-each loop to iterate through all elements in the list.
LinkedList is another common container in Java. It is also a data structure that can store different types of objects. LinkedList differs from ArrayList in that it is a doubly linked list, so adding and removing elements is faster than ArrayList.
The following is a sample code using LinkedList:
LinkedList<Integer> list = new LinkedList<Integer>(); list.add(1); list.add(2); list.add(3); for(Integer i : list) { System.out.println(i); }
In this example, we create a LinkedList object and add three elements of type Integer to it. Then, we use a for-each loop to iterate through all elements.
HashMap is a very useful container in Java that can store key-value pairs. This container class can look up values based on keys, so it's ideal for mapping and lookup operations.
The following is a sample code using HashMap:
HashMap<String, Integer> map = new HashMap<>(); map.put("Java", 1); map.put("C++", 2); map.put("Python", 3); System.out.println(map.get("Java"));
In this example, we create a HashMap object and add three key-value pairs to it. Then, we printed the value with the key "Java" using the get method.
TreeSet is an ordered container in Java that can store an ordered collection of elements. All elements must be comparable, and the objects used for sorting must implement the Comparable interface.
The following is a sample code using TreeSet:
TreeSet<String> set = new TreeSet<String>(); set.add("Java"); set.add("C++"); set.add("Python"); for(String s : set) { System.out.println(s); }
In this example, we create a TreeSet object and add three elements of type String to it. Since the String type has implemented the Comparable interface, TreeSet can sort them. We then iterated through the entire collection using a for-each loop.
Summary
This article introduces several common container classes in Java, including ArrayList, LinkedList, HashMap and TreeSet. These container classes help developers better manage and store data, thereby improving application efficiency and reliability. When using these container classes, make sure you understand how they are used and what they do to better serve your application.
The above is the detailed content of Introduction to container applications in Java language. For more information, please follow other related articles on the PHP Chinese website!