public class Student{
private String name;
private int studentId;
//getter and setters
}
Write a method which accepts a Set of Student objects and returns a Map with Student Id as key and name as value.
June 12, 2012 at 1:06 PM
Here comes an example that stores the student object into Set and display the data in the form of key value pair.
import java.util.*;
public class Student{
public static Map displayData(){
Set<Student> set=new HashSet<Student>();
Scanner input=new Scanner(System.in);
for(int i=1;i<=5;i++){
System.out.print("Enter ID: ");
int id=input.nextInt();
System.out.print("Enter Name: ");
String name=input.next();
set.add(new Student(id,name));
}
HashMap<Integer,String> map=new HashMap<Integer,String>();
for(Student st:set){
map.put(st.getStudentId(),st.getName());
}
return map;
}
private String name;
private int studentId;
Student(int studentId,String name){
this.studentId=studentId;
this.name=name;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setStudentId(int studentId){
this.studentId=studentId;
}
public int getStudentId(){
return studentId;
}
public static void main(String []args){
Map map=displayData();
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
}
}
Related Tutorials/Questions & Answers:
CollectionsCollections Hi,
Please send a study material on
collections.../java/jdk6/introduction-
collections-api.shtml
http://www.roseindia.net/javacodeexamples/index.shtml
http://www.devmanuals.com/tutorials/java/
collections/index.html
collectionscollections in
collections can we perform binarysearch on arraylist elements without sorting them
Advertisements
collectionscollections why we need collections.when we will use
collections in our applications
Collections APICollections API hello,
What is the
Collections API?
hiADS_TO_REPLACE_1
The
Collections API is a set of classes and interfaces that support operations on
collections of objects
java collectionsjava collections what are all the methods and things added for
collections in java5 and java6 please kindly reply me as soon as possible
Collections FrameworkCollections Framework Sir, We know that both HashMap & Hashtable is using for same purposes i.e Used for storing keys-values pair. But there is some difference between this two class that are
1)Hashtable is synchronized
collections in javacollections in java please explain me the flow of this program..i m getting o/p as 2 1 1..y not 2 1 0..is it that if we change the i2 value,it doesnt remove.
import java.util.*;
public class Mapit {
public static void main
java collections achieve this plz without using stringtokenizer with any
collections!!!
Hibernate Criteria With Collections ExampleHibernate Criteria With
Collections Example Good code of Hibernate Criteria With
Collections Example.
Thanks
Example program of using Hibernate Criteria With
Collections in your Java project.
Check the tutorial
collections - Java Interview Questionscollections The Java
Collections API Hi friend,
Java
Collections of API (Application Programming Intreface) Consists of several.../java/jdk6/introduction-
collections-api.shtml
Thanks