集合输出有四种方式:Iterator,ListIterator,Enumeration,foreach
集合输出绝大部分情况下都会使用 Iterator,Iterator 接口的实例化依靠 Collection 接口的 iterator 方法。
Iterator
import java.util.*;
class Book{
private String title;
private int price;
public Book(String title, int price){
this.title = title;
this.price = price;
}
@Override
public String toString() {
return this.title + " " + this.price;
}
@Override
public int hashCode() {
return Objects.hash(title, price);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final Book other = (Book) obj;
return Objects.equals(this.title, other.title)
&& Objects.equals(this.price, other.price);
}
}
public class Main {
public static void main(String[] args) throws Exception{
// 设置了泛型,从而保证集合中所有的数据类型都一致
List <Book> list = new ArrayList <Book>();
list.add(new Book("java",100));
list.add(new Book("c",89));
list.add(new Book("c++",100));
list.add(new Book("python",100));
list.add(new Book("python",100));
Iterator <Book> iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
ListIterator
Iterator 接口的输出顺序为由前向后,Iterator 的子接口 ListIterator 输出顺序为由后向前。
ListIterator 子接口是专门为 List 子接口定义的输出接口。
ListIterator 想要实现由后向前的输出,则必须先实现由前向后的输出。
import java.util.*;
class Book{
private String title;
private int price;
public Book(String title, int price){
this.title = title;
this.price = price;
}
@Override
public String toString() {
return this.title + " " + this.price;
}
@Override
public int hashCode() {
return Objects.hash(title, price);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final Book other = (Book) obj;
return Objects.equals(this.title, other.title)
&& Objects.equals(this.price, other.price);
}
}
public class Main {
public static void main(String[] args) throws Exception{
// 设置了泛型,从而保证集合中所有的数据类型都一致
List <Book> list = new ArrayList <Book>();
list.add(new Book("java",100));
list.add(new Book("c",89));
list.add(new Book("c++",100));
list.add(new Book("python",100));
list.add(new Book("python",100));
ListIterator <Book> listIteratorator = list.listIterator();
while (listIteratorator.hasNext()){
System.out.println(listIteratorator.next());
}
System.out.println("----------");
while (listIteratorator.hasPrevious()){
System.out.println(listIteratorator.previous());
}
}
}
foreach
import java.util.*;
class Book{
private String title;
private int price;
public Book(String title, int price){
this.title = title;
this.price = price;
}
@Override
public String toString() {
return this.title + " " + this.price;
}
@Override
public int hashCode() {
return Objects.hash(title, price);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final Book other = (Book) obj;
return Objects.equals(this.title, other.title)
&& Objects.equals(this.price, other.price);
}
}
public class Main {
public static void main(String[] args) throws Exception{
// 设置了泛型,从而保证集合中所有的数据类型都一致
List <Book> list = new ArrayList <Book>();
list.add(new Book("java",100));
list.add(new Book("c",89));
list.add(new Book("c++",100));
list.add(new Book("python",100));
list.add(new Book("python",100));
for(Book book : list){
System.out.println(book);
}
}
}
Enumeration
Enumeration 接口的实例化只能依靠 Vector 子类来实现。
import java.util.*;
class Book{
private String title;
private int price;
public Book(String title, int price){
this.title = title;
this.price = price;
}
@Override
public String toString() {
return this.title + " " + this.price;
}
@Override
public int hashCode() {
return Objects.hash(title, price);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final Book other = (Book) obj;
return Objects.equals(this.title, other.title)
&& Objects.equals(this.price, other.price);
}
}
public class Main {
public static void main(String[] args) throws Exception{
// 设置了泛型,从而保证集合中所有的数据类型都一致
Vector <Book> vector = new Vector <Book>();
vector.add(new Book("java",100));
vector.add(new Book("c",89));
vector.add(new Book("c++",100));
vector.add(new Book("python",100));
vector.add(new Book("python",100));
Enumeration <Book> enumeration = vector.elements();
while (enumeration.hasMoreElements()){
System.out.println(enumeration.nextElement());
}
}
}