您的位置:首页 > 编程语言 > Java开发

Java集合(2) Queue Deque Map 以及 Stream对集合的操作

2018-08-16 19:59 615 查看
版权声明:转载请注明! https://blog.csdn.net/smile_geek_sq/article/details/81746937

Queue 接口 :

队列:是一组操作受限的线性表。

只能在队尾增加队头删除。 先进先出

上边是实现或继承这个接口的接口或实现类,很多类我也不认识

 

唉 ,日了狗,写了半天的,提交了全没了。

再来。。。

下面来个Demo

[code]package day0816;

import java.util.LinkedList;
import java.util.Queue;

public class Demo3 {
public static void main(String[] args) {
Queue<String> q=new LinkedList<>();
//---------------入队
//offer()向队尾加元素,队列
//添加成功为true  失败 add 会异常,offer 则返回false
q.add("aa");
q.add("bb");
q.offer("aa");
q.forEach(System.out::println);
//---------------出队
//返回移除的元素
//remove 失败则异常,poll 失败 返回null
//		System.out.println(q.remove());
//		System.out.println(q.remove());
//		System.out.println(q.poll());
//队列不允许加入null,但是LinkedList特殊可以加入null,但建议不要加null
//		q.add(null);
//循环出队
System.out.println();
//		while(q.size()>0) {
////			System.out.println(q.poll());
//			System.out.println(q.peek());
//		}

}

}

 

再来说一个PriorityQueue
 按照一定的优先级排序。
 默认 Comparable升序排序
 也可以自己 指定 Comparator

 

来个 Demo  

[code]package day0816;

import java.util.PriorityQueue;
import java.util.Queue;

public class Demo4 {
public static void main(String[] args) {
//优先队列
//默认Comparable升序排序
//可以排序的,也可以自己指定
//违背了队列先进先出的规则
Queue<Integer> q=new PriorityQueue<>((o1,o2)->o1-o2);
q.add(22);
q.add(11);
q.add(33);
//		q.forEach(System.out::println);
while(q.size()>0) {
System.out.println(q.poll());
}
}

}

再来说一下  Deque 

Deque接口:
  双端队列
  可以 模拟 队列
            栈

[code]package day0816;

import java.util.ArrayDeque;
import java.util.Deque;

public class Demo5 {
public static void main(String[] args) {
//Deque 接口继承自Queue,模拟队列  *双端*队列
Deque<String> d=new ArrayDeque<>();
d.add("aa");
d.addLast("bb");
d.offer("cc");
d.offerLast("ee");
d.push("aa");
System.out.println(d);
//出队
while(d.size()>0) {
System.out.print(d.poll()+" ");
//			System.out.println(d.pollFirst());
}
d.clear();
System.out.println();
System.out.println("----------------------");
//--------栈------
/*
* 跑跑执行执行,你会觉得很神奇,为什么上边 是按队列方式FIFO 而下边是栈 FILO呢
*  原因就是push操作也是push到最前边,所以很神奇吧,我看了半天这点
*/
d.addFirst("wtf");
d.offerFirst("wtfa");
d.addFirst("aa");
d.push("aaaaa");
System.out.println(d);
while(d.size()>0) {

System.out.print(d.poll()+" ");
//			System.out.println(d.pop());
}

}

}

来个队列处理的Demo:

[code]package day0816;

import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;

class User{
int num;
String name;
public User(int num, String name) {
this.num = num;
this.name = name;
}

}
public class Demo6 {
public static void main(String[] args) {
Queue<User> q=new LinkedList<>();
q.add(new User(1, "张三1"));
q.add(new User(2, "张三2"));
q.add(new User(3, "张三3"));
q.add(new User(4, "张三4"));
q.add(new User(5, "张三5"));
int counts=q.size();
while(q.size()>0) {
counts--;
System.out.println(q.remove().name+"办理业务完成");
System.out.println("还有"+counts+"人排队");
if(counts==0) {
System.out.println("后便没有队伍了");
System.out.println("全部办理完成");
return;
}else {
System.out.println("剩余:");
q.forEach((o1)->System.out.println(o1.num+" "+o1.name));
}
}

}

}

这就是 队列的作用。

下面说 Map接口:

  双列存储,键值对。
  键是唯一的。

 


------------------------------------
HashMap  和 Hashtable

Hashtable :线程安全的,性能低
HashMap  :键唯一,可以存 null键 ,null值;

LinkedHashMap:
      按照 添加的顺序 来维护;

TreeMap:
     默认 自然升序排序
     也可以 按照 自己指定的 方式排序。

HashMap > LinkedHashMap > TreeMap

 

 

下面上个Demo:

[code]package day0816;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Demo7 {
public static void main(String[] args) {
Map<Integer, String> map=new HashMap<>();
map.put(1, "aa");
map.put(2, "sa");
map.put(2, "wtf");
//		System.out.println(map);
//		System.out.println(map.size());
//		System.out.println(map.isEmpty());
//		//查看指定的键在集合中是否存在
//		System.out.println(map.containsKey(1));
//		System.out.println(map.containsValue("wtf"));
////		map.remove(2);
////		System.out.println(map);
//		//获得键的集合
//		map.keySet().forEach(System.out::println);
//		//获得值的集合
//		System.out.println(map.values());
////		map.clear();
////		System.out.println(map);
//		map.put(null, null);
//		//键是唯一的,再存入会覆盖值
//		map.put(null, "1");
//		//HashMap支持空键的存储 但是只能存入一次
//		System.out.println(map);

}

}

下面是 挺重要的 ,如何遍历  map 呢?

[code]		//遍历
//1.map的foreach
map.forEach((key,value)->System.out.println(key+" "+value));
//把key 转成set集合 因为set也是唯一的
map.keySet().iterator().forEachRemaining(System.out::println);
//吧value 转成 Collection 集合
map.values().iterator().forEachRemaining(System.out::println);
//键值对类型  Entry
Set<Entry<Integer,String>> entryset=map.entrySet();
entryset.forEach(e->System.out.println(e));
entryset.forEach(e->System.out.println(e.getKey()+" "+e.getValue()));
//键值对的迭代器
Iterator it=entryset.iterator();
it.forEachRemaining(System.out::println);

下面再来说一下  Java 8新增的 流 对 集合的操作

Stream

对流中的数据 进行  聚集 运算。

统计

一次性的运算
速度快

末端方法:
   得到结果后 就释放了。

中间方法:
   会得到一个“新”的流 ,可以继续其它方法。


 

[code]package day0816;

import java.util.function.IntPredicate;
import java.util.stream.IntStream;

public class Demo10 {
public static void main(String[] args) {
这里 IntStream 是接口 不能new  但是 有个 builder 方法 然后在后面添加元素,最后,build
IntStream is=IntStream.builder().add(11).add(2).add(3).build();
//System.out.println(is.max().getAsInt());
//末端方法用一次就消耗掉了
//System.out.println(is.max().getAsInt());
//		System.out.println(is.sum());
//		System.out.println(is.average().getAsDouble());
//		System.out.println(is.count());
//是否都处于某个范围内
//		System.out.println(is.allMatch(new IntPredicate() {
//
//			@Override
//			public boolean test(int value) {
//				// TODO Auto-generated method stub
//				return value>1;
//			}
//		}));
//Lambda 简写
//		System.out.println(is.allMatch(v->v>1));

//有一个满足范围就true
//		System.out.println(is.anyMatch(v->v>2));

//-----------------------------------------
//中间方法 返回一个新的流
is.filter(v->v>2).forEach(System.out::println);
//is.filter(v->v>2).forEachOrdered(System.out::println);
}

}

再来看看 对集合的操作:

[code]package day0816;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo11 {
public static void main(String[] args) {
List<Integer> list=new ArrayList<>();
Collections.addAll(list, 11,22,3,4,5,6,7,112);
list.stream().filter(v->v<22).forEach(System.out::println);
System.out.println(list.stream().count());
}

}

所以 还挺好用的是吧 。

最后  再来一个 Demo 

 

[code]package day0816;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

class Book{
String name;
int price;
String author;
public Book(String name, int price,String author) {
super();
this.name = name;
this.price = price;
this.author=author;
}

}
public class Demo13 {
public void filter(List<Book> books,Predicate<Book> p) {
for(Book book:books) {
if(p.test(book)) {
System.out.print(book.name+"  "+book.price+" "+book.author);
System.out.println();
}
}
}
public static void main(String[] args) {
List<Book> books=new ArrayList<>();
books.add(new Book("javawtfa", 20,"sq"));
books.add(new Book("java1", 80,"w"));
books.add(new Book("sql", 20,"www"));
books.add(new Book("hbase", 60,"a"));
new Demo13().filter(books, v->v.name.contains("java"));
System.out.println("*********");
new Demo13().filter(books, p->p.name.length()>3);
System.out.println("*********");
new Demo13().filter(books, p->p.author.length()==2&&p.price>=20);
}

}

 

 

 

第一次写的 莫名其妙 消失了,第二次 可能没第一次好。

抱歉,如有不正,敬请指出!

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: