It's not a pure Java solution, however adding a library called lombok will enable the magic below to compile and work very much similar to auto keyword in C++
List strList = Arrays.asList("foo", "bar", "baz");
for (val s: strList){
System.out.println(s.length());
}