Java 展示了集合框架的使用
在Java中,集合是一个对象,或者可以说是一个容器,它允许我们将多个对象组合成一个单元。集合接口位于所有集合框架接口的根部。我们可以对集合执行多种操作,如添加、删除、迭代、搜索和检索对象。请注意,它们不能与原始数据类型(如int、double)一起工作。然而,Java提供了包装类,使原始数据类型能够作为对象使用。我们将使用这些对象来使用集合接口。
在本文中,我们将制作一个展示集合接口使用的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接口的ArrayList和LinkedList类。
Set - 它是java Collection接口的子接口,不允许重复的值。它类似于数学集合。为了使用集的功能,我们将使用实现了set接口的HashSet类。
Queue - 它提供了队列数据结构的功能。队列遵循先进先出(FIFO)的方式。
我们将在程序中使用的一些内置方法 –
- add() - 用于将单个元素添加到列表的末尾。
-
hasNext() - 如果元素存在,则返回true;否则返回false。
-
next() - 显示列表的下一个元素。
-
iterator() - 用于迭代列表。
-
get() - 返回指定索引的元素。
-
size() - 返回列表中的元素数量。
使用Colllection接口的程序示例
示例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]
示例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
在这个示例中,我们将看到如何使用迭代器接口来迭代列表中的元素。
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
结论
在这篇文章中,我们讨论了实现不同集合接口的类和方法。我们已经看到程序来理解这些接口的使用。它们主要用于组织对象。此外,我们还讨论了迭代器接口。