我想知道如果Stream.of这将有可能取代下面的一段代码:混合输入的内容
Object o;
// o can be one of the two following things
o = Arrays.asList("Toto", "tata", "Titi");
o = "Test";
if (o instanceof List<?>) {
((List<?>) o).stream().forEach(System.out::println);
} else {
System.out.println(o);
}
通过更少的丑陋的东西用Stream.of
。我想到了这一点:
Object o;
// o can be one of the two following things
o = Arrays.asList("Toto", "tata", "Titi");
o = "Test";
Stream.of(o).forEach(System.out::println);
但显然它不是作为Stream.of(aList)
工作不“扁平化”的内容。
任何想法?我是在做一些不存在的东西吗?
谢谢
PS。我不知道forEach
调用之前的Object
类型。这些代码只是这个问题的一个简单例子。
实际上,这部分解决了这个问题。我主要关心的是**我不知道调用之前的对象类型,它可以是列表或单个对象,我希望在之后进行流式处理。因为我不知道对象类型,我不能在已经存在的列表上执行'Collections.singletonList' ... – Jerome