Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

collections

Ads

 
 

Share on Google+Share on Google+

ramachandra
collections
1 Answer(s)      6 years and 4 months ago
Posted in : Java Beginners

1.Create an ArrayList of Object Planet with instance variable name and colour. Add 5 planets to the ArrayList planetList. Sort the ArrayList in ascending order of name and descending order of colour using comparator interface. (Note: Do not use comparable Interface)

Ads
View Answers

March 9, 2011 at 4:56 PM


import java.util.*;

class ShowData {
        String name;
        String color;
        ShowData(String name,String color){
        this.name=name;
        this.color=color;
        }
        public void setName(String name){
                this.name = name;
        }
        public String getName() {
                return name;
        }
        public void setGrade(String color) {
                this.color = color;
        }
        public String getColor(){
                return color;
        }
       }

class NameComparator implements Comparator{
    public int compare(Object o1, Object o2){    
       String name1 = ((ShowData)o1).getName();        
       String name2 = ((ShowData)o2).getName();
       return name1.compareTo(name2);
 }
}
class ColorComparator implements Comparator{
    public int compare(Object o1, Object o2){    
       String color1 = ((ShowData)o1).getColor();        
       String color2 = ((ShowData)o2).getColor();
       return color2.compareTo(color1);
 }
}
public class ArrayListEx{
         public static void main(String[] args){
               ArrayList<ShowData> planetList=new ArrayList<ShowData>();
               planetList.add(new ShowData("Mercury","Yellow"));
               planetList.add(new ShowData("Venus","Blue"));
               planetList.add(new ShowData("Earth","Green"));
               planetList.add(new ShowData("Mars","Red"));
               planetList.add(new ShowData("Jupiter","Orange"));
               System.out.println("Sorting according to ascending order of Planet: ");
               Collections.sort(planetList,new NameComparator());
                for(ShowData data: planetList){
               System.out.println(data.getName()+"\t "+data.getColor());
                     }
                     System.out.println();
                System.out.println("Sorting according to descending order of Color: ");
                Collections.sort(planetList,new ColorComparator());
                for(ShowData data: planetList){
               System.out.println(data.getName()+"\t "+data.getColor());
                     }
         }
}

Ads









Related Tutorials/Questions & Answers:
Collections
Collections  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
collections
collections  in collections can we perform binarysearch on arraylist elements without sorting them
Advertisements
collections
collections  why we need collections.when we will use collections in our applications
what is collections?
what is collections?  what is collections
Collections API
Collections API  hello, What is the Collections API?   hi The Collections API is a set of classes and interfaces that support operations on collections of objects
Collections in Java
Collections in Java  What is Concurrent Hash map
Collections in Java
Collections in Java  What is Concurrent Hash map
java collections
java collections  what are all the methods and things added for collections in java5 and java6 please kindly reply me as soon as possible
Version of commons-collections>commons-collections dependency
List of Version of commons-collections>commons-collections dependency
Version of excalibur-collections>excalibur-collections dependency
List of Version of excalibur-collections>excalibur-collections dependency
Collections Framework
Collections 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 java
collections 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
Version of commons-collections>commons-collections-testframework dependency
List of Version of commons-collections>commons-collections-testframework dependency