0

As the title says, I have a question. I have 2 arrays. One length is five. The other one is seven in length.

Even if the objects in the two arrays are different, if the objects in the array have the same id, it will be judged to have the same object.

There are a lot of answers about javascript, but it seems like there aren't many answers about java. Anyone who has had the same problem can share the solution. If you have any similar questions to my question, please share. Thank you so much.

Below are my attempts and my sample data.

"maxCastings"

[
        {
          "characterNo": "100121",
          "characterName": "막심 드 윈터",
          "manNo": "",
          "manName": "",
         
        },
        {
          "characterNo": "100122",
          "characterName": "댄버스 부인",
          "manNo": "3727",
          "manName": "옥주현",
          
        },
        {
          "characterNo": "100123",
          "characterName": "나",
          "manNo": "29655",
          "manName": "이지혜",
        
        },
        {
          "characterNo": "100124",
          "characterName": "잭 파벨",
          "manNo": "4202",
          "manName": "이창용",
         
        },
        {
          "characterNo": "100125",
          "characterName": "반 호퍼 부인",
          "manNo": "40282",
          "manName": "한유란",
         
        },
        {
          "characterNo": "100126",
          "characterName": "베아트리체",
          "manNo": "3543",
          "manName": "류수화",
         
        },
        {
          "characterNo": "100128",
          "characterName": "프랭크 크롤리",
          "manNo": "42618",
          "manName": "임정모",
         
        },
        {
          "characterNo": "100130",
          "characterName": "줄리앙 대령",
          "manNo": "31476",
          "manName": "김현웅",
         
        }
      ]

and 5 objects of list is this.

"castings"

[
  {
    "characterNo": "100121",
    "characterName": "막심 드 윈터",
    "manName": "에녹"
  },
  {
    "characterNo": "100122",
    "characterName": "댄버스 부인",
    "manName": "옥주현"
  },
  {
    "characterNo": "100123",
    "characterName": "나",
    "manName": "임혜영"
 },
  {
    "characterNo": "100124",
    "characterName": "잭 파벨",
    "manName": "최민철"
  },
  {
    "characterNo": "100125",
    "characterName": "반 호퍼 부인",
    "manName": "김지선"
  },
  {
    "characterNo": "100126",
    "characterName": "베아트리체",
    "manName": "김경호"
  }
]

If the manName is different but characterNo is the same, I want to check that both arrays have the same element.

What I want is 2 out of 7 not the same element.

here is my code

List<Casting> unMatchList = maxCastings.stream().filter(o1 -> castings.stream().noneMatch(o2 -> o2.getCharacterNo().equals(o1.getCharacterNo()))).collect(
                                        Collectors.toList());

and this returns

Error:(737, 95) java: local variables referenced from a lambda expression must be final or effectively final

horoyoi o
  • 584
  • 1
  • 8
  • 29
  • A couple of issues here. First, I have never seen a variable name that starts with a numeral. Is this tested code? Second, `final or effectively final` error is thrown when you use some reference **inside a lambda** that was declared outside and its **referenced object** changes. So, here if the object `5array` points to changes outside the lambda. – Sree Kumar Nov 11 '21 at 12:04
  • Does this answer your question? [local variables referenced from a lambda expression must be final or effectively final](https://stackoverflow.com/questions/27592379/local-variables-referenced-from-a-lambda-expression-must-be-final-or-effectively) – alexanderbird Nov 11 '21 at 21:29
  • @sree-kumar Of course, the variable names are the part written in pseudo code. I thought this would be easier to understand. – horoyoi o Nov 12 '21 at 00:23
  • @horoyoio Thanks for clarifying. One more point, if you implement `equals()` on your class using `characterNo`, you don't even need streams-based filtering. All you need is a `7list.removeAll( 5list )`, (need a collection, hence **list**) and only those that are exclusively in `7list` will remain in that list. But that is only a question of preference. – Sree Kumar Nov 12 '21 at 03:26

1 Answers1

1

Here's a working code sample, which I've used to recreate your problem and it work ...

    class Data {
        Integer characterNo;
        String characterName;
        String manName;

        public Data(Integer characterNo, String characterName, String manName) {
            this.characterNo = characterNo;
            this.characterName = characterName;
            this.manName = manName;
        }

        public Integer getCharacterNo() {
            return characterNo;
        }

        public void setCharacterNo(Integer characterNo) {
            this.characterNo = characterNo;
        }

        public String getCharacterName() {
            return characterName;
        }

        public void setCharacterName(String characterName) {
            this.characterName = characterName;
        }

        public String getManName() {
            return manName;
        }

        public void setManName(String manName) {
            this.manName = manName;
        }

        @Override
        public String toString() {
            return "Data{" +
                    "characterNo=" + characterNo +
                    ", characterName='" + characterName + '\'' +
                    ", manName='" + manName + '\'' +
                    '}';
        }
    }

    @Test
    void getNonMatchingList() {
        var a1 = List.of(
                new Data(1,"x","a"),
                new Data(2,"x","b")
        );

        var a2 = List.of(
                new Data(1,"x","a")
        );

        var unMatchList = a1.stream().filter(o1 -> a2.stream().noneMatch(o2 -> o2.getCharacterNo().equals(o1.getCharacterNo()))).collect(
                Collectors.toList());

        unMatchList.forEach( it -> System.out.println(it));
    }

So, your code seems to work correctly ... the only problem it has is that probably 5Array variable is not final or effectivly final. You can try declaring it final and its likely to work. If not, post the next errror you get and we can debug.

Arthur Klezovich
  • 2,595
  • 1
  • 13
  • 17