0

I want to sort 2 List and compare them, but sorted method did not work. I have 2 String List which includes integer.

These are my Lists

List<String> WORD_OF_ADDRESS = gatekeeperPage.getWords(ADDRESS);
List<String> WORD_OF_CONTACT_ADDRESS = gatekeeperPage.getWords(CONTACT_ADDRESS);

My getwords method returns : return Arrays.asList(words);

When I try this ;

Stream<Object> sortedList = Arrays.stream(WORD_OF_ADDRESS.toArray()).sorted();
System.out.println("SORTED LİST "+ sortedList);

I get this : SORTED LİST java.util.stream.SortedOps$OfRef@3e8c3cb

What should I do to sort 2 list ?

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
  • Collect to list after you sorted it : .collect(Collectors.toList()) – Benoit Cuvelier Feb 15 '22 at 13:45
  • I tried almost everything but it didn't work. I will try again. – Batuhan Bakar Feb 15 '22 at 13:47
  • 3
    [How to print out all the elements of a List in Java?](https://stackoverflow.com/q/10168066) [How to get the String representation of all elements in a Java 8 stream](https://stackoverflow.com/q/39565865) – 001 Feb 15 '22 at 13:48
  • @BenoitCuvelier I tried now this: sortedList.collect(Collectors.toList()); But I get Result of 'Stream.collect()' is ignored ERROR. And when I run it returns SORTED LİST java.util.stream.SortedOps$OfRef@3e8c3cb – Batuhan Bakar Feb 15 '22 at 13:49
  • final List collect = words.stream().sorted().collect(Collectors.toList()); this logic should work – venkat Feb 15 '22 at 16:04
  • @venkat I don't know why but it gets same result. --> public List getWords(String address) { String[] words = address.split("(/) | (,) | ( )"); for ( int i = 0; i < words.length; i++) { words[i] = words[i].replaceAll("[^\\w]", ","); } return Arrays.asList(words); } I use this method. Can it be because of this method ? – Batuhan Bakar Feb 15 '22 at 16:24
  • @BatuhanBakar You're question says that your lists are simply a group of strings like `"A","B","C","D"`. So that is the what is expected. – WJS Feb 15 '22 at 20:31

2 Answers2

1

What you are actually displaying is a Stream. But what you want is the list of string used by this stream, change your code this way :

List<String> sortedList = WORD_OF_ADDRESS.stream()
                         .sorted()
                         .collect(Collectors.toList());

Then display your list the way Johnny Mopp suggered

Benoit Cuvelier
  • 806
  • 7
  • 23
  • I got This ERROR : no instance(s) of type variable(s) exist so that Object conforms to String inference variable T has incompatible bounds: equality constraints: String lower bounds: Object------> Required : List Provided List – Batuhan Bakar Feb 15 '22 at 13:55
  • Look at my edited answer, i didn t noticed that your WORD_OF_ADDRESS was already a list – Benoit Cuvelier Feb 15 '22 at 13:58
  • This didn't work it still comes same list. – Batuhan Bakar Feb 15 '22 at 16:06
  • public List getWords(String address) { String[] words = address.split("(/) | (,) | ( )"); for ( int i = 0; i < words.length; i++) { words[i] = words[i].replaceAll("[^\\w]", ","); } return Arrays.asList(words); } I use this method. Can this be the reason ? – Batuhan Bakar Feb 15 '22 at 16:25
-1

You can sort the lists easily as follows:

List<String> WORD_OF_ADDRESS_sorted gatekeeperPage.getWords(ADDRESS)
    .stream().sorted().toList();
List<String> WORD_OF_CONTACT_ADDRESS_sorted = gatekeeperPage.getWords(CONTACT_ADDRESS)
    .stream().sorted().toList();

Then you can compare them.

However, if you just want to see if they are equal based on contents, sorting is not necessary to test for content equality. Update a map with a running count each time the item occurs is more efficient. Add 1 for one list and subtract 1 for the other. Map.merge is useful because it permits one to replace a 0 value with null which then removes the entry. So if you end up with an empty map, the lists must be equal based on contents.

public static <T> boolean areEqual(List<T> list1, List<T> list2) {
    Map<T, Integer> map = new HashMap<>();
    if (list1.size() != list2.size()) {
        return false;
    }
    
    for (int i = 0; i < list1.size(); i++) {
        map.merge(list1.get(i), -1,
                (k, v) -> v + 1 == 0 ? null : v + 1);
        map.merge(list2.get(i), 1,
                (k, v) -> v - 1 == 0 ? null : v - 1);
    }
    
    return map.isEmpty();
}

I have not extensively tested this but with the all tests using lists of random integers, where only one is copied shuffled tested as equal. Randomly salting the lists with different values tested as false.

Since immutable lists are not altered, they can be passed directly to the method.

Caveat: Since Integers have a maximum value and minimum value, extremely large lists have the potential to fail if those limits are reached. Counting of Long or BigInteger would mitigate this problem but also decrease its performance.

WJS
  • 36,363
  • 4
  • 24
  • 39