java setblack_Java集合

1.Java集合的概述:

为了保存数量不确定的数据,以及保存具有映射关系的数据(也成关联数组),Java提供了集合类,集合类主要负责保存,盛装其他数据,因此集合类也被称为容器类。

Java集合主要由Collection和Map两个接口派生而出,时Java集合框架的根接口,这两个接口又包含了一些子接口或实现类:

b004b1590a8c1cb57bdc8782d06a4b92.png

504beeb89dac397b6721cc9a1c1cf864.png

在图8.1中Set和List是collection接口派生的两个子接口,分别代表无序集合和有序集合,Queue是Java提供的队列的实现

图8.2 是map体系的继承树,所有map实现类用于保存具有映射关系的数据,Map保存的每项数据都是key-value对,且key是不可以重复的,key用于表示集合里每项数据

根据以上两个图,可以把Java所有几何分成三大类:Set ,List, Map.

2.Collection和Iterator接口

Collection是LIst,Set,Queue接口的父接口,里面定义了如下操作集合元素的方法:

280d4a12d7d89610f6387820e22ff2cf.png

20761d90bc78a9bf0d5ac315ec44085c.png

2.1使用Lambda表达式遍历集合:

Java 8 为Iterable接口新增了一个forEach(Consumer action)默认方法,该方法所需参数的类型是一个函数式接口,而Iterable接口是Collection接口的父接口,因此Collection集合也可以直接调用该方法。

当程序调用Iterable的forEach(Consumer action)遍历集合元素时,程序会依次将集合元素传给Consumer的accept(T t)方法(该接口中唯一的抽象方法)。正因为Consumer是函数式接口,因此可以使用Lambda表达式来遍历集合元素。

1 packagemcy;

2

3 importjava.util.Collection;

4 importjava.util.HashSet;

5

6 public classCollectionEach {

7 public static voidmain(String []args){

8 Collection books = new HashSet();

9

10 books.add("hello world");

11 books.add("hello world1");

12 books.add("hello world2");

13 books.add("hello world3");

14

15 books.forEach(obj -> System.out.println("迭代集合元素 "+obj));

16 }

17 }

18 //输出为:

19 迭代集合元素 hello worlda

20 迭代集合元素 hello worldb

21 迭代集合元素 hello worldc

22 迭代集合元素 hello worldd

2.2使用Java 8增强的Iterator遍历集合元素

java 8 为Iterator新增了一个forEachRemaining(Consumer action)方法,该方法所需的Consumer参数同样也是函数式接口。当程序调用Iterator的forEachRemaining(Consumer action)遍历集合元素时,程序会一次将集合元素传递给Consumer的accept(T t)方法(该接口中唯一的抽象方法)。Iterator主要用于遍历,也称迭代器.

Iterator接口里定义了如下四个方法:

1.boolean hasNext(): 如果被迭代的集合元素还没有遍历完,返回true

2.Object next(): 返回集合里的下一个元素。

3.void remove(): 删除集合里上一次next方法返回的元素

4.void forEachRemaining(Consumer action)

packagemcy;

importjava.util.Collection;

importjava.util.HashSet;

importjava.util.Iterator;

public classIteratorTest {

public static voidmain(String []args){

Collection books = new HashSet();

books.add("hello worlda");

books.add("hello worldb");

books.add("hello worldc");

books.add("hello worldd");

Iterator it =books.iterator();

while(it.hasNext()){

String book =it.next();

System.out.println(book);

if(book.equals("hello worlda")){

it.remove();

}

}

System.out.println(books);

}

}

//输出:

hello worlda

hello worldb

hello worldc

hello worldd

[hello worldb, hello worldc, hello worldd] //hello worlda被删除了

注意:Iterator仅用于遍历集合,Iterator本身并不提供盛装对象的能力,如果需要创建Iterator对象,则必须有一个被迭代的集合,没有集合的Iterator无存在的价值

Iterator必须依赖于Collection对象,若有一个Iterator对象则必然有一个与之关联的Collection对象

2.3使用Lambda表达式遍历Iterator

java 8 为Iterator新增了一个forEachRemaining(Consumer action)方法,该方法所需的Consumer参数同样也是函数式接口。当程序调用Iterator的forEachRemaining(Consumer action)遍历集合元素时,程序会一次将集合元素传递给Consumer的accept(T t)方法(该接口中唯一的抽象方法)

packagemcy;

importjava.util.Collection;

importjava.util.HashSet;

importjava.util.Iterator;

public classIteratorTest {

public static voidmain(String []args){

Collection books = new HashSet();

books.add("hello worlda");

books.add("hello worldb");

books.add("hello worldc");

books.add("hello worldd");

Iterator it =books.iterator();

it.forEachRemaining(obj -> System.out.println("迭代元素: " +obj));

}

}

//输出:

迭代元素: hello worlda

迭代元素: hello worldb

迭代元素: hello worldc

迭代元素: hello worldd

2.4使用foreach循环遍历集合元素

除了使用Iterator接口迭代访问Collection集合里的元素之外,使用Java5提供的foreach循环更加便捷

packagemcy;

importjava.util.Collection;

importjava.util.HashSet;

importjava.util.Iterator;

public classIteratorTest {

public static voidmain(String []args){

Collection books = new HashSet();

books.add("hello worlda");

books.add("hello worldb");

books.add("hello worldc");

books.add("hello worldd");

for(Object obj : books){

System.out.println(obj);

}

}

}

//输出:

hello worlda

hello worldb

hello worldc

hello worldd

2.5使用Java 8新增的Predicate操作集合

Java 8 为Collection 集合新增了一个removeIF(Predicate filter)方法,该方法将会批量删除符合filter条件的所有元素,该方法需要一个Predicate(谓词)对象作为参数,Predicate也是函数接口,因此可使用Lambda表达式作为参数

packagemcy;

importjava.util.Collection;

importjava.util.HashSet;

public classIteratorTest {

public static voidmain(String []args){

Collection books = new HashSet();

books.add("hello");

books.add("hello a");

books.add("hello aaaaaaa");

books.add("hello bbbbbbbbbbb");

books.removeIf(ele -> ((String)ele).length()<10);

System.out.println(books);

}

}

输出:

[hello bbbbbbbbbbb, hello aaaaaaa] //长度小于10的被过滤掉了

Predicate可以简化集合的运算:

packagemcy;

importjava.util.Collection;

importjava.util.HashSet;

importjava.util.function.Predicate;

public classIteratorTest {

public static voidmain(String []args){

Collection books = new HashSet();

books.add("hello a");

books.add("hello b");

books.add("hello ccccc");

books.add("hello bbb");

System.out.println(calAll(books,ele -> ((String)ele).contains("hello a")));

System.out.println(calAll(books,ele -> ((String)ele).contains("hello bbb")));

System.out.println(calAll(books,ele -> ((String)ele).length()>10));

}

public static intcalAll(Collection books,Predicate p){

int total = 0;

for(Object obj: books){

if(p.test(obj)){//Predicate的test()方法判断该对象是否满足Predicate指定的条件

total++;

}

}

returntotal;

}

}

//输出:

1

1

1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值