我想知道两种平面图之间是否存在任何显着差异.
情况1:
someCollection
.stream()
.map(CollectionElement::getAnotherCollection)
.flatMap(Collection::stream);
Run Code Online (Sandbox Code Playgroud)
案例2:
someCollection
.stream()
.flatMap(element -> element.getAnotherCollection().stream());
Run Code Online (Sandbox Code Playgroud)
应该首选哪一个?在性能方面有哪些更好?
考虑我有一个列表,其中包含两种类型的数据,一种是有效的,另一种是无效的.
如果我开始过滤此列表,我可以在最后收集两个列表吗?
我想根据过滤器收集物品.但如果未找到匹配项,则不应初始化结果列表.我更喜欢null而不是空列表.
List<String> match = list
.stream()
.filter(item -> item.getProperty == "match")
.collect(Collectors.toList());
if (match != null && !match.isEmpty()) {
//handle seldom match
}
Run Code Online (Sandbox Code Playgroud)
问题:大多数时候我没有匹配,导致空集合.这意味着即使我不需要,列表也会在大多数情况下实例化.
我有以下代码块:
Optional<Integer> result = //some method that returns an Optional<Integer>;
if(result.isPresent()) {
return result.get();
} else {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的IntelliJ建议我用功能表达式替换它.我看到里面有一个方法ifPresentOrElse(),Optional但我无法弄清楚如何在这种特殊情况下使用它.
有什么建议?谢谢!
假设我有一个Reports流,我有一个方法可以检查用户是否有权读取每个报告.如果用户没有权限,此方法将抛出异常
checkReadAuthorization(Report report) throw AccessViolationException;
Run Code Online (Sandbox Code Playgroud)
有没有办法过滤掉此方法引发异常的所有报告?就像是:
reports.stream()
.filterThrowingException(this::checkReadAuthorization);
Run Code Online (Sandbox Code Playgroud)
现在我有一个函数,如果抛出异常则返回true或false但是我想知道是否有更好的方法来实现这个
private boolean checkAuthorization(Report report) {
try {
checkReadAuthorization(report);
return true;
} catch(AccessViolationException ex) {
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在这样的过滤器中使用它
return dossiers.stream()
.filter(this::checkAuthorization)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 假设我有一个字符串列表
["bird", "bird", "dog", "dog", "bird","bog"]
Run Code Online (Sandbox Code Playgroud)
我希望它们以形式收集为地图
{"bird": [0, 1, 4], "dog": [2, 3, 5]}
Run Code Online (Sandbox Code Playgroud)
作为列表值是单词在输入列表中的索引.
有没有办法用Java Streams做到这一点?
有人可以解释我为什么我不能在List by .stream()中过滤我的字符串.过滤器?我收到的只是未经过滤的字符串!我会很感激的.
public static void main(String[] args) throws FileNotFoundException {
List<Country> countries = CsvToBeanConverterCountry("country.csv");
countries
.stream().filter(s -> s.getName().contains("xx")).collect(Collectors.toList());
countries.forEach(s -> System.out.println(s.getName()));
}
private static List CsvToBeanConverterCountry(String csvFilename) throws FileNotFoundException {
CsvToBean csv = new CsvToBean();
CSVReader csvReader = new CSVReader(new FileReader(csvFilename), ';', '"', 1);
List list = csv.parse(setColumMappingCountry(), csvReader);
return list;
}
private static ColumnPositionMappingStrategy setColumMappingCountry() {
ColumnPositionMappingStrategy strategy = new ColumnPositionMappingStrategy();
strategy.setType(Country.class);
String[] columns = new String[]{"country_id", "city_id", "name"};
strategy.setColumnMapping(columns);
return strategy;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个
List<Map<String, Object>>
Run Code Online (Sandbox Code Playgroud)
来自Spring NamedParameterJdbcTemplate queryForList调用.数据返回如下所示:
[{"id":5,"uid":6}, {"id":5,"uid":7}, {"id":6,"uid":8}, {"id":7,"uid":7}, {"id":8,"uid":7}, {"id":8,"uid":9}]
Run Code Online (Sandbox Code Playgroud)
如何按以下格式重新排列数据?
{5:[6, 7], 6:[8], 7:[7], 8:[7, 9]}
Run Code Online (Sandbox Code Playgroud)
我想回来一个 Map<Integer, List<Integer>>
任何人都知道如何实现这一目标?任何帮助非常感谢?
出于某些原因,我需要一个新对象来包装我从其他API获得的数据.
我遇到的问题是不知道从原始对象处理递归字段.
以下是示例代码:
//original object
public class Resource{
private String name;
private String content;
private Integer type;
private List<Resource> children = new LinkedList();
public void addChildrenResource(Resource children) {
this.children.add(children);
}
//getters&setters...
}
Run Code Online (Sandbox Code Playgroud)
//the object try to convert
public class Menu{
private String name;
private String url;
private Integer type;
private List<Menu> subMenu = new LinkedList();
public void addChildrenResource(Menu subMenu) {
this.children.add(subMenu);
}
//getters&setters...
}
Run Code Online (Sandbox Code Playgroud)
我做的实现,我不知道与递归字段...
这是我的代码..
List<Resource> resources = rpcService.getResources();
//Here I can't handle the subMenu or children part..
List<Menu> …Run Code Online (Sandbox Code Playgroud) 据我所知,并行流使用默认值ForkJoinPool.commonPool,默认情况下,线程数比处理器少一个.我想使用自己的自定义线程池.
像这样:
@Test
public void stream() throws Exception {
//System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "20");
ForkJoinPool pool = new ForkJoinPool(10);
List<Integer> testList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
long start = System.currentTimeMillis();
List<Integer> result = pool.submit(() -> testList.parallelStream().map(item -> {
try {
// read from database
Thread.sleep(1000);
System.out.println("task" + item + ":" + Thread.currentThread());
} catch (Exception e) {
}
return item * 10;
})).get().collect(Collectors.toList());
System.out.println(result);
System.out.println(System.currentTimeMillis() …Run Code Online (Sandbox Code Playgroud) java-stream ×10
java ×9
java-8 ×7
collections ×1
exception ×1
flatmap ×1
grouping ×1
list ×1
recursion ×1