Java - Streams: Difference between revisions

From My Limbic Wiki
Line 10: Line 10:


==Samples==
==Samples==
* Print all Students name
=== Print all Students name===
<source lang="Java">
<source lang="Java">
Student student;
Student student;
Line 19: Line 19:
}
}
</source>
</source>
* Get a Student
=== Get a Student ===
<source lang="Java">
<source lang="Java">
Student student = getNextStudent();
Student student = getNextStudent();
while (student != null) {
while (student != null) {
     collection.add(student);
     collection.add(student);
}
</source>
<source lang="Java">
Student student;
Iterator iterator = collection.iterator();
while (iterator.hasNext()) {
    student = (Student)(iterator.next());
    System.out.println(student.getFullName());
}
}
</source>
</source>

Revision as of 16:56, 18 October 2019

Collections

Main Methods

<source lang="Java"> - add(Object o) //Adds the specified object to the collection. - remove(Object o) //Removes the specified object from the collection. - clear() //Removes all elements from the collection. - size() //Returns an integer that indicates how many elements are currently in the collection. - iterator() //Returns an object that can be used to retrieve references to the elements in the collection. </source>

Samples

Print all Students name

<source lang="Java"> Student student; Iterator iterator = collection.iterator(); while (iterator.hasNext()) {

   student = (Student)(iterator.next());
   System.out.println(student.getFullName());

} </source>

Get a Student

<source lang="Java"> Student student = getNextStudent(); while (student != null) {

   collection.add(student);

} </source> <source lang="Java"> Student student; Iterator iterator = collection.iterator(); while (iterator.hasNext()) {

   student = (Student)(iterator.next());
   System.out.println(student.getFullName());

} </source>

A voir

  • Parallel stream