我有一个类似 classe User{ String Department; 的类;字符串地址;我有用户列表,我可以使用 java 8 Stream/map/collect 获得以下输出吗
List<User> userList=getUserList();
Map<String,List<String>> userAddressMap=new HashMap<String,List<String>>();
for(User user : userList){
List<String> addressList=userAddressMap.get(user.getDepartment());
if(addressList==null){
userAddressMap.put(user.getDepartment(),addressList);
}
addressList.add(user.getAddress);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试对中的getPrice每个方法进行并行调用。我有这段代码并验证了 getPrice 正在单独的线程中运行,但它们是按顺序运行的,而不是并行运行的。谁能指出我在这里缺少什么吗?productproducts
非常感谢你的帮助。
ExecutorService service = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
Set<Product> decoratedProductSet = products.stream()
.map(product -> CompletableFuture
.supplyAsync(() -> getPrice(product.getId(), date, context), service))
.map(t -> t.exceptionally(throwable -> null))
.map(t -> t.join())
.collect(Collectors.<Product>toSet());
Run Code Online (Sandbox Code Playgroud) 我从简单的 Web 应用程序迁移到 Maven Web 应用程序,并使用 Eclipse Neon 遇到了一个令人沮丧的问题:在 pom.xml 中添加使用 Java 8 或 7 的规范后,它不起作用。
为了验证它是否有效,我编写了一个简单的类,在其中使用了 try(表达式) 声明。
我应该怎么做才能在 Maven 中使用 Java 8(我已经安装并且它可以在正常的 Web 应用程序中工作)?
pom.xml的代码是这样的:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.webdata</groupId>
<artifactId>WebParser</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>WebParser</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.23</version>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud) 我有一个小问题,就是从java中的数组中找到最大偶数和。例如:在数组 {1,2,3,4,5,6} 中,最大偶数和应为 6(1+2+3),10( 中的 16(1+2+3+4+6) 1+2+3+4) 和 16。
但到目前为止,我得到 10 作为输出,因为它将 sum 作为连续的总和。请帮我解决一下。
我收到“不兼容的类型,必需:CopyOnWriteArrayList,找到:对象”,内容如下。我正在使用 IntelliJ 2016.1.1。
CopyOnWriteArrayList<Foo> l = fields.stream()
.distinct()
.collect(toCollection(CopyOnWriteArrayList::new));
Run Code Online (Sandbox Code Playgroud) 我必须编写程序,该程序应该读取字谜文件并显示单词+他的字谜。Txt文件很大,使用扫描仪后,listOfWords大小为:25000。
输出示例:
word anagram1 anagram2 anagram3 ...
word2 anagram1 anagram2...
Run Code Online (Sandbox Code Playgroud)
我有代码,它可以工作,但速度很慢:
private static List<String> listOfWords = new ArrayList<String>();
private static List<ArrayList<String>> allAnagrams = new ArrayList<ArrayList<String>>();
public static void main(String[] args) throws Exception {
URL url = new URL("www.xxx.pl/textFile.txt");
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
String nextToken = scanner.next();
listOfWords.add(nextToken);
}
scanner.close();
while (listOfWords.isEmpty() == false) {
ArrayList<String> anagramy = new ArrayList<String>();
String wzor = listOfWords.remove(0);
anagramy.add(wzor);
char[] ch = wzor.toCharArray();
Arrays.sort(ch);
for (int i = 0; i < …Run Code Online (Sandbox Code Playgroud) 在 ReentrantLock 和 StampedLock 之间进行选择的用例应该是什么?例如,如果我有 10 个读者和 10 个写者,应该选择哪种锁?如果我有 20 位读者和 1 位作者,该选择哪一个?
build.gradle 配置\xef\xbc\x9a
\n\napply plugin: \'com.android.application\'\napply plugin: \'realm-android\'\n//apply plugin: \'android-apt\'\n\nandroid {\n compileSdkVersion 25\n buildToolsVersion "25.0.0"\n defaultConfig {\n applicationId "com.way.event"\n minSdkVersion 21\n targetSdkVersion 25\n versionCode 1\n versionName "1.0"\n testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"\n jackOptions {\n enabled true\n }\n }\n compileOptions {\n sourceCompatibility JavaVersion.VERSION_1_8\n targetCompatibility JavaVersion.VERSION_1_8\n }\n buildTypes {\n release {\n minifyEnabled false\n proguardFiles getDefaultProguardFile(\'proguard-android.txt\'), \'proguard-rules.pro\'\n }\n }\n}\n\ndependencies {\n compile fileTree(include: [\'*.jar\'], dir: \'libs\')\n androidTestCompile(\'com.android.support.test.espresso:espresso-core:2.2.2\', {\n exclude group: \'com.android.support\', module: \'support-annotations\'\n })\n compile \'com.android.support:appcompat-v7:25.0.0\'\n compile \'com.android.support:design:25.0.0\'\n compile \'com.android.support:support-v4:25.0.0\'\n testCompile \'junit:junit:4.12\'\n}\nRun Code Online (Sandbox Code Playgroud)\n\n它不支持 Java8\xef\xbc\x9a
\n\n\n …
我有两个简单的 POJO:
public class Parent {
String name;
List<Child> children = new ArrayList<>();
void addChild(Integer age) { children.add(new Child(age)); }
}
public class Child {
Integer age;
}
Run Code Online (Sandbox Code Playgroud)
然后我生成一些数据,为父级 100 个子级添加:
List<Parent> parentList = new ArrayList<>();
Parent first = new Parent("first");
for (int i = 0; i < 100; ++i) { first.addChild(i); }
parentList.add(first);
Run Code Online (Sandbox Code Playgroud)
我的目标是消除所有十岁以下的儿童:
parentList.stream().forEach(parent -> {
parent.setChildren(getChildrenOlderThan(parent));
});
List<Child> getChildrenOlderThan10(Parent parent) {
return parent.getChildren().stream()
.filter(child -> child.getAge() > 10)
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
最后我列出了十岁以上的孩子。但我不想编写一个单独的方法 getChildrenOlderThan。
如何仅使用单个流让所有列出十岁以上儿童的父母?
我在类构造函数中的反射与继承一起使用时遇到问题。具体来说,我想获取所有属性值。
这是一个简单实现的演示,但不起作用:
import java.lang.reflect.Field;
public class SubInitProblem {
public static void main(String[] args) throws IllegalAccessException {
Child p = new Child();
}
}
class Parent {
public int parentVar = 888888;
public Parent() throws IllegalAccessException {
this.showFields();
}
public void showFields() throws IllegalAccessException {
for (Field f : this.getClass().getFields()) {
System.out.println(f + ": " + f.get(this));
}
}
}
class Child extends Parent {
public int childVar = 999999;
public Child() throws IllegalAccessException {
super();
}
}
Run Code Online (Sandbox Code Playgroud)
这将显示childVar为零: …
java-8 ×10
java ×7
java-stream ×4
collectors ×2
anagram ×1
android ×1
arrays ×1
collections ×1
concurrency ×1
constructor ×1
eclipse ×1
inheritance ×1
locking ×1
maven ×1
realm ×1