sorting Set - program hangs

Dear experts

I am trying to sort a set. My code below
public class Car implements Comparator<Car>{  
	
	private String model;
	private int year;
	
	public int compare(Car c1, Car c2){
		//return c2.getModel().compareTo(c1.getModel()); //sort by model
		return c2.getYear() - c1.getYear();  //sort by Year
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

public static class MyCarTest{
Car c1 = new Car();		
Car c2 = new Car();
Set<Car> carSet = new HashSet<>();
		
private void addSet(){
			c1.setModel("Mercedes");
			c1.setYear(2012);
			
			c2.setModel("BMW");  //BMW
			c2.setYear(2013);    // 2014
			
			carSet.add(c1);
			carSet.add(c2);
			
			List<Car> carList = new ArrayList<Car>(carSet);
			//Collections.sort(carList);
			for(Car cars : carSet){
			System.out.println("Cars in set are>>" +cars.getModel() + "-"+ cars.getYear() );
			}
		}
public static void main(String[] args){
		MyCarTest test = new MyCarTest();
		test.addSet();
	}

Open in new window


When i uncomment this line >>Collections.sort(carList);
and try to save my java file, eclipse just freezes, happened 4 times already.
any idea what could be wrong?
thanks.
royjaydAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krakatoaCommented:
Cast it to ArrayList ?

can't get your code to compile - errors are

C:\EE_Q_CODE>javac Car.java
Car.java:1: error: cannot find symbol
public class Car implements Comparator<Car>{
                            ^
  symbol: class Comparator
Car.java:30: error: cannot find symbol
Set<Car> carSet = new HashSet<>();
^
  symbol:   class Set
  location: class MyCarTest
Car.java:30: error: cannot find symbol
Set<Car> carSet = new HashSet<>();
                      ^
  symbol:   class HashSet
  location: class MyCarTest
Car.java:42: error: cannot find symbol
                        List<Car> carList = new ArrayList<Car>(carSet);
                        ^
  symbol:   class List
  location: class MyCarTest
Car.java:42: error: cannot find symbol
                        List<Car> carList = new ArrayList<Car>(carSet);
                                                ^
  symbol:   class ArrayList
  location: class MyCarTest
5 errors
gurpsbassiCommented:
Your code compiles fine on my eclipse.
Of course on line 53 and 53 I added a cure brace each since you have a class within an outer class.

Try looking through the eclipse error log.
Window > Show View > Other > Error Log

It might reveal something.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
You should really have your source files separate - especially if you're in an IDE anyway

Do that and you'll get

Cars in set are>>BMW-2013
Cars in set are>>Mercedes-2012

Open in new window


(a descending year sort since that's what you specified)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.