justtestingout
justtestingout

Reputation: 31

Errors iterating on LinkedList

I'm getting an error for return on method declaration.

I have a feeling that also my iterator.element() is an issue.

Trying to find duplicates

I'm attempting to find the duplicates within a LinkedList using the iterator of Collection instead of implementing from scratch.

import java.util.*;

class Main {

  public static removeDupes(LinkedList<Integer> list){
    HashSet<Integer> set = new HashSet<>();
    ListIterator iterator = list.listIterator(0);

    while (iterator.hasNext()){
      if (set.contains(iterator.element())){
        iterator.remove(iterator.element());
      }
      else{
        set.add(iterator.element());
        iterator.previous = iterator;
      }
      iterator = iterator.next();
    }
    return list;
  }



  public static void main(String[] args) {
    //create obj of linkedlist
    LinkedList<Integer> list = new LinkedList<Integer>();

    list.add(5);
    list.add(4);
    list.add(3);
    list.add(3);
    list.add(1);

    System.out.println(removeDupes(list));
  }
}

Upvotes: 0

Views: 87

Answers (3)

hc_dev
hc_dev

Reputation: 9407

💡Please post related error message as complete output when asking for resolution of an error.

How to analyse compiler errors

The compiler error is written as ouput on the console:

Main.java:5: error: invalid method declaration; return type required
    public static removeDupes(LinkedList<Integer> list){
                  ^

It's already telling you the exact position: the file and line number (here: line 5 in file Main.java) plus visually pointing to the column (here: before removeDupes) with a caret like arrow.

The problem description for this position:

error: invalid method declaration; return type required

can be interpreted as error in two parts (separated by semicolon):

  • in general an invalid syntax: "invalid method declaration"
  • in detail a missing requirement: "return type required"

Note that compiler besides error also may output warning and info messages.

What to learn from this error

You will always get this error when your method has no return type declared, before the name. Only method that must not have a return type declared is a constructor.

How to solve this error

In your case:

  • either add a return type matching your return statement return list, e.g. LinkedList<Integer> removeDupes(LinkedList<Integer> list)
  • or add void (nothin returned) and removing the return statement like void removeDupes(LinkedList<Integer> list)

Upvotes: 1

Koushlendra
Koushlendra

Reputation: 290

You need to provide the return type in your method declaration.

public static removeDupes(LinkedList<Integer> list){

should be

public static List<Integer> removeDupes(LinkedList<Integer> list){

If you are trying to find duplicate values this will not be the optimal solution. If you want I could share the one.


    public static void main(String[] args) {
        List<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(2);
        list.add(4);
        list.add(1);
        list.add(5);
        Function<Set<Integer>, Predicate<Integer>> duplicateElements = integerSet -> n1 -> !integerSet.add(n1);
        System.out.println(getDataFromList(list, duplicateElements));
    }

    public static Set<Integer> getDataFromList(List<Integer> linkedList, Function<Set<Integer>, Predicate<Integer>> function) {
        Set<Integer> items = new HashSet<>();
        Set<Integer> collect = linkedList.stream()
                .filter(function.apply(items))
                .collect(Collectors.toSet());
        return collect;
    }

Output: [1, 2] as 1,2 are the duplicate elements

Upvotes: 2

AP11
AP11

Reputation: 617

You have a lot of mistakes, which means you really don't know how to work with Iterator. You should look up some kind of "tutorial", e.g. here.

Now It's not a good code, but it should serve as an example of your mistakes:

public static LinkedList<Integer> removeDupes(LinkedList<Integer> list){
    HashSet<Integer> set = new HashSet<>();
    ListIterator iterator = list.listIterator();
    while (iterator.hasNext()){
        // get current Integer with next()
        int current = (Integer) iterator.next();
        if (set.contains(current)) {
            list.remove(current);
        } else {
            set.add(current);
        }
    }
    return list;
}

EDIT: If you don't want to have duplicates, you should use Set instead of List.

Upvotes: 2

Related Questions