鉴于代码:
public Statement methodCallByName(MethodDeclaration method, String string) {
List<ExpressionStatement> expressions = method.getBody().statements().stream()
.filter(s -> s instanceof ExpressionStatement)
.map(ExpressionStatement.class::cast)
.collect(Collectors.toList());
return null;
}
Run Code Online (Sandbox Code Playgroud)
我在Eclipse Oxygen中有以下错误:
请注意,根据JDT文档statements()返回a .List
怎么了?
可能将Java int []数组的副本复制到HashSet <Integer>但很难回答这么新的问题.
我有一套我想宣布的:
int[] flattened = Arrays.stream(arcs).flatMapToInt(Arrays::stream).toArray();
Set<Integer> set = new HashSet<Integer>(Arrays.asList(flattened));
Run Code Online (Sandbox Code Playgroud)
但由于返回类型Arrays.asList是一个列表本身,它无法解决.什么是将列表int[]转换为最佳方法Set<Integer>
我想知道在List实现中是否存在某种类型的reverse_iterator(如c ++)ArrayList<Integer>.即,我想从列表的末尾而不是从头开始迭代?
有没有Java8流解决方案?
我写了一个线性搜索实现.我也使用Java 7和8编写了代码.
以下是我的代码:
int[] arr = new int[] { 12, 34, 94, 8, 37, 10 };
System.out.println("Enter the element to search: ");
Scanner scan = new Scanner(System.in);
int element = scan.nextInt();
boolean found = false;
// Linear search implementation using Java 7 features
for (int i = 0; i < arr.length; i++) {
if (element == arr[i]) {
int n = element;
System.out.println("Element : "+n+" has been found at the index : "+i);
found = true;
break;
}
}
// …Run Code Online (Sandbox Code Playgroud) 我有一个Map和List它看起来像
int total = 0;
int incr = 10;
Map<String, Boolean> hashMap = new HashMap();
hashMap.put("A", true);
hashMap.put("B", false);
hashMap.put("C", true);
List<String> states = new ArrayList<>();
states.add("A");
states.add("B");
states.add("D");
states.add("E");
states.add("F");
Run Code Online (Sandbox Code Playgroud)
每当我从列表中找到一个键时,Map我想用增量值递增总计;
states.stream()
.filter(s -> hashMap.containsKey(s) && hashMap.get(s))
.forEach((s) -> {total = total + incr;} );
Run Code Online (Sandbox Code Playgroud) 我正在开发一个程序,它使用返回的方法,Optional我需要迭代它并创建一个新对象.我怎么做?
import java.util.Optional;
class Info {
String name;
String profileId;
Info(String name, String profileId) {
this.name = name;
this.profileId = profileId;
}
}
class Profile {
String profileId;
String profileName;
Profile(String profileId, String profileName) {
this.profileId = profileId;
this.profileName = profileName;
}
}
class Content {
String infoName;
String profileName;
Content(String infoName, String profileName) {
this.infoName = infoName;
this.profileName = profileName;
}
public java.lang.String toString() {
return "Content{" + "infoName='" + infoName + '\'' + ", profileName='" + …Run Code Online (Sandbox Code Playgroud) 功能:
private static void printArray(int[] array, Optional<Integer> startIndex, Optional<Integer> endIndex) {
for(Integer i = a.orElse(new Integer(0)); i<=endIndex.orElse(new Integer(array.length));i++) {
System.out.print(array[i]+" ");
}
}
Run Code Online (Sandbox Code Playgroud)
在传递如下值时:
printArray(arr1, null, null);
Run Code Online (Sandbox Code Playgroud)
NPE被抛出.为什么Optional.orElse函数不创建新的Integer对象?我检查了StackOverflow但是没有发现NPE orElse被抛出.我发现这种行为出乎意料.
欢迎所有建议.
我想在Java中创建一个双精度数组,其中包含0到100之间的值,步长为0.1
在Python中,可以使用范围函数: range(0, 100, 0.1)
在Java 8中,这可以工作:
Double[] x = DoubleStream.iterate(0.1, i -> i + 0.1).limit(1000).boxed().toArray(Double[]::new);
Run Code Online (Sandbox Code Playgroud)
有什么比我更简单的了吗?
我有这个方法,我想返回一个空的可选项是没有找到
@Override
public Optional<Menu> findBySymbol (String symbol) {
Optional<Menu> menu =
StreamSupport
.stream(cachedMenus.get(ALL_CURRENCIES_KEY).spliterator(), true)
.findFirst();
return menu.orElse(Optional.empty());
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了编译错误: Type mismatch: cannot convert from Optional<Object> to Menu
我试图压扁地图但不丢失钥匙.
我想拥有的东西:
Map<K, Collection<V>> keyToValuesMap = ...;
Stream.flattenCollection(keyToValuesMap).forEach((k,v) -> print(k, v));
Run Code Online (Sandbox Code Playgroud)
其中,v表示集合中对应于每个值的每个值k.
一个等效的旧学校Java代码将是:
for (Map.Entry<K, Collection<V>> entry : keyToValuesMap.entrySet())
{
K k = entry.getKey();
for (V v : entry.getValue())
{
print(k, v);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这个问题不仅仅是对值进行流式处理,因为它key会丢失,如下所示:keyToValuesMap.entrySet().stream().flatMap(keyValuesEntry -> keyValuesEntry.getValue().stream()).forEach(v -> printJustV(v));
*这print只是该对的一个示例动作.
*我在StreamEx中搜索过这样的API ,但找不到.我确信我会在那里找到它......
java-8 ×10
java ×9
java-stream ×4
optional ×3
collections ×2
arrays ×1
hashmap ×1
null ×1
set ×1