鉴于一堆具有int size()方法和get(int i)方法的东西,它如何最容易流式传输?
import nu.xom.Builder;
import nu.xom.Element;
import nu.xom.Elements;
// My builder.
Builder builder = new Builder();
class Thing {
public Thing(Element from) {
// Construct from an Element.
}
}
private Stream<Thing> allThings(Path path) throws FileNotFoundException, ParsingException, IOException {
Elements things = builder.build(new FileInputStream(path.toFile()))
.getRootElement().getChildElements();
// Return a stream of `Thing`s created from all of the children.
// How??
}
Run Code Online (Sandbox Code Playgroud)
我的尝试使用了一所旧式学校,Iterable并且流式传输似乎不必要的杂乱.
免责声明:这只是我目前正在做的家庭作业的一部分
假设我有一个方法数组,其中包含我所有声明的方法
Method methodList[] = classGetter.getDeclaredMethods();
Run Code Online (Sandbox Code Playgroud)
其中classGetter只是获取我想要获取方法的类.有没有办法通过与方法关联的修饰符类型对此数组进行排序?当我使用打印出修改器时,我看到了
for(Method m: methodList){
System.out.println(m.getModifiers());
}
Run Code Online (Sandbox Code Playgroud)
它返回整数值.我尝试使用Arrays.sort方法排序,但我得到了错误异常.任何指针?
我有一个函数列表,所有这些函数都扩展了Consumer接口.我需要提取出函数的索引.这样做的原因是我正在实现循环工作流,其中用户按顺序调用列表中的函数.因此,当用户使用foo完成时,foo调用一个调度程序函数,该函数在列表中的foo之后调度该函数.为此,foo需要知道列表中自己的索引.我不想硬编码数字,这就是我试图检索foo索引的原因.这就是我所拥有的:
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.function.Consumer;
public class Experiment {
private List<Consumer<String>> activities = ImmutableList.of(
this::bar,
this::foo
);
public void bar(String x) {
System.out.println();
}
public void foo(String x) {
System.out.println();
}
public void print() {
System.out.println(activities.indexOf((Consumer<String>)this::foo));
}
public static void main(String []args) {
Experiment e = new Experiment();
e.print();
}
}
Run Code Online (Sandbox Code Playgroud)
这打印-1给我,所以它无法在列表中找到它.有没有办法从这个列表中提取出函数的索引?
我有一个: List<Map<String, Long>> items = new ArrayList<>();
我想得到一个Map,其中键被分组,值是总和.
示例:列表
结果:地图
我知道如何以"长"方式执行此操作,但尝试使用java 8功能发现lambda/streaming/groupby方法.有什么想法吗?
我有哈希图: Map<String, Set<String>> myMap
我想将其拆分为包含Map以下内容的列表:
List<Map<String,Set<String>>> listofMaps;
Run Code Online (Sandbox Code Playgroud)
,每张地图最多可包含100个键。我知道如何以常规方式执行此操作。(在entryset上进行foreach,每100个项目会创建一个新地图)。有什么选择可以用Java 8 lambda或其他东西吗?(有点像Lists.partitions()..)?
我正在尝试转换List为HashMap使用流.这是我的代码..
attributeUnitMap = attributesList.stream()
.filter(a -> a.getAttributeUnit() != null)
.collect(Collectors.toMap(Attribute::getAttributeName, a -> a.getAttributeUnit()));
Run Code Online (Sandbox Code Playgroud)
现在我想添加条件,如果我得到属性名称null,那么应该将项目添加到带有空白字符串的map中,如下所示(any_attributeName,"").
如何使用流操作实现此目的.我知道我可以通过使用过滤条件来检查属性名称是否为null但是如果它为null则可以添加空字符串.可能吗?如果没有,为什么呢?请帮忙.
我想知道是否有比我正在尝试的更好、更短、更优雅的方法来实现这一目标。假设我有 3 个整数(value1、value2、value3),我想找到这些整数中的最大值,并且它们允许为空值。我无法使用下面的代码,因为它可能会抛出 NullPointerException:
Math.max(Math.max(value1, value2), value3)
Run Code Online (Sandbox Code Playgroud)
我写了一个野蛮的代码(如下所示),但它不会缩放超过 3 个整数:
public Integer getMaxValue(Integer value1, Integer value2, Integer value3) {
Integer defaultValue = 1;
if (value1 == null && value2 == null && value3 == null) {
return defaultValue;
} else if (value1 == null && value2 != null && value3 != null) {
return Math.max(value2, value3);
} else if (value2 == null && value1 != null && value3 != null) {
return Math.max(value1, value3);
} else if (value3 == null …Run Code Online (Sandbox Code Playgroud) 为了探索 lambda 及其工作原理,我编写了下面的代码。
public class Foo { // enclose class
public static void main(String[] args) {
System.out.println(new Foo().new InnerFoo());
IntSupplier intSupplier = () -> 42;
System.out.println(intSupplier);
}
class InnerFoo{} // inner class
}
Run Code Online (Sandbox Code Playgroud)
执行代码后,我看到以下输出。
Foo$InnerFoo@3af49f1c
Foo$$Lambda$15/0x0000000840064c40@13221655
Run Code Online (Sandbox Code Playgroud)
我基本上有两个基于输出的问题。
toList() 的实现明确返回一个 ArrayList,它确实保证了可变性:
public static <T>
Collector<T, ?, List<T>> toList() {
return new CollectorImpl<>(ArrayList::new, List::add,
(left, right) -> { left.addAll(right); return left; },
CH_ID);
}
Run Code Online (Sandbox Code Playgroud)
但是 toList 的 JavaDoc 是这样的:
/**
* Returns a {@code Collector} that accumulates the input elements into a
* new {@code List}. There are no guarantees on the type, mutability,
* serializability, or thread-safety of the {@code List} returned; if more
* control over the returned {@code List} is required, use {@link #toCollection(Supplier)}.
*
* …Run Code Online (Sandbox Code Playgroud) 在我的代码中,我有这个:
private static final List<String> a = new ArrayList<String>() {{
addAll(b);
addAll(c);
add(d);
add(e);
}};
Run Code Online (Sandbox Code Playgroud)
我在某些地方读到双括号语法创建了一个匿名类或其他东西,这是一种非常糟糕的语法。
由于这是一个静态最终变量,因此需要在单个逻辑行中设置它。
我找到了使用流在单行中连接列表的代码示例,但没有一个允许添加单个元素。
我正在寻找一种与 Java 8 兼容的语法,但(如果不同)仅与更高版本的 Java 兼容的更好语法也将受到欢迎。
java ×10
java-8 ×10
java-stream ×4
collections ×2
lambda ×2
collectors ×1
dictionary ×1
hashmap ×1
list ×1
reflection ×1