Home > database >  how to extend the for each loop to all objects?
how to extend the for each loop to all objects?

Time:05-29

I don't know how to check each record with books in my linked list if I change if (book.getPubyear() > 2000) { to < 2000 I get abcd 6 times. I don't know how to check every book not only first one

public class sredniaocen {
    
    class Book {
        int pubyear;
        String title;
    
        public Book(String title, int pubyear) {
            this.title = title;
            this.pubyear = pubyear;
        }
    
        public String getTitle() {
            return this.title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public int getPubyear() {
            return this.pubyear;
        }
    
        public void setPubyear(int pubyear) {
            this.pubyear = pubyear;
        }
    
        public static void main(String args[]) {
            sredniaocen.Book book = new sredniaocen().new Book("abcd", 1998);
            sredniaocen.Book book1 = new sredniaocen().new Book("efgh", 1999);
            sredniaocen.Book book2 = new sredniaocen().new Book("ijkl", 2000);
            sredniaocen.Book book3 = new sredniaocen().new Book("mnou", 2001);
            sredniaocen.Book book4 = new sredniaocen().new Book("prst", 2002);
            sredniaocen.Book book5 = new sredniaocen().new Book("wxyz", 2003);
    
            LinkedList<Book> booklist = new LinkedList<Book> ();
            booklist.add(book);
            booklist.add(book1);
            booklist.add(book2);
            booklist.add(book3);
            booklist.add(book4);
            booklist.add(book5);
    
            for (Book title: booklist) {
                if (book.getPubyear() > 2000) {
                    System.out.println(book.title);
                }
            }
            System.out.println("Książka "   book.title   " "   book.pubyear);
        }

}

I spent a lot of hours today but I don't know.

CodePudding user response:

I think you probably don't need to use a LinkedList for your use case:

import java.util.StringJoiner;

public class Book {
    private String title;
    private int pubYear;

    public Book(String title, int pubYear) {
        this.title = title;
        this.pubYear = pubYear;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getPubYear() {
        return this.pubYear;
    }

    public void setPubYear(int pubYear) {
        this.pubYear = pubYear;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Book.class.getSimpleName()   "[", "]")
            .add("title='"   title   "'")
            .add("pubYear="   pubYear)
            .toString();
    }
}
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Book book0 = new Book("abcd", 1998);
        Book book1 = new Book("efgh", 1999);
        Book book2 = new Book("ijkl", 2000);
        Book book3 = new Book("mnou", 2001);
        Book book4 = new Book("prst", 2002);
        Book book5 = new Book("wxyz", 2003);
        List<Book> booksList = Arrays.asList(book0, book1, book2, book3, book4, book5);
        System.out.printf("All books: %s%n", booksList);
        System.out.println("Books published after the year 2000:");
        for (Book book : booksList) {
            if (book.getPubYear() > 2000) {
                System.out.println(book);
            }
        }
    }
}

Output:

All books: [Book[title='abcd', pubYear=1998], Book[title='efgh', pubYear=1999], Book[title='ijkl', pubYear=2000], Book[title='mnou', pubYear=2001], Book[title='prst', pubYear=2002], Book[title='wxyz', pubYear=2003]]
Books published after the year 2000:
Book[title='mnou', pubYear=2001]
Book[title='prst', pubYear=2002]
Book[title='wxyz', pubYear=2003]

Note: If the books in your real list are sorted by pubYear, consider iterating over your list backwards and breaking as soon as you see a year less than 2000, or even better using Collections.binarySearch to find the split point in O(log n) time.

CodePudding user response:

Before I did not use the iterating variable form the loop as title but a previous instance:

for (Book title: booklist) {
    if (book.getPubyear() > 2000) {  // must be title instead book

So I changed:

LinkedList<Book> booklist = new LinkedList<Book>();
booklist.add(new Book("abc", 1998));
booklist.add(new Book("def", 1999));
booklist.add(new Book("ghi", 2000));
booklist.add(new Book("jkl", 2001));
booklist.add(new Book("mno", 1978));
booklist.add(new Book("prs", 2012));
booklist.add(new Book("tuw", 2006));


for (Book book : booklist) {
  if (book.getPubyear() > 2000) {
    System.out.println(book.getTitle());
  }
}

Now it works as expected.

CodePudding user response:

You can Stream the linkedList, filter it based on publish year greather than 2000, collect them to a list and print it

   public static void main(String[] args)
{
    LinkedList<Book> booklist = new LinkedList<Book>();
    booklist.add(new Book("abc", 1998));
    booklist.add(new Book("def", 1999));
    booklist.add(new Book("ghi", 2000));
    booklist.add(new Book("jkl", 2001));
    booklist.add(new Book("mno", 1978));
    booklist.add(new Book("prs", 2012));
    booklist.add(new Book("tuw", 2006));
    booklist.stream().filter(book -> book.getPubYear()>2000).collect(Collectors.toList()).stream().forEach(book -> System.out.println(book));
}

Output:

Book[title='jkl', pubYear=2001]
Book[title='prs', pubYear=2012]
Book[title='tuw', pubYear=2006]
  •  Tags:  
  • java
  • Related