-1

I need to sort my Linkedlist by collections.sort(). I am thinking using it without writing comprator but eclipse does not recognize it and says "Collections cannot be resolved" and asks me to import Collections although i have imported java.util.*. If it is not possible without wrting comprator, would you please help me with writing that?

`package MergeTwoSortedLists;

import java.util.*;

public class Driver {

public static void main(String[] args) {
    
Scanner scanner= new Scanner(System.in);
String str1= scanner.nextLine();
char[] char_arr1=str1.toCharArray();
List<Character> lnk_List1= new LinkedList<>();
   for ( int i=0; i<char_arr1.length; i++) {
    lnk_List1.add(char_arr1[i]);
   }
Collections.sort(lnk_List1);
System.out.println(lnk_List1);
}

}

` Would you please help?

  • 1
    Does it work if you fully qualify the import: `import java.util.Collections;`? – knittl May 16 '23 at 19:44
  • That said, I cannot reproduce the error. Here's an ideone link showing successful _compilation_ of your source code: https://ideone.com/6B4mga – knittl May 16 '23 at 19:59
  • Only eclipse complains or when you try to compile from the command line receive an error? Most probably is a project or eclipse configuration issue. – Andrés Alcarraz May 16 '23 at 21:04
  • 1
    Side note: this is a typical case where an `ArrayList` is more appropriate. As said in [When to use LinkedList over ArrayList in Java?](https://stackoverflow.com/q/322715/2711488), “*If you're not sure — just start with `ArrayList`.*” Further, you can simply use `lnk_List1.sort(null);` without the helper class `Collections`; it seems you’re following a very old textbook. But even that is an unnecessary complication here, as you can simply use `Arrays.sort(char_arr1); System.out.println(char_arr1);` without copying into a `List`. – Holger May 17 '23 at 07:43
  • The import java.util.Collections cannot be resolved, I receive this!! – user21386103 May 18 '23 at 02:48
  • I cleaned the project and now it is solved. Thank you all. – user21386103 May 18 '23 at 03:01

1 Answers1

0

When I cleaned the project and run it again, it worked. Thanks all.

Project > Clean

but now my question is why and how Collections.sort() is able to sort my linked List without writing: Collections.sort(LinkedList, new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.length() - s2.length(); } } ); Here is my code which is working( I can have my sorted LinkedList) just by writing Collectins.sort()!

package MergeTwoSortedLists;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;

public class Driver
{
  public static void main(String[] args)
  {
   Scanner scanner= new Scanner(System.in);
   String str1= scanner.nextLine();
   String str2= scanner.nextLine();
   
   String[] str_arr1=str1.split(" ");;
   String[] str_arr2=str2.split(" ");
   System.out.println("String Arrays");
   System.out.println(Arrays.toString(str_arr1));
   System.out.println(Arrays.toString(str_arr2));
   
   int[] int_arr1= new int[str_arr1.length];
   int[] int_arr2= new int[str_arr2.length];
   for (int i=0; i<str_arr1.length; i++) {
       int_arr1[i]=Integer.parseInt(str_arr1[i]);
   }
   for (int i=0; i<str_arr2.length; i++) {
       int_arr2[i]=Integer.parseInt(str_arr2[i]);
   }
   System.out.println("int Arrays");
   System.out.println(Arrays.toString(int_arr1));
   System.out.println(Arrays.toString(int_arr2));

   LinkedList<Integer> lnk_List1= new LinkedList<Integer>();
   LinkedList<Integer> lnk_List2= new LinkedList<Integer>();
   for (int i=0; i<int_arr1.length; i++) {
       lnk_List1.add(int_arr1[i]);
   }
   for(int i=0; i<int_arr2.length; i++) {
       lnk_List2.add(int_arr2[i]);
   }
   System.out.println("int linkedList");
   System.out.println(lnk_List1);
   System.out.println(lnk_List2);
   
   LinkedList<String> lnk_List3= new LinkedList<String>();
   LinkedList<String> lnk_List4= new LinkedList<String>();
   for (int i=0; i<str_arr1.length; i++) {
       lnk_List3.add(str_arr1[i]);
   }
   for(int i=0; i<str_arr2.length; i++) {
       lnk_List4.add(str_arr2[i]);
   }
   System.out.println("String linkedList");
   System.out.println(lnk_List3);
   System.out.println(lnk_List4);
        
   Collections.sort(lnk_List1);
   Collections.sort(lnk_List2);
   System.out.println("Sorted linke_Lists:1,2:");
   System.out.println(lnk_List1);
   System.out.println(lnk_List2);

   Collections.sort(lnk_List3);
   Collections.sort(lnk_List4);
   System.out.println("Sorted linke_Lists:3,4:");
   System.out.println(lnk_List3);
   System.out.println(lnk_List4);
}

}

A you see Collections.sort () without writting Comprator is working for Integer and String, both.

Thank you.