Share on Google+Share on Google+

1 Answer(s)      6 years and 7 months ago
Posted in : Java Beginners

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){
        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());


Related Tutorials/Questions & Answers:
Collections  Hi, Please send a study material on collections.../java/jdk6/introduction-collections-api.shtml
collections  in collections can we perform binarysearch on arraylist elements without sorting them
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 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


Advertisement null