Implementing Iterable Interface In Java To Enable For Each Loop Based Iteration


Posted by | Posted in Vehicle Tracking | Posted on 02-03-2018

Implementing Iterable interface in Java to enable for-each loop based iteration by InfocampusTypical for-each construct usagefor(MyClass myClassObject: list){ //code to use myClassObject}Where list is an instance of java.util.List.Most of the important in-built collection types now support iteration using the enhanced for-each loop. This is by virtue of their implementing the interface Iterable. In fact, any class which implements Iterable, can be used in a for-each loop to iterate over the objects of type T which it holds or encapsulates. Extending this logic to the small code snippet we saw above MyCollection which implements Iterable, can be used in a for-each loop to iterate through MyClass objects stored in it.Having understood the relationship between implementing Iterable interface and use of the implementing class in for-each loop, let us now understand how to go about implementing Iterable.How to implement Iterable interfaceAny class implementing Iterable needs to follow three simple steps 1.Implement Iterable interface.2.Override Iterables iterator() method.3.Return an instance of Iterator from the iterator() method.So, if you have an API/Class containing a collection of String type of elements, and you want clients of this API to be able to access theString objects using a for-each loop, then your three steps of implementing Iterable would go like this 1.Implement Iterable.2.Override Iterables iterator() method.3.Return an instance of Iterator from the iterator() method.Simple, right! There is a small piece of logic missing though!!How do you get hold of an Iterator instance pointing to your stored collection?The general practice in this case is to return the in-built Iterator instance of the collection class you use to store the iterable objects in your API. So, if you use a List to store the String objects to be iterated, then you return Iterator returned by List.iterator() method as the output of overridden Iterable.iterator() method.Let us see a Java code example to see how Iterable implementation can be done.Java code example showing Iterable implementationLets take a simple case of aggregation to show an Iterable implementation in action. For our example scenario we have 2 types Department and Employee. A Department instance holds multiple Employee instances in a employee list, or List. We will make Department class implement the Iterable interface. Doing so would would allow us to iterate through employees of a department using the for-each loop just by getting hold of a Department instance. Let us see the code in action now, which will be followed by detailed explanation of the code.Java code example showing Iterable implementation// com.javabrahman.corejava;public class Employee { private String name; private Integer age; public Employee(String name, Integer age) { = name; this.age = age; } //setters and getters for name & age go here //standard override of equals() & hashcode() methods goes here}// which implements Iterablepackage com.javabrahman.corejava;import java.util.List;import java.util.Iterator;public class IterableDepartment implements Iterable { List employeeList; public IterableDepartment(List employeeList){ this.employeeList=employeeList; } @Override public Iterator iterator() { return employeeList.iterator(); }}//Client class through IterableDepartment’s employees using for-each looppackage com.javabrahman.corejava;import java.util.Arrays;import java.util.List;public class IterableDeptClient { public static void main(String args[]){ List employeeList = Arrays.asList(new Employee(“Tom Jones”, 45), new Employee(“Harry Jones”, 42), new Employee(“Ethan Hardy”, 65), new Employee(“Nancy Smith”, 22), new Employee(“Deborah Sprightly”, 29)); IterableDepartment iterableDepartment=new IterableDepartment(employeeList); for(Employee emp: iterableDepartment){ System.out.println(emp.getName()); } }}OUTPUT of the above codeTom JonesHarry JonesEthan HardyNancy SmithDeborah SprightExplanation of the is the POJO class in this example. It has only 2 attributes name & age.IterableDepartment class contains a List attribute named employeeList which is initialized using IterableDepartments only public constructor.IterableDeptClient first creates an employee list consisting of 5 employees, and then passes this employee list to the constructor of the new IterableDepartment instance it creates.Then it iterates through the Employee objects in the IterableDepartment instance using a for-each loop.In each iteration of the for-each loop, name of the employee encountered is printed. As expected, the for-each loop correctly iterates through the 5 Employee objects stored in the IterableDepartment instance, and prints their names.SummaryIn the above tutorial we understood how Iterable interface can be implemented on a class holding a collection of objects. We then saw with a Java code example showing how such a collection can be iterated through using the enhanced for-each loop.Infocampus Bangalore offers java training in bangalore. Best java training in Bangalore. We also provide java certification training path for our students in Bangalore.on day time classes, end of the week instructional courses, evening group classes and quick track instructional courses. Contact 9738001024 to know substantially more points of interest on java preparing. Visit Source:

YouTube Preview Image

I Os App Development}


Posted by | Posted in Vehicle Tracking | Posted on 19-02-2018

iOS App Development


InfocampusWhat is an iOS? iOS (formerly iPhone OS) is a convenient working structure/working framework made and made by Apple Inc. exclusively for its gear It is the working framework that powers an impressive parcel of the association’s phones, including the iPhone, iPad, and iPod Touch. Like present day desktop working framework, iOS uses a graphical UI, or GUI. iOS is sketched out around touch screen contribution, rather than a console and mouse. For example, applications, or “applications,” can be opened by a solitary tap, rather than a twofold tap. Diverse screens can be seen by swiping your finger over the screen, rather than tapping on open windows. Since iOS is proposed to be fundamental and easy to use, it excludes a couple of features found in a standard working framework. For example, you can’t oversee records and coordinators like you can in Mac OS X or Windows. You moreover have limited access to iOS system settings. Instead of changing application slants from inside each program, most settings ought to be adjusted inside the Settings application. Additionally, while you can run distinctive projects at one, you can simply see one open program at any given moment. IOS utilizes a multi-touch interface where essential movements are used to work the contraption, as swipe, tap, squeeze, and switch squeeze, all of which incorporate specific definitions inside the setting of the iOS working framework 7 and its multi-touch interface. Inside accelerometers are used by a couple of uses to respond to shaking the contraption (one fundamental result is the fix request) or handing it over three estimations (one ordinary result is trading among portrayal and scene mode). iOS Training Institutes In Bangalore Collaboration with the OS fuses flags such Apple has been by and large lauded for merging watchful receptiveness limits into iOS, engaging customers with vision and hearing failures to really use its things .There are more than one million iOS applications available for download in the Apple application store, the most surely understood application store of any PDA. It is the second most well known compact working framework after Android. At first uncovered in 2007 for the iPhone, iOS has been extended to help other Apple contraptions, for instance, the iPod Touch (September 2007) and the iPad (January 2010). As of January 2017, Apple’s App Store contains more than 2.2 million iOS applications, 1 million of which are nearby for iPads. These convenient applications have all around been downloaded more than 130 billion times. What is an Operating framework? In the most essential terms, a working framework is the thing that lies among you and the physical contraption. It interprets the requests of programming (“applications”), and it gives those applications access to features of the contraption, for instance, the multi-touch screen or the limit. Flexible working framework like iOS change from most other working framework since they put every application in its own particular guarded shell, which shields diverse applications from modifying them. This makes it incomprehensible for an infection to spoil applications on a flexible working framework, albeit diverse sorts of malware can and do exist. The guarded shell around applications in like manner stances hindrances since it keeps applications from clearly cooperating with each other. iOS gets around this by using extensibility, a segment that fundamentally gives an application a chance to have the support to interface with another application. Hope this was helpful after your insight, Thank you Infocampus

Infocampus is the best iOS Training Institutes In Bangalore, with certified experts Highly Talented with 8+ Years Experienced Trainers Well Equipped Class Rooms.

We provide real-time live projects and hands on experience; Training is also aligned with certifications so you can easily validate your newly acquired skills. Infocampus is one of top and best App Development Course In Bangalore.For more details contact: 9738001024Visit:

Article Source:

YouTube Preview Image