正在浏览这里Java 8提到的功能.无法理解究竟是什么.有人可以解释一下和之间的实际区别是什么?parallelSort()sort()parallelSort()
Java 8引入了一个类似Scala的Stream的Stream类,这是一个功能强大的惰性结构,使用它可以非常简洁地执行这样的操作:
def from(n: Int): Stream[Int] = n #:: from(n+1)
def sieve(s: Stream[Int]): Stream[Int] = {
s.head #:: sieve(s.tail filter (_ % s.head != 0))
}
val primes = sieve(from(2))
primes takeWhile(_ < 1000) print // prints all primes less than 1000
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能在Java 8中这样做,所以我写了这样的东西:
IntStream from(int n) {
return IntStream.iterate(n, m -> m + 1);
}
IntStream sieve(IntStream s) {
int head = s.findFirst().getAsInt();
return IntStream.concat(IntStream.of(head), sieve(s.skip(1).filter(n -> n % head != 0)));
}
IntStream primes …Run Code Online (Sandbox Code Playgroud) 我有一个日期时间属性的JSON,格式为"2014-03-10T18:46:40.000Z",我想使用Gson将其反序列化为java.time.LocalDateTime字段.
当我尝试反序列化时,我收到错误:
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING
Run Code Online (Sandbox Code Playgroud) 什么是receiverJava中的参数?Java 8语言规范谈论this.
当我需要一堆无状态实用程序方法时,Java 8中的最佳实践是什么.是不是有将一个接口不会被任何人,即实现public interface Signatures和public interface Environments,或者是更好的做旧的方式-有public final class Signatures和public final class Environments私人构造|| 枚举?
让我们说我们必须使用这段无聊的代码:
ArrayList<Long> ids = new ArrayList<Long>();
for (MyObj obj : myList){
ids.add(obj.getId());
}
Run Code Online (Sandbox Code Playgroud)
切换到Java 8后,我的IDE告诉我我可以用这个代码替换collect call它,并自动生成:
ArrayList<Long> ids = myList.stream().map(MyObj::getId).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但是它给了我这个错误:
Steam中的collect(java.util.stream.Collector)无法应用于:(java.util.stream.Collector,capture,java.util.List>)
我试图铸造参数,但它给我不确定的A和R,而IDE没有给任何建议.
我很好奇你collect call在这种情况下如何使用,我找不到任何可以指导我的信息.任何人都可以光明吗?
我现在正在学习lambda,我想知道如何用lambda一行编写这段代码.
我有一个Person包括ID和name字段的类
目前,我有一个List<Person>存储这些Person对象.我想要完成的是获取一个由人的id组成的字符串.
"id1,id2,id3".
我怎样才能用lambda实现这个目标?
A HashMap有一个来自它的文档的短语:
如果初始容量大于最大条目数除以加载因子,则不会发生重新加载操作.
注意文档怎么说的老调重弹,没有调整 -即使翻版只会当调整大小会发生; 那就是当桶的内部大小增加两倍时.
当然HashMap,我们可以提供这样一个构造函数来定义这个初始容量.
使用指定的初始容量和默认加载因子(0.75)构造一个空的HashMap.
好的,似乎很容易:
// these are NOT chosen randomly...
List<String> list = List.of("DFHXR", "YSXFJ", "TUDDY",
"AXVUH", "RUTWZ", "DEDUC", "WFCVW", "ZETCU", "GCVUR");
int maxNumberOfEntries = list.size(); // 9
double loadFactor = 0.75;
int capacity = (int) (maxNumberOfEntries / loadFactor + 1); // 13
Run Code Online (Sandbox Code Playgroud)
所以容量是13(内部是16- 下一个2的幂),这样我们保证文档部分不重复.好吧,让我们测试一下,但首先介绍一个方法,进入HashMap并查看值:
private static <K, V> void debugResize(Map<K, V> map, K key, V value) throws Throwable …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用sparkjava.com框架为我的apache spark作业构建一个web api.我的代码是:
@Override
public void init() {
get("/hello",
(req, res) -> {
String sourcePath = "hdfs://spark:54310/input/*";
SparkConf conf = new SparkConf().setAppName("LineCount");
conf.setJars(new String[] { "/home/sam/resin-4.0.42/webapps/test.war" });
File configFile = new File("config.properties");
String sparkURI = "spark://hamrah:7077";
conf.setMaster(sparkURI);
conf.set("spark.driver.allowMultipleContexts", "true");
JavaSparkContext sc = new JavaSparkContext(conf);
@SuppressWarnings("resource")
JavaRDD<String> log = sc.textFile(sourcePath);
JavaRDD<String> lines = log.filter(x -> {
return true;
});
return lines.count();
});
}
Run Code Online (Sandbox Code Playgroud)
如果我删除lambda表达式或将其放在一个简单的jar而不是web服务(不知何故是一个servlet)中,它将运行而没有任何错误.但是在servlet中使用lambda表达式将导致此异常:
15/01/28 10:36:33 WARN TaskSetManager: Lost task 0.0 in stage 0.0 (TID 0, hamrah): java.lang.ClassCastException: cannot assign instance …Run Code Online (Sandbox Code Playgroud) 在对JavaDoc中findFirst说,如果流有一个邂逅的命令,那么第一个元素总是会返回,但如果流没有遭遇订单,可以返回的任何元素.
我试图演示它如何在没有遭遇顺序的流上工作,但我不能让它返回除了实际的第一个元素之外的任何东西.
我尝试将元素添加到a Set,它没有定义的遭遇顺序:
Set<String> words = new HashSet<>();
words.addAll(Arrays.asList("this", "is", "a", "stream", "of", "strings"));
Optional<String> firstString = words.stream()
.findFirst();
System.out.println(firstString);
Run Code Online (Sandbox Code Playgroud)
每次我跑,我得到a第一个字符串.然后我试图做一个Collections.shuffle关于List它添加到之前Set,但这并没有改变任何东西.
List<String> wordList = Arrays.asList("this", "is", "a", "stream", "of", "strings");
words = new HashSet<>();
words.addAll(wordList);
firstString = words.stream()
.findFirst();
System.out.println(firstString);
Run Code Online (Sandbox Code Playgroud)
我a每次都会回复这个词.
然后我尝试使用unorderedfrom方法BaseStream,声称返回没有遇到顺序的流,但没有区别:
firstString = Stream.of("this", "is", "a", "stream", "of", "strings")
.unordered()
.findFirst();
System.out.println(firstString);
Run Code Online (Sandbox Code Playgroud)
现在我this每次都得到这个词.我错过了什么吗?有没有办法证明findFirst在无序流上返回不同的值?