我正在尝试通过(映射)进行分组,然后将值列表转换为不同的列表.
我有DistrictDocuments列表:
List<DistrictDocument> docs = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
然后我在它上面流式传输并按城市分组:
Map<String, List<DistrictDocument>> collect = docs.stream()
.collect(Collectors.groupingBy(DistrictDocument::getCity));
Run Code Online (Sandbox Code Playgroud)
我还有一个方法,它采用了DistrictDocument并从中创建了Slugable:
private Fizz createFizz(DistrictDocument doc) {
return new Fizz().name(doc.getName()).fizz(doc.getFizz());
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将该方法放入我的流中,所以我得到了Map<String, List<Fizz>>
?我尝试将第二个参数添加到groupingBy但是找不到合适的方法并且总是遇到编译错误.
编辑:如果我的createFizz返回List<Fizz>
怎么办?是否可以选择在Collectors.mapping中平展此列表,因为我仍然想要Map<String, List<Fizz>>
而不是Map<String, List<List<Fizz>>>
我有两个应用程序 - app1和app2,其中app1是app2的config server
配置.我在app1中定义了端点,需要等到它返回状态才能启动app2的 pod ./readiness
OK
至关重要的是app2的部署要等到app1中的/ readyiness端点kubernetes
接收Http Status OK
,因为它是配置服务器,并且为app2保留了重要的配置.
是否可以执行此类部署依赖性?
我在替换这个特定的例子时遇到了问题:
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
}
};
Run Code Online (Sandbox Code Playgroud)
是否可以用lambda替换它,因为它使用DefaultConsumer的非默认构造函数?
它来自rabbitMQ java教程 - > LINK到全班
是否可以在运行时编译和实例化Kotlin类?我正在谈论类似的事情,但是使用Kotlin API:如何以编程方式编译和实例化Java类?
例如:我正在获得完整的类定义为String:
val example = "package example\n" +
"\n" +
"fun main(args: Array<String>) {\n" +
" println(\"Hello World\")\n" +
"}\n"
Run Code Online (Sandbox Code Playgroud)
然后将其插入某个class.kt中并运行它,以便在运行时在控制台中打印“ Hello World”。
我正在创建 rest api 并实现 Spring Security - 一切正常,但我希望(目前,当我仍在开发时)能够让任何人未经授权打开 localhost:8080/console。我的代码:
@Override
protected void configure(HttpSecurity http) throws Exception {
// allow everyone to register an account; /console is just for testing
http.authorizeRequests().antMatchers("/register", "/console").permitAll();
http.authorizeRequests().anyRequest().fullyAuthenticated();
// making H2 console working
http.headers().frameOptions().disable();
/*
https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#when-to-use-csrf-protection
for non-browser APIs there is no need to use csrf protection
*/
http.csrf().disable();
}
Run Code Online (Sandbox Code Playgroud)
真正奇怪的是 - localhost:8080/register 不需要任何身份验证但 /console 返回:
{
"timestamp": 1485876313847,
"status": 403,
"error": "Forbidden",
"message": "Access Denied",
"path": "/console"
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何修复它?
我尝试将 Id 字段用作 Long 并收到此错误:
ERROR: Error CREATEing SolrCore 'brands': Unable to create core
[brands] Caused by: uniqueKey field (id) can not be configured to use a
Points based FieldType: plong
Run Code Online (Sandbox Code Playgroud)
这是我的 schema.xml:
<?xml version="1.0" encoding="UTF-8"?>
<schema name="brands-config" version="1.6">
<uniqueKey>id</uniqueKey>
<fieldType name="plong" class="solr.LongPointField" docValues="true"/>
<fieldType name="string" class="solr.StrField" sortMissingLast="true" docValues="true"/>
<field name="name" type="string"/>
<field name="id" type="plong" multiValued="false" indexed="true" required="true" stored="true"/>
</schema>
Run Code Online (Sandbox Code Playgroud)
我的 solrconfig.xml 是基本的 - 仅删除了该线程中提到的部分,但没有帮助。
有什么办法可以让我的 id 字段类型变长吗?我需要这种方式来避免太多的映射。
尝试将我的项目从Spring Boot 1.5.8更新到2.0.1,并遇到Quartz问题。我使用了Spring BOM,并具有Quartz 2.3.0版本。
这是我的QuartzConfigurer,它为Spring设置了bean:
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import javax.annotation.PostConstruct;
@Slf4j
@Configuration
public class QuartzConfigurer {
@Value("${quartz.cron.contract-status}")
private String contractCron;
@Value("${quartz.config-filename}")
private String quartzConfigFileName;
@Autowired
private ApplicationContext applicationContext;
@PostConstruct
public void init() {
log.debug("Initialization of quartz configurer");
}
@Bean
public SpringBeanJobFactory springBeanJobFactory() {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean
public SchedulerFactoryBean scheduler(Trigger …
Run Code Online (Sandbox Code Playgroud) 我的 Java 项目中有 2 个分支:master 和 refactor。我已经完成了重构的工作,所以现在想checkout master
将重构合并到 master 中。在进行重构时,我还向 .gitignore 添加了一些文件(其中一个是 .idea),现在我得到:
[michal@michal-pc MCleaner]$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
.idea/description.html
.idea/misc.xml
.idea/modules.xml
.idea/project-template.xml
.idea/vcs.xml
Please move or remove them before you switch branches.
Aborting
Run Code Online (Sandbox Code Playgroud)
我已经阅读了很多帖子,但没有任何效果。如何在不访问 master 分支的情况下删除这些文件?有没有办法解决这个问题?如果可以,请提供 cmd 命令,我还是 git 新手。
这是来自的输出git status
:
On branch refactor
Your branch is up-to-date with 'origin/refactor'.
Untracked files: (use "git add <file>..." to include in what will be committed) …
Run Code Online (Sandbox Code Playgroud) 上周在一个流中出现了非常奇怪的NPE,这让我遇到了很多麻烦,所以现在我觉得在使用Stream时我对NPE过于安全.
这是我现在的方法:
private boolean matchSomeError(final List<ErrorAtMessageLevel> errorList) {
return errorList.stream()
.filter(errorAtMessageLevel -> errorAtMessageLevel.getErrorSegment() != null && errorAtMessageLevel.getErrorSegment().getErrorDetails() != null)
.map(errorAtMessageLevel -> errorAtMessageLevel.getErrorSegment().getErrorDetails())
.anyMatch(errorDetails -> SOME_FANCY_ERROR_CODE.equals(errorDetails.getErrorCode()));
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我在这里处理外部POJO所以我无法更改它并使其为null安全所以我必须调整我的代码.
这里有一些限制:1)errorList - 这里不能为null所以调用.stream()
是安全的 - 当它为空时它将返回false 2)getErrorSegment()
并且getErrorDetails()
都可以为null这就是为什么我使用这样的过滤器以确保它们都不是是null 3)getErrorCode()
可以为null但它永远不会抛出NPE,因为它只会在与null匹配时返回false - 很好.
你会如何改善这条小溪?我觉得我.filter()
很糟糕,可以做得更好.最近写了很多这样的代码,因为我不确定流是如何处理null并且不想让NPE进入,.map()
因为它被调用为null
我想创建一个接收DataFiles List的方法,然后在其中一个字段上使用String谓词过滤它,并返回DataFiles的List而不是字符串.
这是我的代码:
public static List<DataFile> someMethod(List<DataFile> dataFiles) {
return dataFiles
.stream()
.map(DataFile::getFileName)
.filter(companyAndDateFilter(date, BLABLA, LALALA))
.collect(Collectors.toList());
}
private static Predicate<String> companyAndDateFilter(LocalDate date, String... companyNames) {
String companies = getCompaniesAsString(companyNames);
String formattedDate = formatTheDate(date);
Pattern pattern = Pattern
.compile("^(" + companies + ")_(" + formattedDate + ")");
LOGGER.info("Filtering files using RegEx: " + pattern.pattern());
return pattern.asPredicate();
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,.map(DataFile::getFileName)
我将我的流转换为,Stream<String>
但我想保留它,Stream<DataFile>
并仍然使用过滤器(即Predicate<String>
)DataFile::getFileName
.那可能吗?
java ×5
java-stream ×3
java-8 ×2
spring ×2
git ×1
git-checkout ×1
git-merge ×1
h2 ×1
kotlin ×1
kubernetes ×1
lambda ×1
lucene ×1
solr ×1
spring-boot ×1