Queue接口

  • 队列: 是访问受限的线性表

  • 先进先出的数据结构

    1. 添加元素从队尾添加
    2. 删除元素从队头删除

方法

添加 删除 获取列表头元素 操作失败时
add remove element 会产生异常
offer poll peek 不会产生异常,而是返回特定的值。

实现类

LinkedList类

  • 队列不允许添加null,但是linkedList可以添加
  1. add(e)
    向集合添加元素:(向后添加)
    成功: 返回true;
    失败: 返回异常
  2. offer(e)
    向集合添加元素:(向后添加)
    成功: 返回true;
    失败: 返回false
  3. remove()
    向集合删除元素:(第一个)
    成功: 返回true;
    失败: 返回异常
  4. poll()
    向集合删除元素:(第一个)
    成功: 返回true;
    失败: 返回null
  5. element()//不删除查看元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Queue<String> q = new LinkedList<>();
q.add("aa");
q.add("bb");
q.offer("cc");
q.forEach(System.out::println);

//出队---------------------------
System.out.println(q.remove());
System.out.println(q.poll());

//循环出队 -----------------------------------
while(q.size() > 0) {
System.out.println(q.poll());//移除,查看第一个
System.out.println(q.peek());//第一个没有走,死循环
}

PriorityQueue类

  1. 优先队列
  2. 违背了队列先进先出的规则
  3. 使用该类不要使用集合的forEach遍历,看不到效果

默认构造使用自然升序优先级(comparable)
带参(comparator)
**不能使用list的forEach();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public static void main(String[] args) {
// 优先队列
Queue<Integer> q = new PriorityQueue<>((n1, n2) -> n2 - n1);
q.add(22);
q.add(55);
q.add(11);
// 没有效果
// q.forEach(System.out::println);

// 遍历
while (q.size() > 0) {
System.out.println(q.poll());
}
}

逆序输出:
55
22
11

Deque 接口

  1. 双端队列

  2. 模拟栈(后进先出):

  3. addFirst—getFirst();

  4. addLast—-getLast()

  5. 是Queue的子类

方法

添加 删除 获取列表头元素 操作失败时
addFirst removeFirst getFirst 会产生异常
addLast removeLast getLast 会产生异常
offerFirst pollFist peekFirst 不会产生异常,而是返回特定的值。
offerLaset pollLast peekLast 不会产生异常,而是返回特定的值。

栈的方法:

  • push 入栈:添加
  • pop 弹栈:删除

ArrayDeque

模拟队列

  • 队列(队尾进,队头出)
1
2
3
4
5
入队		出队
add() remove()
offer() poll()
addLast() removeFirst()
offerLast() pollFirst()
1
2
3
4
5
6
7
8
9
10
11
12
//模拟 队列
Deque<String> d = new ArrayDeque<>();
d.add("aa");
d.addLast("bb");
d.offer("cc");
d.offerLast("dd");
System.out.println(d);
//出队
while(d.size() > 0) {
// System.out.println(d.poll());
System.out.println(d.pollFirst());
}

模拟栈

  • 栈(从上入栈,从上出栈)
1
2
3
入栈		出栈
addFirst() pollFirst()
push() pop()
1
2
3
4
5
6
7
8
9
10
11
Deque<String> d = new ArrayDeque<>();
//入栈
d.addFirst("aa");
d.offerFirst("bb");
d.addFirst("cc");
d.push("dd");
//出栈
while(d.size() > 0) {
// System.out.println(d.pollFirst());
System.out.println(d.pop());
}

Stream 流

  1. java.util.* 包下,1.8出现
  2. 对流中的数据进行聚集运算。
  3. 一次性的运算
  4. 速度快
  • 使用:

    • IntStream
    • LongStream
    • DoubleStream

聚集方法

  1. max():获取流中的最大值
  2. min()
  3. sum()
  4. average()
  5. count()
  6. allMatch()是否所有元素符合条件
  7. anyMatch()是否至少包含一个元素
  8. filter: 过滤器,中间方法,返回的是一个新的流,可以继续调用方法

以上的方法使用一次后(比如求玩最大值后,流对象就不能继续使用)

解决办法:使用一次后重新创建一次流

流的创建

IntStream

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

    • 创建

      1
      2
      IntStream  is = IntStream.build();
      IntStream  is = IntStream.builder().add(11).build();
    • 示例

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      public static void main(String[] args) {
      IntStream is = IntStream.builder().add(11).add(22).add(33).add(55).build();
      // 末端方法-------------------------------------------
      // System.out.println(is.max().getAsInt()); //55
      // System.out.println(is.min().getAsInt()); //11
      // System.out.println(is.sum()); //121
      // System.out.println(is.average().getAsDouble()); //30.25
      // System.out.println(is.count());

      // 带条件的流统计
      // all 所有 的 数据 都满足条件 返回 true
      // allMatch(): 是否所有元素符合条件
      is.allMatch(new IntPredicate() {

      @Override
      public boolean test(int value) {
      return value>20;
      }
      });

      System.out.println(is.allMatch(v->v > 10)); //true:流中的所有元素大于10

      //any 流中只要有一个元素满足条件 就返回true
      System.out.println(is.anyMatch(v-> v > 50)); //true
      }
  • 中间方法:得到一个新的流,可以进行新的操作

    1
    2
    IntStream is = IntStream.builder().add(11).add(22).add(33).add(55).build();
    is.filter(v-> v>20).forEach(System.out::println);//中间方法

集合使用流

集合转换为流

  • stream():将集合的元素转换为流

    1
    2
    3
    4
    5
    6
    7
    8
    public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();
    Collections.addAll(list, 11, 454, 44, 6878, 23);
    list.stream().filter(v -> v < 55).forEach(System.out::println);
    System.out.println(list.stream().filter(v -> v < 55).count());
    //
    list.stream().forEach(System.out::println);
    }

Predicate 接口

  • 过滤器的接口

  • 使用的位置:

    • 流中的filter ()方法
    • 集合中的removeIf()方法
  • 例子:过滤学生年龄在30岁以上并且名字中包含”g”的学生

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    // 学生类
    class Student {
    private String name;
    private int age;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }

    public Student(String name, int age) {
    super();
    this.name = name;
    this.age = age;
    }

    public Student() {
    super();
    // TODO Auto-generated constructor stub
    }

    @Override
    public String toString() {
    return "Student [name=" + name + ", age=" + age + "]";
    }

    }
  • 测试过滤器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    // 测试过滤器
    public class TestPredicate1 {

    public void showStu(List<Student> stus, Predicate<Student> p) {
    for (Student stu : stus) {
    if (p.test(stu)) {
    System.out.println(stu);
    }
    }
    }

    public static void main(String[] args) {
    List<Student> stus = new ArrayList<>();
    stus.add(new Student("zhangsan", 44));
    stus.add(new Student("lisi", 55));
    stus.add(new Student("wangwu", 22));
    new TestPredicate1().showStu(stus, t -> t.getAge() > 30 && t.getName().contains("g"));
    }

    }