How to Set Multiple data and retrive Using Generic list and Comparator
package com.AddMultiple;
import java.util.*;
/**
*
* @author Ananddw
*
*/
public class Mutiple implements Comparable<Mutiple>{
private String name;
private String age;
private String location;
@Override
public int compareTo(Mutiple o) {
return name.compareTo(o.name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public static void main(String[]args){
List<Mutiple>list = new ArrayList<Mutiple>();
for(int i=0;i<3;i++){
Mutiple mutiple =new Mutiple();
if(i==0){
mutiple.setName("Anand");
mutiple.setAge("24");
mutiple.setLocation("Bangalore");
}
if(i==1){
mutiple.setName("Ajeet");
mutiple.setAge("24");
mutiple.setLocation("Pune");
}
if(i==2){
mutiple.setName("Antrish");
mutiple.setAge("24");
mutiple.setLocation("Chennai");
}
list.add(mutiple);
}
for(Mutiple mutlist:list){
System.out.println("==>"+mutlist.getName()+"\t"+mutlist.getAge()+"\t"+mutlist.getLocation());
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
O/P
==>Anand 24 Bangalore
==>Ajeet 24 Pune
==>Antrish 24 Chennai
import java.util.*;
/**
*
* @author Ananddw
*
*/
public class Mutiple implements Comparable<Mutiple>{
private String name;
private String age;
private String location;
@Override
public int compareTo(Mutiple o) {
return name.compareTo(o.name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public static void main(String[]args){
List<Mutiple>list = new ArrayList<Mutiple>();
for(int i=0;i<3;i++){
Mutiple mutiple =new Mutiple();
if(i==0){
mutiple.setName("Anand");
mutiple.setAge("24");
mutiple.setLocation("Bangalore");
}
if(i==1){
mutiple.setName("Ajeet");
mutiple.setAge("24");
mutiple.setLocation("Pune");
}
if(i==2){
mutiple.setName("Antrish");
mutiple.setAge("24");
mutiple.setLocation("Chennai");
}
list.add(mutiple);
}
for(Mutiple mutlist:list){
System.out.println("==>"+mutlist.getName()+"\t"+mutlist.getAge()+"\t"+mutlist.getLocation());
}
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
O/P
==>Anand 24 Bangalore
==>Ajeet 24 Pune
==>Antrish 24 Chennai
Comments
Post a Comment