int [] intArray = { 10, 20, 30, 40, 50 };
for( int value : intArray ) {
System.out.println( value );
}
//itemset contains variables of type <Data Type>
for(<Data Type> item : itemset) {
//loop
}
List<String> someList = new ArrayList<String>();
// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) {
System.out.println(item);
}
for (String name : names) {
System.out.println(name);
}
for (String item : someList) {
System.out.println(item);
}
for (String item : someList) {
System.out.println(item);
}
names.forEach(name -> {
System.out.println(name);
});
int [] intArray = { 10, 20, 30, 40, 50 };
for( int value : intArray ) {
System.out.println( value );
}
//each llop
for (type var : array)
{
statements using var;
}
//each llop
for (type var : array)
{
statements using var;
}
import java.util.concurrent.ConcurrentHashMap;
class Main {
public static void main(String[] args) {
ConcurrentHashMap<String, Integer> numbers = new ConcurrentHashMap<>();
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);
System.out.println("ConcurrentHashMap: " + numbers);
// forEach() without transformer function
numbers.forEach(4, (k, v) -> System.out.println("key: " + k + " value: " + v));
// forEach() with transformer function
System.out.print("Values are ");
numbers.forEach(4, (k, v) -> v, (v) -> System.out.print(v + ", "));
}
}
int value;
int[] arr = {6, 7, 8};
for (value : arr) {
System.out.print(val + " ");
}
class For_Each
{
public static void main(String[] arg)
{
{
int[] marks = { 125, 132, 95, 116, 110 };
int highest_marks = maximum(marks);
System.out.println("The highest score is " + highest_marks);
}
}
public static int maximum(int[] numbers)
{
int maxSoFar = numbers[0];
// for each loop
for (int num : numbers)
{
if (num > maxSoFar)
{
maxSoFar = num;
}
}
return maxSoFar;
}
}
//each llop
for (type var : array)
{
statements using var;
}
List<String> list = List.of("a", "b", "c");
list.forEach(elt -> System.out.println(elt)) /* output: a
b
c */
for (int i=0; i<arr.length; i++)
{
type var = arr[i];
statements using var;
}