问题:Java面试知识点:红黑树、Stream流
答案:
1.红黑树
红黑树
• 平衡二叉B树
• 每一个节点可以是红或者黑
• 红黑树不是高度平衡的,它的平衡是通过“自己的红黑规则”进行实现的
2.Stream流
Stream流的常见中间操作方法
• Stream<T> limit(long maxSize) : 截取指定参数个数的数据
• Stream<T> skip(long n):跳过指定参数个数的数据
• static <T> Stream<T> concat(Stream a, Stream b):合并a和b两个流为一个流
• Stream<T> distinct() :去除流中重复的元素。依赖(hashCode和equals方法)
在流中无法直接修改数据源中的数据。
代码如下:
public class test15Stream {public static void main(String[] args) {ArrayList<String> list=new ArrayList<>();list.add("杨大大");list.add("恒大大");// Stream<String> stream=list.stream();
// stream.forEach(s-> System.out.println(s));list.stream().forEach(s-> System.out.println(s));HashMap<String,Integer> map=new HashMap<>();map.put("杨大大",22);map.put("恒大大",22);Set<String> keySet = map.keySet();keySet.stream().forEach(s-> System.out.println(s));Set<Map.Entry<String, Integer>> entries = map.entrySet();entries.stream().forEach(s-> System.out.println(s));int[] arr={1,2};Arrays.stream(arr).forEach(s-> System.out.println(s));Stream.of(1,2).forEach(s-> System.out.println(s));list.stream().filter(new Predicate<String>() {@Overridepublic boolean test(String s) {boolean b = s.startsWith("杨");return b;}}).forEach(s-> System.out.println(s));list.stream().filter(s->{boolean b = s.startsWith("杨");return b;}).forEach(s-> System.out.println(s));list.stream().filter(s->s.startsWith("杨")).forEach(s-> System.out.println(s));list.stream().limit(2).forEach(s-> System.out.println(s));list.stream().skip(1).forEach(s-> System.out.println(s));}
}
public class test16Stream {public static void main(String[] args) {ArrayList<String> list=new ArrayList<>();list.add("杨大大");list.add("恒大大");Stream.of(1,2).forEach(s-> System.out.println(s));list.stream().filter(new Predicate<String>() {@Overridepublic boolean test(String s) {boolean b = s.startsWith("杨");return b;}}).forEach(s-> System.out.println(s));list.stream().filter(s->{boolean b = s.startsWith("杨");return b;}).forEach(s-> System.out.println(s));list.stream().filter(s->s.startsWith("杨")).forEach(s-> System.out.println(s));list.stream().limit(2).forEach(s-> System.out.println(s));list.stream().skip(1).forEach(s-> System.out.println(s));ArrayList<String> list1=new ArrayList<>();list1.add("杨大大");list1.add("恒大大");Stream.concat(list.stream(),list1.stream()).forEach(s-> System.out.println(s));list1.add("恒大大");//forEach方法的底层,会循环获取到流中的每一个数据,//并循环调用accept方法,并把每一个数据传递给accept方法//s就依次表示了流中的每一个数据,//所以,我们只要在accept方法中,写上处理的业务逻辑就可以了。list1.stream().distinct().forEach(s-> System.out.println(s));long count = list1.stream().count();System.out.println(count);}
}
public class test17Stream {public static void main(String[] args) {ArrayList<Integer> list=new ArrayList<>();for (int i = 1; i <= 10; i++) {list.add(i);}list.stream().filter(new Predicate<Integer>() {@Overridepublic boolean test(Integer integer) {return integer%2==0;}}).forEach(s-> System.out.println(s));list.stream().filter(integer -> integer%2==0).forEach(s-> System.out.println(s));List<Integer> collect = list.stream().filter(integer -> integer % 2 == 0).collect(Collectors.toList());System.out.println(collect);Set<Integer> collect1 = list.stream().filter(integer -> integer % 2 == 0).collect(Collectors.toSet());System.out.println(collect1);}
}
public class Actor {private String name;@Overridepublic String toString() {return "Actor{" +"name='" + name + '\'' +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public Actor(String name) {this.name = name;}public Actor() {}
}public class test18Stream {public static void main(String[] args) {ArrayList<String> list = new ArrayList<>();list.add("杨大大,22");list.add("恒大大,23");Map<String, Integer> collect = list.stream().filter(s -> {String[] split = s.split(",");if (Integer.parseInt(split[1]) > 22) {return true;};return false;}).collect(Collectors.toMap((String s) -> {return s.split(",")[0];},(String s) -> {return Integer.parseInt(s.split(",")[1]);}));System.out.println(collect);ArrayList<String> actors=new ArrayList<>();actors.add("杨洋");actors.add("李易峰");actors.add("易烊千玺");actors.add("胡歌");ArrayList<String> actors1=new ArrayList<>();actors1.add("杨幂");actors1.add("杨紫");actors1.add("杨颖");actors1.add("迪丽热巴");Stream<String> stream = actors.stream().filter(s -> s.length() == 3).limit(2);Stream<String> stream1 = actors1.stream().filter(s -> s.startsWith("杨")).skip(1);Stream.concat(stream,stream1).forEach(name->{Actor actor=new Actor(name);System.out.println(actor);});}
}