(一)
1.Collection接口 主要有 以下 用法;
.booleam add(Object o):该方法用于向集合里面添加一个元素。
.void clear():清除集合里面的所有元素,将集合长度变为0.
.booleam contains(Object o):返回集合里面是否包含指定元素。
.intsize():该方法返回集合里元素的个数
Object[] toArray(): 把集合转换成一个数组,所有的集合元素变成对应的数组元素。
2.代码实现
package Four;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
/**
* @author Administrator
* guanyu
*/
public class CollectionTest {
public static void main(String[] args) {
Collection c = new ArrayList();
/**
* 添加元素
*/
c.add("孙悟空");
c.add(6);
System.out.println(c.size());
c.forEach(System.out::print);
c.remove(6);
System.out.println("----------------------\n");
System.out.println(c.size());
c.forEach(System.out::print);
System.out.println(c.contains("孙悟空"));
c.add("清量型JavaEE 企业应用实践");
System.out.println(c);
Collection books = new HashSet();
books.add("轻量级JavaEE企业应用实践");
books.add("疯狂Java讲义");
System.out.println(c.contains(books));
c.remove(books);
System.out.println(c);
c.clear();
System.out.println(c);
books.retainAll(c);
System.out.println(books);
}
}
3.代码结果图
(二)
1使用Lambda表达式遍历集合
代码图
package Four;
import java.util.Collection;
import java.util.HashSet;
/**
* @author Administrator
* 使用Lambda表达式来遍历集合元素
*/
public class CollectionEach {
public static void main(String[] args) {
Collection books = new HashSet();
books.add("轻量级JavaEE 企业应用实践");
books.add("疯狂Java讲义");
books.add("疯狂Android讲义");
/**
* 调用foreach()方法遍历集合
*/
books.forEach(obj ->System.out.println("迭代集合元素:"+obj) );
}
}
结果图
.booleam hasNest():
.Object next()返回集合中一个元素
void remove();删除集合上次next的元素
代码图
package Four;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
/**
* @author Administrator
* /**
* 创建集合。添加元素的代码与前一个程序相同
* 获取books集合对应的迭代器
*/
public class IteratorTest {
public static void main(String[] args) {
Collection books = new HashSet();
books.add("轻量级JavaEE 企业应用实践");
books.add("疯狂Java讲义");
books.add("疯狂Android讲义");
Iterator it = books.iterator();
while (it.hasNext()){
/**
* it.next()返回的数据类型是Object类型,因此需要强制类型转换
*/
String book =(String)it.next();
System.out.println(book);
if (book.equals("疯狂Android讲义")) {
//从集合中删除上一次next()方法放回的元素
it.remove();
}
//对book变量赋值,不会改变集合元素本身
book = "测试字符串";
}
System.out.println(books);
}
}
结果图
(三)
用法代码图
System.out.println输出的时候只能输出一个
package Four;
import java.util.Arrays;
import java.util.stream.IntStream;
/**
* @author Administrator
*/
public class IntStreamTest {
public static void main(String[] args) {
IntStream.Builder is = IntStream.builder();
// is.add(20);
is.add(13);
is.add(15);
is.add(18);
// System.out.println(is.build().max().getAsInt());
//System.out.println(is.build().min().getAsInt());
// System.out.println(is.build().sum());
// System.out.println(is.build().count());
//System.out.println(is.build().average().getAsDouble());
//System.out.println(is.build().allMatch(value -> value * value > 20));
// IntStream newIs = is.build().map(operand -> operand*2+1);
// newIs.forEach(System.out::println);
// int[] array = is.build().toArray();
// System.out.println(Arrays.toString(array));
// System.out.println(is.build().findFirst().getAsInt());
System.out.println(is.build().findAny().getAsInt());
}
}
package Four;
import java.util.Collection;
import java.util.HashSet;
import java.util.stream.IntStream;
/**
* @author Administrator\
* 使用Stream来改写Collection
*/
public class CollectionStream {
public static void main(String[] args) {
Collection books = new HashSet();
books.add(new String("轻量级Java EE企业应用实践"));
books.add(new String("疯狂Java讲义"));
books.add(new String("疯狂iOS讲义"));
books.add(new String("疯狂ajax讲义"));
// System.out.println(books.stream().filter(o -> ((String) o).contains("疯狂")).count());
// System.out.println(books.stream().filter(o -> ((String) o).contains("Java")).count());
// System.out.println(books.stream().filter(o -> ((String) o).length() > 10).count());
//先调用collection对象的Stream()方法的将集合转换为Stream
//在调用Stream的MapToInt方法获取原有的Stream对应的IntStream
books.stream().mapToInt(value -> ((String)value).length())
.forEach(System.out::println);
// books.stream().mapToInt(value -> ((String) value).length());
//程序只需要调用Collection对象的Stream()方法即可返回该集合对应的Stream,接下来就可以通过Stream提供
//的方法 对所有集合元素进行处理,这样大大的简化了集合编程的代码,这也是Stream编程带来的优势
/**
* 上面程序中最后一段粗体代码先调用了Collection对象的Stream()方法将集合准换为Stream对象,然后调用
* Stream对象的MapToInt()方法将其转换为IntStream-这个MapToInt()方法就是一个中间方法,因此
* 程序可继续调用InStream的forEach()方法来遍历流中的元素
*
*/
}
}