reverse() method

 MAIN.JAVA:

import java.io.*;
import java.util.*;
public class Main{
public static void main(String args[]) throws IOException {
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of users:");
int num=Integer.parseInt(bf.readLine());
ArrayList<User> user=new ArrayList<User>();
User u1=new User();
for(int i=0; i<num; i++)
{
System.out.println("Enter the details of User "+(i+1));
String str=bf.readLine();
String st[]=str.split(",");
User u=new User(st[0],st[1],st[2],st[3]);
user.add(u);
}
Collections.sort(user,Collections.reverseOrder());
System.out.println("The user details in reverse order:");
System.out.printf("%-15s%-15s","Name","Mobile number");
System.out.println();
for(User ele:user)
{
System.out.println(ele);
}
}
}




USER.JAVA:

public class User implements Comparable<User> {
private String neme,mobileNumber,username,password;
public User() {
super();
}
public User(String neme, String mobileNumber, String username, String password) {
super();
this.neme = neme;
this.mobileNumber = mobileNumber;
this.username = username;
this.password = password;
}
public String getNeme() {
return neme;
}
public void setNeme(String neme) {
this.neme = neme;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override public String toString(){
return String.format("%-15s%-15s",getNeme(),getMobileNumber());
}
@Override public int compareTo(User arg0) {
// TODO Auto-generated method stub
return this.getNeme().compareTo(arg0.getNeme());
}
//write your code here
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.