collections

Ads
 

collections

1.Create class Dog with instance variable breed and height. Create 5 instances of Dog and add it to the Treeset dogSet. The Treeset should be in sorted order of height using a Comparator interface. Iterate through the Treeset using a for-each loop and print the height and breed of each Dog.

View Answers

March 9, 2011 at 5:47 PM

import java.util.*;

class Dog {
        String breed;
        int height;
        Dog(String breed,int height){
        this.breed=breed;
        this.height=height;
        }
        public void setBreed(String breed){
                this.breed = breed;
        }
        public String getBreed(){
                return breed;
        }
        public void setHeight(int height) {
                this.height = height;
        }
        public int getHeight(){
                return height;
        }
       }
class HeightComparator implements Comparator{

    public int compare(Object o1, Object o2){    
       int h1 = ((Dog)o1).getHeight();        
       int h2 = ((Dog)o2).getHeight();
       return h1-h2;
 }

}
public class TreeSetEx{
         public static void main(String[] args){
               TreeSet<Dog> dogset=new TreeSet<Dog>(new HeightComparator());
               dogset.add(new Dog("German Shepherd",25));
               dogset.add(new Dog("Pomeranian",10));
               dogset.add(new Dog("Skye Terrier",9));
               dogset.add(new Dog("Boxer",24));
               dogset.add(new Dog("Mastiff",29));
               System.out.println("Sorting according to the Height: ");
               for(Dog data: dogset){
               System.out.println(data.getBreed()+"\t "+data.getHeight());
               }                    
         }
}

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 in Java
Collections in Java  What is Concurrent Hash map
Collections in Java
Collections in Java  What is Concurrent Hash map
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
Version of commons-collections>commons-collections-testframework dependency
List of Version of commons-collections>commons-collections-testframework 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
collections
collections
collections
collections
Collections
collections
collections
collections
Collections
collections
collections
collections
collections
Collections
collections
collections
java collections
achieve this plz without using stringtokenizer with any collections!!!  
Hibernate Criteria With Collections Example
Hibernate 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
Version of com.jonlandrum>collections dependency
List of Version of com.jonlandrum>collections dependency
Version of com.twineworks>collections dependency
List of Version of com.twineworks>collections dependency

Ads