Collections not accepted Eclipse

import java.io.BufferedReader; import java.util.Collections; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; import com.javaranch.common.TextFileIn; public class SortNames { public static class CelebrityNamesFile { public String firstName; public String lastName; public static class CompareLastName implements Comparator< CelebrityNamesFile > { @Override public int compare( CelebrityNamesFile o1, CelebrityNamesFile o2 ) { return o2.lastName.compareTo( o1.lastName ); } } public static void main( String[ ] args ) throws IOException { ArrayList< CelebrityNamesFile > myCelebrityList; myCelebrityList = new ArrayList< CelebrityNamesFile >(); TextFileIn celebrityNamesFile = new TextFileIn( "celebrityNamesFile.txt" ); boolean doneReadingCelebrityNames = false; while ( ! doneReadingCelebrityNames ) { String oneName = celebrityNamesFile.readLine(); if ( oneName == null ) { doneReadingCelebrityNames = true; } 

$ Eclipse does not like the following add statement, namely: the add method (SortNames.CelebrityNamesFile) of type ArrayList (SortNames.CelebrityNamesFile) is not applicable for arguments (String)

  else { myCelebrityList.add( oneName ); } } celebrityNamesFile.close(); 

$ And then it doesn’t like my query of the form, namely: Associated mismatch: the general method sort (List T) of type Collections is not applicable for arguments (ArrayList (SortNames.CelebrityNamesFile)). The deduced type SortNames.CelebrityNamesFile is not a valid substitute for a limited parameter (T extends Comparable (? Super T))

  Collections.sort( myCelebrityList ); System.out.println( myCelebrityList ); Collections.sort( myCelebrityList, new CelebrityNamesFile.CompareLastName() ); System.out.println( myCelebrityList ); } } 

}

What am I doing wrong? I read a lot of posts here, read Java docs regarding comparator, read Java tutorials on comparator. Head First Java, Ivor Horton, the beginner of Java 7. Still clueless.

The purpose of the program is to read the names from the txt file, add them to arraylist, print arithmetic in natural order, sort arraylist by last name, print the list again.

+4
source share
5 answers

For the first problem, the problem is quite simple. oneName is a String , and myCelebrityList is a collection of CelebrityNamesFile - this means that you can add objects to a collection like CelebrityNamesFile .

In the second problem, your problem is that the CelebrityNamesFile class does not implement the Comparable interface. Sorting requires an order to be executed for a list item type, for example. by implementing Comparable for a class or by providing a Comparator sort .

+12
source

For the first problem, you are trying to add a String to the List<CelebrityNamesFile> instance. You can use String to create an instance of CelebrityNamesFile and add this instance to your list:

 CelebrityNamesFile celebrityNamesFile = new CelebrityNamesFile(); celebrityNamesFile.lastName = oneName; myCelebrityList.add( celebrityNamesFile ); 

For the second problem, try

 Collections.sort(myCelebrityList, new CompareLastName()); 

As @alyu points, the CelebrityNamesFile class must implement the Comparable interface for working with the code you sent, but you can also add an instance of Comparator<CelebrityNamesFile> (and you already have a CompareLastName class that implements it).

More on this:

+2
source
  • You are trying to add a String ( oneName object) to CelebrityNamesFile s list. You need to somehow create a CelebrityNamesFile from this String .
  • Collections.sort() only works with Comparable objects, unless you explicitly pass a comparator. Try instead:

    Collections.sort(myCelebrityList, new CelebrityNamesFile.CompareLastName());

+1
source

The myCelebrityList collection is of type CelebrityNamesFile , and this means that it can only accept instances of this class. You need to create an instance of CelebrityNamesFile and add it to the collection. Collections will not accept your collection because the CelebrityNamesFile class does not implement the Comparator interface. You need to pass the comparator instance to Collections.sort() as the second parameter.

+1
source

Comparator CompareLastName implemented in the CompareLastName class can be placed outside the SortNames class SortNames or at least outside the CelebrityNamesFile class. I had the same problem until I put a class that implements my Comparator outside my object class. After I did this, the program worked perfectly. Here is an example of my code with comments if it helps.

 // Comparator interface for my Word Class. public interface Comparator<Word> { int compare(Word first, Word second); } // Word Class, which is my object. public class Word { private String word; public Word(String input) { word = input; } public String get() { return word; } public int wordSize() { return word.length(); } } // Comparator implementation is separate from my Word Class. public class WordComparator implements Comparator<Word> { public int compare(Word first, Word second) { if (first.wordSize() < second.wordSize()) { return -1; } else if (first.wordSize() > second.wordSize()) { return 1; } else { return first.get().compareTo(second.get()); } } } // Now run the program to ensure the Word Class objects and the WordComparator // Class are implemented correctly. public class WordComparatorDemo { public static void main(String[] args) throws FileNotFoundException { ArrayList<Word> list = new ArrayList<Word>(); JFileChooser find = new JFileChooser(); Scanner read = null; if(find.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { File selectedFile = find.getSelectedFile(); read = new Scanner(selectedFile); try { list = inputData(read); // Here the sort implementing the WordComparator. Collections.sort(list, new WordComparator()); } finally { read.close(); } } } public static ArrayList<Word> inputData(Scanner in) { ArrayList<Word> list = new ArrayList<Word>(); in.useDelimiter("[^A-Za-z]+"); while(in.hasNext()) { String word = in.next(); Word temp = new Word(word); list.add(temp); } return list; } } 

Note. My answer is a year after the original post, but I hope it will help anyone who comes to this site for help.

+1
source

All Articles