在较低的层次上,java 8对新的运算符" - >"进行了哪些更改,尽管我理解在正常的编程实践中我们不能像C++一样重载任何现有的运算符.
我最近开始检查新的Java 8功能.
我遇到过这个forEach迭代器 - 迭代了Collection.
我们假设我有一个值为{1,2,3,4,5} ArrayList的类型<Integer>
list.forEach(i -> System.out.println(i));
Run Code Online (Sandbox Code Playgroud)
此语句遍历列表并在其中打印值.
我想知道我将如何指定我希望它仅迭代某些特定值.
就像,我希望它从第二个值开始并迭代到第二个值.或类似的东西 - 或替代元素.
我该怎么做?
我知道有isAfter两种方法LocalDateTime和LocalDate.
然而,如果我可以混合这些类,即LocalDate.isAfter(LocalDateTime)不将其中一个类转换为另一个类,它将使我的生活更容易.
是否可以用lambda替换构造:
DMXFrame[] frames = new DMXFrame[universes.size()];
for (int i = 0; i < frames.length; i++) {
frames[i] = universes.get(i).getDMXFrame();
}
return frames;
Run Code Online (Sandbox Code Playgroud) 使用Java 8,我正在检查它的一些新功能......
创建了以下类:
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Run Code Online (Sandbox Code Playgroud)
创建了PersonApp类:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import static java.util.Comparator.comparing;
public class PersonApp {
public static void printSorted(List<Person> people, Comparator<Person> comparator) {
people.stream() …Run Code Online (Sandbox Code Playgroud) 我有一个整数数组,例如:
{ 30 , 2, 3, 4, 5}
Run Code Online (Sandbox Code Playgroud)
是否有可能创建一个lambda表达式(使用流)来区分数组的第一个元素(在我们的例子30中)和数组其余部分的总和,(2+3+4+5 = 13)结果是17?
我正在尝试将java7程序转换为java8。我想在下面使用流API进行输出。
public List<String> getTopThreeWeatherCondition7() {
List<String> _Top3WeatherList = new ArrayList<String>();
Map<String, Integer> _WeatherCondMap = getWeatherCondition7();
List<Integer> _WeatherCondList = new ArrayList<Integer>(_WeatherCondMap.values());
Collections.sort(_WeatherCondList, Collections.reverseOrder());
List<Integer> _TopThreeWeathersList = _WeatherCondList.subList(0, 3);
Set<String> _WeatherCondSet = _WeatherCondMap.keySet();
Integer count = 0;
for (String _WeatherCond : _WeatherCondSet) {
count = _WeatherCondMap.get(_WeatherCond);
for (Integer _TopThreeWeather : _TopThreeWeathersList) {
if (_TopThreeWeather == count) {
_Top3WeatherList.add(_WeatherCond);
}
}
}
_WeatherCondList = null;
_WeatherCondMap = null;
_TopThreeWeathersList = null;
_WeatherCondSet = null;
return _Top3WeatherList;
}
Run Code Online (Sandbox Code Playgroud) 我想使用Streams和Lambdas.但是如何用它重写我当前的方法呢?
public double calculate() {
double result = 0.0;
if (!mediumList.isEmpty()) {
Iterator<Medium> it = mediumList.iterator();
while (it.hasNext()) {
result= result+ it.next().getAge();
}
result = result / mediumList.size();
}
return result;
}
Run Code Online (Sandbox Code Playgroud) 我正在做一个家庭作业,我需要读取一个java源文件并删除它的所有注释.其余的造型应该保持不变.
我已经使用正则表达式完成了任务.
但我希望在不使用正则表达式的情况下完成同样的工作.
示例输入
// My first single line comment
class Student {
/* Student class - Describes the properties
of the student like id, name */
int studentId; // Unique Student id
String studentName; // Name of the student
String junk = "Hello//hello/*hey";
} // End of student class
Run Code Online (Sandbox Code Playgroud)
结果
class Student {
int studentId;
String studentName;
String junk = "Hello//hello/*hey";
}
Run Code Online (Sandbox Code Playgroud)
我的想法是阅读每一行
1)检查前两个字符
如果以// ==>开头,则删除该行
如果它以/*==>开头删除所有行直到*/
2)另一种情况是处理
示例 - int studentId; //评论或/*评论*/
有人可以提供更好的方法吗?
1)敏锐地知道如何使用Java 8并行运行Method1、2、3
2)这是使用流8满足我的要求的正确方法吗?
public void RunParallel()
{
String method1 = method1();
String method2 = method2();
String method3 = method3(String Value);
}
Run Code Online (Sandbox Code Playgroud)
Stream.<Runnable>of(() -> method1(),() -> method2(),() -> method3()).parallel().forEach(Runnable::run);