Home > Java > javaTutorial > body text

Demonstrates a Java program using the collections framework

王林
Release: 2023-09-06 14:37:09
forward
440 people have browsed it

In Java, a collection is an object, or we can say a container, used to combine multiple objects into a unit. The collection interface is at the root of all collection framework interfaces. We can perform various operations on collections such as adding, deleting, iterating, searching and retrieving objects. Please note that they cannot be used with primitive data types (such as int, double). However, Java provides wrapper classes that can use primitive data types as objects. We will use these objects to manipulate the collection interface.

In this article, we will make a Java program that shows the use of Collection interface. We will also discuss several sub-interfaces and methods of the Collection interface.

The Chinese translation of

Collection Interface

is:

Collection Interface

Hierarchical structure of collection interface

Demonstrates a Java program using the collections framework

grammar

Collection<element_Type> collection_name = new Collection<element_Type>();
Or,
Collection<element_Type> collection_name = new Collection<>();
Copy after login

Example

ArrayList<Integer> al = new ArrayList<>();
Copy after login

‘al’ is the name of the array list collection collection_name.

‘Integer’ is the type of element we want to store. Note that this is an object and not a primitive type.

‘ArrayList’ is a collection.

It is important to import the 'java.util' package because the collection interface is available in this package. To import, use the following command -

import java.util.*;
Copy after login

Here, * means that we import all classes and methods in this package into our program.

Let’s discuss the sub-interfaces of the collection interface -

List − It is a sub-interface of the java Collection interface. It is a linear structure where each element is stored and accessed sequentially. To use the features of list, we will use ArrayList and LinkedList classes that implement the list interface.

Set − It is a sub-interface of the java Collection interface and does not allow duplicate values. It is similar to a mathematical set. In order to use the set's features, we will use the hash set class that implements the set interface.

Queue - It provides the features of a queue data structure. The queue follows the first-in-first-out (FIFO) principle.

We will use some built-in methods in our program −

  • add() − Used to add an element to the end of the list.

  • hasNext() − Returns true if the element exists, otherwise returns false.

  • next() − Displays the next element of the list.

  • iterator() − Used to traverse the list.

  • get() − It returns the element at the specified index.

  • size() − It returns the number of elements in the list.

Show the program using Collection interface

The Chinese translation of

Example 1

is:

Example 1

The following program demonstrates the use of the collection interface:

import java.util.*;
public class Listcol {
   public static void main(String[] args) {
      // Creating arraylist 
      ArrayList<Integer> araylist = new ArrayList<>();
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(4);
      araylist.add(7);
      System.out.println("Elements of the Arraylist : " + araylist);
      // Creating Linkedlist 
      LinkedList<Integer> list2 = new LinkedList<>();
      // Adding elements in linkedlist
      list2.add(8);
      list2.add(4);
      list2.add(1);
      list2.add(0);
      System.out.println("Elements of the Linkedlist : " + list2);
      // Creating Set
      HashSet<Integer> set = new HashSet<>();
      // Adding elements in Hashset
      set.add(9);
      set.add(6);
      set.add(1);
      set.add(3);
      System.out.println("Elements of the HashSet : " + set);
      // Creating Queue
      Queue<String> queue = new PriorityQueue<>();
      // Adding elements in queue
      queue.add("Tutorix");
      queue.add("Tutorialspoint");
      queue.add("Tutorial");
      System.out.println("Elements of the Queue : " + queue);
   }
}
Copy after login

Output

Elements of the Arraylist : [8, 5, 2, 9, 4, 7]
Elements of the Linkedlist : [8, 4, 1, 0]
Elements of the HashSet : [1, 3, 6, 9]
Elements of the Queue : [Tutorial, Tutorix, Tutorialspoint]
Copy after login
The Chinese translation of

Example 2

is:

Example 2

Let’s see how to iterate over the elements in a list using a for loop.

import java.util.*;
public class RunningSum {
   public static void main(String[] args) {
      // Creating arraylist 
      ArrayList<Integer> araylist = new ArrayList<Integer>();
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(4);
      araylist.add(7);
      System.out.println("Elements of the list : ");
      // loop to iterate through elements
      for(int i = 0; i < araylist.size(); i++ ) {
         // to print the elements in the list
         System.out.println(araylist.get(i)); 
      }
   }
}
Copy after login

Output

Elements of the list : 
8
5
2
9
4
7
Copy after login
Copy after login

Example 3

In this example, we will see how to iterate over the elements of a list using the Iterator interface.

import java.util.*;
public class Listcol {
   public static void main(String[] args) {
      // Creating arraylist 
      ArrayList<Integer> araylist = new ArrayList<Integer>();
      // Adding elements in arraylist
      araylist.add(8);
      araylist.add(5);
      araylist.add(2);
      araylist.add(9);
      araylist.add(4);
      araylist.add(7);
      System.out.println("Elements of the list : ");
      // creating iterator interface to iterate through elements
      Iterator<Integer> itr = araylist.iterator();
      while (itr.hasNext()) {
         // to print the elements in the list
         System.out.println(itr.next()); 
      }
   }
}
Copy after login

Output

Elements of the list : 
8
5
2
9
4
7
Copy after login
Copy after login

in conclusion

In this article, we have discussed the classes and methods that implement different collection interfaces. We have seen program to understand the use of these interfaces. They are mainly used to group objects. Also, we discussed about iterator interface.

The above is the detailed content of Demonstrates a Java program using the collections framework. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!