Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot be cast to java.lang.Comparable

Tags:

java

import java.util.HashMap; import java.io.*; import java.util.*; public class Fegan implements Comparable{ HashMap<String, Integer> cart = new HashMap<String, Integer>(); List list = new ArrayList<FoodItems>(); int x =0; public void addToCart(FoodItems f) {     cart.put(f.name, f.valueOfFood); } public String display(FoodItems f) {     return(f.name + " costs " + f.valueOfFood); } public void addToList(FoodItems f) {     //FoodItems temp = (FoodItems) f;     list.add(f); } public int compareTo(Object o) {     //FoodItems temp = (FoodItems) o;     if(this.x == ((FoodItems)o).valueOfFood)         return 0;     else if (this.x <((FoodItems)o).valueOfFood)         return 1;     else          return -1; } public void sortMap(List list) {     for(int i =0; i< list.size(); i++)     {         FoodItems temp = (FoodItems) list.get(i);         cart.put(temp.name, temp.valueOfItem);      } } } 

import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.io.*; import java.util.*; public class Test { public static void main(String [] args) {     HashMap<String, Integer> cart = new HashMap<String, Integer>();     FoodItems firts = new FoodItems("Chocolate" , 50);     FoodItems second = new FoodItems("Juice", 79);     FoodItems third = new FoodItems("Apple", 200);     FoodItems forth = new FoodItems("Orange", 300);     FoodItems fifth = new FoodItems("Milk" , 400);     ArrayList items = new ArrayList();     items.add(firts);     items.add(second);     items.add(third);     items.add(forth);     items.add(fifth);     Collections.sort(items);     Iterator itr = items.iterator();     Fegan myFegan = new Fegan();     myFegan.sortMap(items);      while(itr.hasNext()){         Object element = itr.next();         System.out.println(element + "\n");     } } } 

Why it is writing:

Exception in thread "main" java.lang.ClassCastException: FoodItems cannot be cast to java.lang.Comparable at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source) at java.util.ComparableTimSort.sort(Unknown Source) at java.util.ComparableTimSort.sort(Unknown Source) at java.util.Arrays.sort(Unknown Source) at java.util.Collections.sort(Unknown Source) at Test.main(Test.java:21) 
like image 863
who must not be named Avatar asked Jan 02 '12 09:01

who must not be named


People also ask

How to solve ClassCastException?

// type cast an parent type to its child type. In order to deal with ClassCastException be careful that when you're trying to typecast an object of a class into another class ensure that the new type belongs to one of its parent classes or do not try to typecast a parent object to its child type.

What is Java comparable interface?

The Java Comparable interface, java. lang. Comparable , represents an object which can be compared to other objects. For instance, numbers can be compared, strings can be compared using alphabetical comparison etc. Several of the built-in classes in Java implements the Java Comparable interface.

What can you cast in Java?

Type casting is a way of converting data from one data type to another data type. This process of data conversion is also known as type conversion or type coercion. In Java, we can cast both reference and primitive data types. By using casting, data can not be changed but only the data type is changed.


1 Answers

  • the object which implements Comparable is Fegan.

The method compareTo you are overidding in it should have a Fegan object as a parameter whereas you are casting it to a FoodItems. Your compareTo implementation should describe how a Fegan compare to another Fegan.

  • To actually do your sorting, you might want to make your FoodItems implement Comparable aswell and copy paste your actual compareTo logic in it.
like image 100
plus- Avatar answered Sep 28 '22 02:09

plus-