Java Iterator examples

A few of Java Iterator and ListIterator examples. 1. Iterator 1.1 Get Iterator from a List or Set, and loop over it. JavaIteratorExample1a.java package com.mkyong; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class JavaIteratorExample1a { public static void main(String[] args) { /* Set<Integer> set = new HashSet<>(); set.add(1); set.add(2); set.add(3); set.add(4); set.add(5); Iterator<Integer> iterator = …

Read more

Java 8 forEach print with Index

A simple Java 8 tip to print the Array or List with index in the front. 1. Array with Index Generate the index with IntStream.range. JavaListWithIndex.java package com.mkyong.java8; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class JavaArrayWithIndex { public static void main(String[] args) { String[] names = {"Java", "Node", "JavaScript", "Rust", "Go"}; List<String> collect = …

Read more

Java – How to print a name 10 times?

This article shows you different ways to print a name ten times. 1. Looping 1.1 For loop JavaSample1.java package com.mkyong.samples; public class JavaSample1 { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println("Java "); } } } Output Java Java Java Java Java Java Java Java Java …

Read more

Java – How to print name 1000 times without looping?

Here’s a Java example to print a name 1000 times without looping or recursion, instead, use String concatenation and simple math. Just for fun. JavaNoLoop.java package com.mkyong.samples; public class JavaNoLoop { public static void main(String[] args) { String s1 = "Mkyong\n"; String s3 = s1 + s1 + s1; String s10 = s3 + s3 …

Read more

How to loop an enum in Java

Call the .values() method of the enum class to return an array, and loop it with the for loop: for (EnumClass obj : EnumClass.values()) { System.out.println(obj); } For Java 8, convert an enum into a stream and loop it: Stream.of(EnumClass.values()).forEach(System.out::println); 1. For Loop Enum 1.1 An enum to contain a list of the popular JVM …

Read more

Python – How to print dictionary

Python example to print the key value of a dictionary. stocks = { ‘IBM’: 146.48, ‘MSFT’: 44.11, ‘CSCO’: 25.54 } print(stocks) for k, v in stocks.items(): print(k, v) for k in stocks: print(k, stocks[k]) Output {‘IBM’: 146.48, ‘MSFT’: 44.11, ‘CSCO’: 25.54} IBM 146.48 MSFT 44.11 CSCO 25.54 IBM 146.48 MSFT 44.11 CSCO 25.54 References Python …

Read more

Java – How to Iterate a HashMap

In Java, there are 3 ways to loop or iterate a HashMap 1. If possible, always uses the Java 8 forEach. Map<String, String> map = new HashMap<>(); map.forEach((key, value) -> System.out.println("[Key] : " + key + " [Value] : " + value)); 2. Normal for loop in entrySet() Map<String, String> map = new HashMap<>(); for …

Read more

JMH – Java Forward loop vs Reverse loop

A JMH benchmark test about Forward loop vs Reverse loop for a List. There is a myth about reverse loop is faster, is this true? Tested with JMH 1.21 Java 10 Maven 3.6 CPU i7-7700 Forward loop for (int i = 0; i < aList.size(); i++) { String s = aList.get(i); System.out.println(s); } Reverse loop …

Read more

Java JMH Benchmark Tutorial

Benchmark (N) Mode Cnt Score Error Units BenchmarkLoop.loopFor 10000000 avgt 10 61.673 ± 1.251 ms/op BenchmarkLoop.loopForEach 10000000 avgt 10 67.582 ± 1.034 ms/op BenchmarkLoop.loopIterator 10000000 avgt 10 66.087 ± 1.534 ms/op BenchmarkLoop.loopWhile 10000000 avgt 10 60.660 ± 0.279 ms/op In Java, we can use JMH (Java Microbenchmark Harness) framework to measure the performance of a …

Read more

Python – How to loop a List

In this tutorial, we will show you how to use for-in-loop to loop a List in Python. #example 1 frameworks = [‘flask’, ‘pyramid’, ‘django’] #list for temp in frameworks: print temp print "Python framework : %s" %temp #example 2 numbers = [2,4,6,8,10] #list for num in numbers: print num print "Number : %d" %num Output …

Read more

Python – How to loop a dictionary

In this tutorial, we will show you how to loop a dictionary in Python. 1. for key in dict: 1.1 To loop all the keys from a dictionary – for k in dict: for k in dict: print(k) 1.2 To loop every key and value from a dictionary – for k, v in dict.items(): for …

Read more

How to loop ArrayList in Java

No nonsense, four ways to loop ArrayList in Java For loop For loop (Advance) While loop Iterator loop package com.mkyong.core; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListLoopingExample { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Text 1"); list.add("Text 2"); list.add("Text 3"); System.out.println("#1 normal for loop"); for (int i = …

Read more

How to loop / iterate a List in Java

Here i show you four ways to loop a List in Java. Iterator loop For loop For loop (Adcance) While loop package com.mkyong.core; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class ArrayToList { public static void main(String[] argv) { String sArray[] = new String[] { "Array 1", "Array 2", "Array 3" }; // convert array …

Read more