我有一个Parent具有20个属性的Java类(attrib1, attrib2 .. attrib20)及其相应的getter和setter.我还有两个Parent对象列表:list1和list2.
现在我想合并两个列表并避免基于attrib1和的重复对象attrib2.
使用Java 8:
List<Parent> result = Stream.concat(list1.stream(), list2.stream())
.distinct()
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但是我必须在哪个地方指定属性?我应该覆盖hashCode和equals方法吗?
Parent是Child继承的类.这是由GrandChild继承的.每个类都包含子类的List(即Parent包含Child和Child的List包含GrandChild的List).每个类包含50个属性(attrib1-atrib50).getChildList()返回类型为Child的对象的arrayList getGrandChildList()返回GrandChild类型的对象的arrayList
设resultSet为Parent列表
List<Parent> resultSet
Run Code Online (Sandbox Code Playgroud)
现在我想根据一些属性对列表进行排序.例如,如果我想基于两个父属性(比如属性1和属性2)对resultSet进行排序,我使用此代码.
Comparator<Parent> byFirst = (e1, e2) -> e2.getAttrib1().compareTo(e1.getAttrib1());
Comparator<Parent> bySecond = (e1, e2) -> e1.getAttrib2().compareTo(e2.getAttrib2());
Comparator<Parent> byThird = byFirst.thenComparing(bySecond);
List<Parent> sortedList = resultSet.stream().sorted(byThird).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
现在我想根据Child类的属性1和GrandChild类的属性1对父列表进行排序.我应该如何排序呢.
List<List<String>> superlist = new ArrayList<List<String>>();
List<String> list1 = new ArrayList<String>();
list1.add("a1");
list1.add("a2");
List<String> list2 = new ArrayList<String>();
list2.add("b1");
list2.add("b2");
List<String> list3= new ArrayList<String>();
list3.add("c1");
list3.add("c2");
superlist.add(list1);
superlist.add(list2);
superlist.add(list3);
List<String> result= new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个包含所有值的新列表superList.这里的结果应该包含a1,a2,b1,b2,c1,c2
让我们考虑一个Parent只包含一个Integer属性的类.我创建了6个父类对象,属性值为100, 20, 300, 400, 500, null.
现在我将所有对象添加到列表中(列表名称是列表).然后我想得到属性值大于的对象100.我为此目的使用了Java 8流.
Predicate<Entity> predicate = e -> e.getParentId() < 100;
result = list.stream().filter(predicate).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我还想按降序对列表进行排序.我为此目的使用了以下代码.
Comparator<Entity> comp = (d1,d2) -> d2.getId().compareTo(d1.getId());
list.sort(comp);
Run Code Online (Sandbox Code Playgroud)
在这两种情况下我都得到了NullPointerException.
怎么办呢?
让我们考虑一个Parent只包含一个Integer属性的类.我创建了6个父类对象,并将一个对象作为null.然后我将这些对象添加到列表中.
我想通过Integer属性值检索相应的对象.我使用Java 8 Streams.
Predicate<Parent> predicate = e -> e.getId() == 100; // sample attribute value
result = list.stream().filter(predicate).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但我得到了NullPointerException,所以我编辑了代码:
list.stream().filter(h -> h!=null).filter(predicate).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但是如果任何对象为null,我想抛出异常.如果列表中没有对象为null,那么我想从列表中检索相应的对象.
如何使用Java 8 Streams使用单个语句实现此目的?
考虑Parent具有属性的类attrib1,attrib2以及List<Child>具有相应的getter和setter的child.
这Child是另一个具有五个属性的类attrib1- attrib5具有相应的getter和setter.
现在我创建了一个List<Parent>父母.然后我想过滤掉List<Parent>以下条件: - Child.Attrib1 > 10;
所以我用Java 8流创建了以下查询.
parent.stream().filter(e -> e.getChild().stream().anyMatch(c -> c.getAttrib1() > 10));
Run Code Online (Sandbox Code Playgroud)
但问题是我会在每个Parent对象中获得所有孩子.在这里,我想只获得那些List<Child>符合给定条件的子对象.
如何删除List中不遵守该条件并获取新列表的所有子对象.
假设我有两个列表:
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(1, 2, 4, 5);
Run Code Online (Sandbox Code Playgroud)
现在我想表演(list1 - list2).预期的ouptut是{3}.如何使用java 8流做到这一点?
让我们考虑一下java中的一个类
class Entity {
Integer id;
Integer parentId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
}
}
Run Code Online (Sandbox Code Playgroud)
将parentId视为外键(与另一个对象的id相关).
现在我创建了6个对象并放置了一些值.
Entity e1 = new Entity();
e1.setId(400);
Entity e2 = new Entity();
e2.setId(300);
e2.setParentId(400);
Entity e3 = new Entity();
e3.setId(200);
e3.setParentId(300);
Entity e4 = new Entity();
e4.setId(100);
e4.setParentId(200);
Entity e5 = new Entity();
e5.setId(50);
e5.setParentId(100);
Entity e6 …Run Code Online (Sandbox Code Playgroud) List<String> result = map.entrySet()
.stream()
.map(Map.Entry::getValue)
.flatMap(x -> x.stream())
.collect(Collectors.toCollection(ArrayList::new));
Run Code Online (Sandbox Code Playgroud)
上面的代码将创建一个不是线程安全的ArrayList.那么如何才能使其线程安全.