Posts

Showing posts from October, 2017

HashMap Example Code

import java . util . HashMap ; import java . util . Map ; import java . util . Iterator ; import java . util . Set ; public class HashMapEx { public static void main ( String args []) { HashMap < Integer , String > hmap = new HashMap < Integer , String >(); // Adding to HashMap hmap . put ( 12 , "Kabir" ); hmap . put ( 2 , "Ravi" ); hmap . put ( 7 , "Luis" ); hmap . put ( 49 , "Peter" ); hmap . put ( 3 , "Aditya" ); // Iterate through HashMap Set set = hmap . entrySet (); Iterator iterator = set . iterator (); while ( iterator . hasNext ()) { Map . Entry mentry = ( Map . Entry ) iterator . next (); System . out . print ( "Key is: " + mentry . getKey () + " & Value is: " ); System . out . println ( mentry . getValue ()); } // Get a value...

Program to Create arraylists for Courses and Students using OOP concepts with getter and setter methods

Course.java public class Course { private String id; private String name; public Course() { } public Course(String id, String name) { this.id = id; this.name = name; } public void setId(String id) { this.id = id; } public String getId() { return this.id; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } @Override public String toString() { return this.id; } } Student.java public class Student { private int rollNo; private String name; private Course course; private String address; private int phoneNumber; /** * Default Constructor */ public Student() { } /** * Overloaded Constructor */ public Student(int rollNo, String name, String address, int phoneNumber, Course course) { this.rollNo = rollNo; this.name = name; this.address = address; this.phoneNumber = phoneNumber; this.course = course; } /** * Setter for rol...