简体   繁体   English

在Java中过滤项目

[英]Filtering for items in Java

I have a package, called Things. 我有一个叫做Things的包裹。 In this package, there are regular and abstract classes such as Fruit, Food, Perishable, Shoes, Item etc. I want to create a filter that would filter these when clicked on. 在此程序包中,有常规和抽象类,例如Fruit,Food,Perishable,Shoes,Item等。我想创建一个过滤器,单击该过滤器即可对其进行过滤。 For example, clicking on the button fruit would filer for fruits. 例如,单击按钮水果将过滤水果。 In a separate package called CODE, I have 4 classes, each serving a function such as importing my data and methods, running the app, the GUI and the controller. 在一个单独的名为CODE的程序包中,我有4个类,每个类提供一个功能,例如导入我的数据和方法,运行应用程序,GUI和控制器。 I've set up the GUI class to show the button, however, I am confused on how to get Java to filter my items in the "listening" class - a class which responds to the buttons clicked in the GUI class. 我已经设置了GUI类来显示按钮,但是,我对如何获取Java来过滤“侦听”类中的项目感到困惑,该类可响应GUI类中单击的按钮。 How do I approach writing the code? 我该如何编写代码? I have tried the following, but it doesn't work. 我已经尝试了以下方法,但是没有用。 Do I have to import things.food in the code? 我必须在代码中导入Things.food吗?

In another post there was this code: 在另一篇文章中有以下代码:

List<Person> beerDrinkers = persons.stream()
.filter(p -> p.getAge() > 16).collect(Collectors.toList());

//Lets say I edit this to make it similar to my problem
List<Item> Perishables = Food.stream()
.filter(p -> p.getName()).collect(Collectors.toList());

That would not work, and it gives me errors. 那是行不通的,它给了我错误。 Could someone explain how this code works and how I could apply it to my problem? 有人可以解释此代码的工作原理以及如何将其应用于我的问题吗?

// I currently have this empty in my controller class to use as a filter. Am I suppose to filter over here or in the class that holds the methods?
else if(text.equals("Food")){
    }

package Products; 包装产品; public abstract class Food extends Item { 公共抽象类食品扩展项{

String Expiration_Date;

public String toString()
{
    return super.toString() + ", Expiration: " + Expiration_Date;
}
}

package Products;

public class Fruit extends Perishable {

public Fruit(String s)
{
    StringTokenizer stk = new StringTokenizer(s);

    stk.nextToken();

    this.Item_name = stk.nextToken();
    this.Item_code = stk.nextToken();
    this.price = Double.parseDouble(stk.nextToken());
    this.Expiration_Date = stk.nextToken();
    if (stk.nextToken().compareTo("false") == 0)
        this.flag = false;
    else
        this.flag = true;
    this.inventory = Integer.parseInt(stk.nextToken());

}

public String toString()
{
    return super.toString();
}

}

The code that you referred to is using JDK 1.8. 您引用的代码正在使用JDK 1.8。 You should check your jdk version by executing javac -version in the console. 您应该通过在控制台中执行javac -version来检查jdk版本。

List<Item> foods = perishables.stream()
.filter(p -> p.getSimpleName().equals("Food")).collect(Collectors.toList());

Referring to the source, Food is a list for Item? 参考消息来源,食物是项列表? List.stream() returns Stream interface with the collection. List.stream()返回带有集合的Stream接口。 Stream is a interface of JDK 1.8 that we can create a closure to operate the collection element. Stream是JDK 1.8的接口,我们可以创建一个闭包来操作collection元素。 You should return a bool value in the filter method. 您应该在filter方法中返回布尔值。

stream() is a method of class that implements Collection , so we use this method of an ArrayList(or others) of Perishable to get Stream interface with Perishable objects. stream()是实现Collection的类的方法,因此我们使用Perishable的ArrayList(或其他)的此方法来获取带有Perishable对象的Stream接口。 Then use filter method to filter Perishable objects and return a new stream with filtered objects. 然后使用过滤器方法过滤易腐烂的对象,并返回包含过滤对象的新流。 Finally using collect(Collectors.toList()) to accumulate filtered objects to an ArrayList. 最后,使用collect(Collectors.toList())将过滤的对象累积到ArrayList中。

BTW Class.getSimpleName can return a short class name without package names. BTW Class.getSimpleName可以返回没有包名的简短类名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM