我试图打印出一个列表的所有元素,但它是打印对象的指针,而不是值。
这是我的打印代码…
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
有人能告诉我为什么它不打印元素的值吗?
我试图打印出一个列表的所有元素,但它是打印对象的指针,而不是值。
这是我的打印代码…
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
有人能告诉我为什么它不打印元素的值吗?
当前回答
我现在正好在做这个…
List<Integer> a = Arrays.asList(1, 2, 3);
List<Integer> b = Arrays.asList(3, 4);
List<int[]> pairs = a.stream()
.flatMap(x -> b.stream().map(y -> new int[]{x, y}))
.collect(Collectors.toList());
Consumer<int[]> pretty = xs -> System.out.printf("\n(%d,%d)", xs[0], xs[1]);
pairs.forEach(pretty);
其他回答
public static void main(String[] args) {
answer(10,60);
}
public static void answer(int m,int k){
AtomicInteger n = new AtomicInteger(m);
Stream<Integer> stream = Stream.generate(() -> n.incrementAndGet()).limit(k);
System.out.println(Arrays.toString(stream.toArray()));
}
考虑一个List<String> stringList,它可以使用Java 8结构以多种方式打印:
stringList.forEach(System.out::println); // 1) Iterable.forEach
stringList.stream().forEach(System.out::println); // 2) Stream.forEach (order maintained generally but doc does not guarantee)
stringList.stream().forEachOrdered(System.out::println); // 3) Stream.forEachOrdered (order maintained always)
stringList.parallelStream().forEach(System.out::println); // 4) Parallel version of Stream.forEach (order not maintained)
stringList.parallelStream().forEachOrdered(System.out::println); // 5) Parallel version ofStream.forEachOrdered (order maintained always)
这些方法之间有什么不同?
第一种方法(Iterable.forEach)- 通常使用集合的迭代器,它被设计为快速失败,这意味着如果底层集合在迭代期间被结构修改,它将抛出ConcurrentModificationException。正如在ArrayList的文档中提到的:
结构修改是添加或删除一个或 更多的元素,或者显式地调整支持数组的大小;只是设置 元素的值不是结构修改。
它的意思是数组列表。对于每个设置值都是允许的,没有任何问题。在并发收集的情况下,例如ConcurrentLinkedQueue,迭代器将是弱一致的,这意味着在forEach中传递的操作甚至可以在不抛出ConcurrentModificationExceptionexception的情况下进行结构更改。但是这里的修改可能在迭代中可见,也可能不可见。
第二种方法(Stream.forEach)- 顺序没有定义。虽然它可能不会出现在连续流中,但规范并不能保证它。此外,该行为还必须是不干涉性质的。如doc中所述:
此操作的行为明显是非确定性的。为 平行流管道,此操作不保证 尊重溪流的遭遇顺序,因为这样做会牺牲 并行的好处。
第三种方法(Stream.forEachOrdered)- 该操作将按照流的遇到顺序执行。因此,每当顺序问题时,请使用forEachOrdered。正如文件中提到的:
在相遇中为此流的每个元素执行一个操作 如果流具有定义的遇到顺序,则为流的顺序。
当迭代一个同步集合时,第一种方法将获得集合的锁一次,并在所有对action方法的调用中保持它,但在流的情况下,他们使用集合的spliterator,它不锁,依赖于已经建立的不干扰规则。如果在迭代过程中修改了支持流的集合,则会抛出ConcurrentModificationException异常或可能出现不一致的结果。
第四种方法(Parallel Stream.forEach)- 正如已经提到的,在并行流的情况下,不能保证尊重遇到的顺序。有可能在不同的线程中对不同的元素执行不同的操作,而forEachOrdered则永远不会出现这种情况。
第五种方法(Parallel Stream.forEachOrdered)- forEachOrdered将按照源指定的顺序处理元素,而不管流是顺序的还是并行的。所以在并行流中使用这个是没有意义的。
下面是一些关于打印list组件的例子:
public class ListExample {
public static void main(String[] args) {
List<Model> models = new ArrayList<>();
// TODO: First create your model and add to models ArrayList, to prevent NullPointerException for trying this example
// Print the name from the list....
for(Model model : models) {
System.out.println(model.getName());
}
// Or like this...
for(int i = 0; i < models.size(); i++) {
System.out.println(models.get(i).getName());
}
}
}
class Model {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
这取决于List中存储的对象类型,以及它是否有toString()方法的实现。System.out.println(list)应该打印所有标准java对象类型(String, Long, Integer等)。在这种情况下,如果我们使用自定义对象类型,那么我们需要重写自定义对象的toString()方法。
例子:
class Employee {
private String name;
private long id;
@Override
public String toString() {
return "name: " + this.name() +
", id: " + this.id();
}
}
测试:
class TestPrintList {
public static void main(String[] args) {
Employee employee1 =new Employee("test1", 123);
Employee employee2 =new Employee("test2", 453);
List<Employee> employees = new ArrayList(2);
employee.add(employee1);
employee.add(employee2);
System.out.println(employees);
}
}
使用String.join () 例如:
System.out.print(String.join("\n", list));