public static void main(String args[])
{
ArrayList<Item> arrayList3 = new ArrayList<Item>();
ArrayList<Item> arrayList4 = new ArrayList<Item>();
Item item1 = new Item();
item1.setId(1L);
item1.setBreed("2");
item1.setSpec("2");
item1.setBrand("3");
Item item4 = new Item();
item4.setId(2L);
item4.setBreed("2");
item4.setSpec("2");
item4.setBrand("3");
// Item item16= new Item();
// item16.setBreed("2");
// item16.setSpec("3");
// item16.setBrand("3");
arrayList3.add(item1);
arrayList3.add(item4);
// arrayList3.add(item16);
Item item2 = new Item();
item2.setId(1L);
item2.setBreed("22");
item2.setSpec("2");
item2.setBrand("3");
Item item3 = new Item();
item3.setId(22L);
item3.setBreed("22");
item3.setSpec("2");
item3.setBrand("3");
arrayList4.add(item2);
arrayList4.add(item3);
// 将对象为空的排除
list2.parallelStream().filter(Objects::nonNull).collect(Collectors.toList());
//假设有两个List,一个是list1,另一个是list2,它们都有一个相同的属性名为“id”,我们想要从list1中排除那些在list2中也存在的id的元素,可以使用以下代码:
List<MyObject> result = list1.stream()
.filter(obj -> list2.stream().noneMatch(o -> o.getId() == obj.getId()))
.collect(Collectors.toList());
/**
* 多条件去重
*/
arrayList4.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(o -> ((Item) o).getBreed() + ";" + ((Item) o).getSpec()))), ArrayList::new));
// 根据某一个属性 去重
arrayList4.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Item::getId))), ArrayList::new));
//两个属性相乘(加减乘除),乘积再累加reduce
Double ss = arrayList3.stream().reduce(0.0, (x, y) -> x + (y.getId() * y.getId()), Double::sum);
//两个属性相乘(加减乘除),乘积再累加reduce
Double axx = arrayList3.stream().mapToInt(x-> x.getId() * x.getId()).sum();
// 只取一个属性集合
List<String> breeds= arrayList3.stream().map(Item:: getBreed).collect(Collectors.toList());
// 找到id最大 arrayList3.stream().collect(Collectors.maxBy(Comparator.comparing(Item::getId))).get();
// 判断对象的某个属性值是否为空,为空给默认值
String string = Optional.ofNullable(item1).map(x -> x.getBreed()).orElse("1");
System.out.println(string);
// 集合id属性 最大的值(max方法同理)
Long long2 = arrayList3.stream().map(Item::getId).collect(Collectors.maxBy(Long::compareTo)).get();
System.out.println(long2);
// 集合id属性 最小的值
Long long3 = arrayList3.stream().map(Item::getId).collect(Collectors.minBy(Long::compareTo)).get();
System.out.println(long3);
// 平均值
Double collect8 = arrayList3.stream().collect(Collectors.averagingLong(x -> x.getId()));
System.out.println(collect8);
// 根据某一属性 比较 拿出最大的对象
Item max2 = arrayList3.stream().max((p1, p2) -> p1.getId().compareTo(p2.getId())).get();
System.out.println(max2);
// 根据某一属性 比较 拿出最小的对象
Item min = arrayList3.stream().min((p1, p2) -> p1.getId().compareTo(p2.getId())).get();
System.out.println(min);
// 根据某个属性 获取总和
Long long1 = arrayList3.stream().map(Item::getId).reduce(Long::sum).get();
System.out.println(long1);
// 根据id 从小到大排序
List<Item> collect7 = arrayList3.stream().sorted(Comparator.comparing(Item::getId)).collect(Collectors.toList());
// 根据id 排序 从大到小排序
List<Item> collect9 = arrayList3.stream().sorted(Comparator.comparing(Item::getId).reversed()).collect(Collectors.toList());
System.out.println("collect9: " + collect9);
System.out.println(collect7);
// 两个list 根据id 找出并集
List<Item> collect6 = arrayList3.stream().filter(a ->
arrayList4.stream().map(b -> b.getId()).collect(Collectors.toList()).
contains(a.getId())).collect(Collectors.toList());
System.out.println(collect6);
// 两个list 根据多个属性 找出并集
List<Item> collect61 = arrayList3.stream().filter(a ->
arrayList4.stream().map(b -> b.getId()+b.getBreed()).collect(Collectors.toList()).
contains(a.getId()+a.getBreed())).collect(Collectors.toList());
System.out.println(collect61);
// 根据 两个 list 相同 的属性 。将 4的 复制给 3
List<Item> list111 = arrayList3.stream()
.map(item -> arrayList4.stream()
.filter(girl -> item.getId().equals(girl.getId()))
.findFirst()
.map(girl -> {
item.setBreed(girl.getBreed());
return item;
}).orElse(item))
.collect(Collectors.toList());
System.out.println(list111);
// 去重
List<Item> myList = arrayList4.stream().distinct().collect(Collectors.toList());
System.out.println(myList.size());
// 去重
List<Item> collect4 = Stream.of(arrayList4,arrayList3).flatMap(Collection::stream).distinct().collect(Collectors.toList());
System.out.println(collect4.size());
long count4 = arrayList3.stream().filter(arrayList4::contains).count();
System.out.println(count4);
// list 根据条件筛选
List<Item> list2 = arrayList3.stream().filter(x -> x.getBreed().equals("1")).collect(Collectors.toList());
System.out.println(list2);
List<Item> list3 = arrayList3.stream().filter(x -> x.getBreed().equals("2")).collect(Collectors.toList());
System.out.println(list3);
// 多个条件筛选
List<Item> list5 = arrayList3.stream().filter(x -> x.getBreed().equals("1") && x.getId().equals(1)).collect(Collectors.toList());
System.out.println(list5);
// 筛选总
long count2 = arrayList3.stream().filter(x -> x.getBreed().equals("1")).count();
long count3 = arrayList3.stream().filter(x -> x.getBreed().equals("2")).count();
System.out.println(count2);
System.out.println(count3);
StringBuffer stringBuffer = new StringBuffer();
// 循环
arrayList3.stream().forEach(p -> stringBuffer.append(p.getBrand() +" "));
System.err.println(stringBuffer.toString().trim());
// 找出符合条件的一个 orElse 没有给默认
String brand = arrayList3.stream().filter(a -> a.getBrand().equals("wwww")).findAny().orElse(new Item()).getBrand();
System.out.println(brand);
Item item6 = new Item();
// 如果不为空
Optional.ofNullable(item4).ifPresent(
a -> item6.setBrand(item4.getBrand()));
System.out.println(item6);
Map<String, Object> param1 = JSONObject.parseObject(JSONObject.toJSONString(item1));
System.out.println(param1);
// isPresent
boolean present = Optional.ofNullable(item1).isPresent();
System.out.println(present);
Double double1 = 0.0;
Double double2 = 0.0;
System.out.println(double1 == double2);
Integer integer1 = 127;
Integer integer2 = 127;
System.out.println(integer1 == integer2);
Integer integer3 = 128;
Integer integer4 = 128;
System.out.println(integer3 == integer4);
ArrayList<Integer> arrayList1 = new ArrayList<Integer>();
arrayList1.add(1);
arrayList1.add(2);
arrayList1.add(3);
arrayList1.add(3);
// 总和
Integer reduce = arrayList1.stream().reduce(0, (x, y) -> x + y);
System.out.println(reduce);
// list 有一个满足条件
boolean anyMatch = arrayList1.stream().anyMatch(a -> a.equals(1));
System.out.println("-----------------1-----------------");
System.out.println(anyMatch);
System.out.println("-----------------1-----------------");
// 全部都满足条件
boolean allMatch = arrayList1.stream().allMatch(a -> a.equals(1));
System.out.println(allMatch);
// 变 set集合
Set<Integer> collect = arrayList1.stream().collect(Collectors.toSet());
System.out.println(collect);
System.out.println("----------------------------------");
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList1.stream().filter(p -> p == 2).forEach(d ->arrayList.add(d));;
System.err.println(arrayList);
arrayList2.add("1");
arrayList2.add("2");
// 要判断 集合1 的内容
boolean retainAll = arrayList2.retainAll(arrayList1);
System.out.println(retainAll);
System.err.println(String.format("%,f", 8888.00));
String format = String.format("%04d",new Object[] { Integer.valueOf(1) });
System.out.println(format);
ArrayList<User> list = new ArrayList<User>();
User user1 = new User(1,"1");
// user1.setAge(1);
// user1.setName("1");
User user2 = new User(2,"2");
// user2.setAge(2);
// user2.setName("2");
User user3 = new User(3,"3");
// user2.setAge(3);
// user2.setName("3");
list.add(user1);
list.add(user2);
list.add(user3);
list.stream().forEach(System.out :: println);
ArrayList<User> list4 = new ArrayList<User>();
list.stream().forEach(x ->{
list4.add(x);
});
Map<String, Object> param = JSONObject.parseObject(JSONObject.toJSONString(user1));
System.out.println("----------------------------------");
System.out.println(param);
System.out.println("----------------------------------");
List<Integer> collect5 = list.stream().map(User::getAge).collect(Collectors.toList());
System.out.println("----------------------------------");
System.err.println(collect5);
System.out.println("----------------------------------");
Map<Integer, User> collect3 = list.stream().collect(Collectors.toMap(User::getAge, Function.identity()));
System.out.println(collect3);
Map<Integer, String> collect2 = list.stream().collect(Collectors.toMap(User::getAge, User::getName));
System.out.println(collect2);
Optional<User> max = list.stream().max((p1,p2) -> p1.getAge().compareTo(p1.getAge()));
System.out.println(max.get());
boolean b = list.stream().allMatch(p -> p.getAge() == 1 || p.getAge() == 2);
System.out.println(b);
}
类:
class Item{
Long id;
String breed;
String spec;
String brand;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public String getSpec() {
return spec;
}
public void setSpec(String spec) {
this.spec = spec;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((brand == null) ? 0 : brand.hashCode());
result = prime * result + ((breed == null) ? 0 : breed.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((spec == null) ? 0 : spec.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item other = (Item) obj;
if (brand == null) {
if (other.brand != null)
return false;
} else if (!brand.equals(other.brand))
return false;
if (breed == null) {
if (other.breed != null)
return false;
} else if (!breed.equals(other.breed))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (spec == null) {
if (other.spec != null)
return false;
} else if (!spec.equals(other.spec))
return false;
return true;
}
@Override
public String toString() {
return "Item [id=" + id + ", breed=" + breed + ", spec=" + spec
+ ", brand=" + brand + "]";
}
}
class User{
Integer age;
String name;
public User() {}
public User(Integer age, String name) {
super();
this.age = age;
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "user [age=" + age + ", name=" + name + "]";
}