首页 > 文章列表 > 展示使用集合框架的Java程序

展示使用集合框架的Java程序

java程序 集合框架 展示
214 2023-09-16

在Java中,集合是一个对象,或者我们可以说是一个容器,用于将多个对象组合成一个单元。集合接口位于所有集合框架接口的根部。我们可以对集合执行各种操作,如添加、删除、迭代、搜索和检索对象。请注意,它们不能与原始数据类型(如int、double)一起使用。然而,Java提供了包装类,可以将原始数据类型作为对象使用。我们将使用这些对象来操作集合接口。

在本文中,我们将制作一个显示Collection接口使用的Java程序。我们还将讨论Collection接口的几个子接口和方法。

Collection Interface

的中文翻译为:

集合接口

集合接口的层次结构

展示使用集合框架的Java程序

语法

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

实例

ArrayList<Integer> al = new ArrayList<>();

‘al’ 是数组列表集合 collection_name 的名称。

‘Integer’是我们要存储的元素类型。请注意,这是一个对象而不是原始类型。

‘ArrayList’ 是集合。

重要的是导入'java.util'包,因为集合接口在这个包中可用。要导入,请使用以下命令 -

import java.util.*;

在这里,* 表示我们将这个包中的所有类和方法导入到我们的程序中。

让我们讨论集合接口的子接口 -

List − 它是java Collection接口的子接口。它是一种线性结构,按顺序存储和访问每个元素。要使用list的特性,我们将使用实现list接口的ArrayList和LinkedList类。

Set − 它是java Collection接口的子接口,不允许重复的值。它类似于数学集合。为了使用set的特性,我们将使用实现set接口的hash set类。

队列 - 它提供了队列数据结构的特性。队列遵循先进先出(FIFO)的原则。

我们将在我们的程序中使用一些内置方法 −

  • add() − 用于在列表末尾添加一个元素。

  • hasNext() − 如果元素存在,则返回true,否则返回false。

  • next() − 显示列表的下一个元素。

  • iterator() − 用于遍历列表。

  • get() − 它返回指定索引的元素。

  • size() − 它返回列表中元素的数量。

展示使用Collection接口的程序

Example 1

的中文翻译为:

示例 1

下面的程序演示了集合接口的使用:

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);
   }
}

输出

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]

Example 2

的中文翻译为:

示例2

让我们看看如何使用for循环迭代列表中的元素。

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)); 
      }
   }
}

输出

Elements of the list : 
8
5
2
9
4
7

示例3

<p>在这个例子中,我们将看到如何使用Iterator接口迭代遍历列表的元素。</p>
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()); 
      }
   }
}

输出

Elements of the list : 
8
5
2
9
4
7

结论

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.