简体   繁体   中英

Java - Sorting an ArrayList

I am working on a program where I have to count the frequency of food items in a file in order to sort them in descending order.

For example: if my file has ( pizza, ice_cream, pasta, pizza )

I want my program to print something similar to this:

1 ice_cream 1 pasta 2 pizza

I am using a bubble sort algorithm but it seems that I am missing something for this algorithm to work. Any help will be greatly appreciated!

Within class Listabc, I have two local variables and a method called "compareTo."

class Listabc {

     int count = 1;
     String item;


     int compareTo(Listabc listabc) {
        return 0;
     }

}

Within my main method, I have a bubble sort algorithm to sort the food items in a descending order

public class MainMethod {

   public static void main(String[] args) throws FileNotFoundException {

       Scanner input = new Scanner(new BufferedReader(new FileReader("file.txt")));

       List<Listabc> lists = new ArrayList<Listabc>();

       for (int a = 0; a < lists.size() - 1; ++a) {
           for (int b = a + 1; b < lists.size(); b++) {
               if ((lists.get(b)).compareTo(lists.get(a)) > 0) {
                   Listabc temp = lists.get(a);
                   lists.set(a, lists.get(b));
                   lists.set(b, temp);
               }
           }
           System.out.println(lists.get(a));
       }

     }
 }

Your implementation of compareTo method is broken . You need to change it by applying a real comparison:

int compareTo(Listabc listabc) {
    return 0; //this means every element is "similar" to another
}

Here's an example about comparing the elements by the item field:

int compareTo(Listabc listabc) {
    return this.item.compareTo(listabc.item);
}

如果使Listabc实施具有可比性 ,则只需调用Collections.sort(lists)

Below program will sort the list using Collections.sort

    public class SortMap {

    public static void main(String[] args) {
    Map<String, Integer> t = new HashMap<String, Integer>();
    Scanner sc = null;
    try {
        sc = new Scanner(new File("foodlist.txt"));
        while (sc.hasNext()) {
            String item = sc.next();
            if (t.get(item) != null) {
                Integer count = t.get(item);
                t.put(item,++count);
            } else {
                t.put(item, 1);
            }

        }
        Set<Map.Entry<String, Integer>>  mp = t.entrySet();
        List<Map.Entry<String, Integer>> ll = new ArrayList<Map.Entry<String,Integer>>(mp);
        Collections.sort(ll, new SortMap.ValueComparator());

        System.out.println(ll);


    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        sc.close();
    }

}

static class ValueComparator implements Comparator<Map.Entry<String, Integer>> {

    public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
        return o1.getValue().compareTo(o2.getValue());
    }


}

   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM