简体   繁体   English

试图找出一个java列表对象是否有元素STRING或INT?

[英]Trying to find if a java list object has an element STRING or INT?

I am trying to find if a list contains a string. 我试图找到一个列表是否包含一个字符串。 I have a list object as follows: 我有一个列表对象如下:

Please note this is just example code, to illustrate my point/question! 请注意这只是示例代码,以说明我的观点/问题!

import java.util.List;

public class FilterByList {

    private String actionHero;
    private String actionHero2;
    private String move;
    private int number;
    private String actionHero3;

    public FilterByList(String actionHero, String actionHero2, String move, int number, String actionHero3) {
        this.actionHero = actionHero;
        this.actionHero2 = actionHero2;
        this.move = move;
        this.number = number;
        this.actionHero3 = actionHero3;
    }

    public String getActionHero() {
        return actionHero;
    }

    public void setActionHero(String actionHero) {
        this.actionHero = actionHero;
    }

    public String getActionHero2() {
        return actionHero2;
    }

    public void setActionHero2(String actionHero2) {
        this.actionHero2 = actionHero2;
    }

    public String getMove() {
        return move;
    }

    public void setMove(String move) {
        this.move = move;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getActionHero3() {
        return actionHero3;
    }

    public void setActionHero3(String actionHero3) {
        this.actionHero3 = actionHero3;
    }
}

And then: 接着:

public static void main(String[] args) {

    List<FilterByList> myList = Collections.singletonList(
    new FilterByList("superman", "antman", "ACTION", 123, "batman"));

    System.out.println(myList);
    if (myList.contains("batman")) {
        System.out.println("found it!");
    } else {
        System.out.println("************************* Did not find anything!!1");
    }
}

It does NOT find batman in the list object. 它在列表对象中找不到batman So what is printed is the follows: 那么印刷的是以下内容:

[com.company.FilterByList@7577b641]
************************* Did not find anything!!1

Any idea how I can: 知道我怎么做:

a) print the contents of the list? a)打印列表的内容?

b) find an element in the list with as little code as possible? b)用尽可能少的代码在列表中找到一个元素?

Would appreciate your help. 非常感谢你的帮助。 Please use my code context to answer as it will give me more pointers. 请使用我的代码上下文来回答,因为它会给我更多指示。

You run contains on List<FilterByList> and that list doesn't have String batman It has instance of FilterByList, that one of members is field of type string and value ' batman ' 你在List<FilterByList>上运行contains并且该列表没有String batman它有FilterByList的实例,其中一个成员是string类型的字段和值' batman '

Based on your code. 根据您的代码。 You create instance of object FilterByList and you try to compare that object with String 您创建对象FilterByList的实例,并尝试将该对象与String进行比较

Line 线

if (myList.contains("batman")) {

Those object are different types, that is the reason, why it is not found 那些对象是不同的类型,这就是为什么没有找到它的原因

To check if there is batman you can use Stream API 要检查是否有蝙蝠侠,您可以使用Stream API

boolean d = myList.stream().map(FilterByList::getActionHero3).anyMatch(hero3 -> hero3.equals("batman"));

Add to your FilterByList class containsHero(String hero) where you compare hero with each of 3 heroes. 添加到您的FilterByList类containsHero(String hero) ,您可以将hero与3个英雄中的每一个进行比较。 You don't need to store this single object in List. 您不需要在List中存储此单个对象。 Just use it. 只是使用它。

  FilterByList f = new FilterByList("superman", "antman", "ACTION", 123, "batman");
  if (f.containsHero("batman")) {
      System.out.println("found it!");
  } else {
      System.out.println("************************* Did not find anything!!1");
  }

PS imho all the things you are trying to do looks very strange... PS imho所有你想要做的事情看起来很奇怪......

I added a new method to the class for searching 我在课堂上添加了一个新方法进行搜索

public boolean containsHero(String hero) {
    return actionHero.equals(hero) ||  actionHero2.equals(hero) ||  actionHero3.equals(hero);
}

And then used it with streams like this 然后将它与这样的流一起使用

if (myList.stream().anyMatch(f -> f.containsHero("batman"))) {
    System.out.println("found it!");
} 

To get a readable output of your class you can override the toString() method, here is one example 要获得类的可读输出,可以覆盖toString()方法,这是一个示例

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append(actionHero);
    builder.append(", ");
    builder.append(actionHero2);
    builder.append(", ");

    builder.append(actionHero3);
    builder.append(": ");

    builder.append(move);
    builder.append(": ");

    builder.append(number);

    return builder.toString();
}

Doing System.out.println(myList); System.out.println(myList); will then output 然后输出

[superman, antman, batman: ACTION: 123] [超人,蚂蚁,蝙蝠侠:行动:123]

You can put into the list two different types of objects, eg String and Integer if you will omit the generic type , and implicitly it will be List<Object> myList = new ArrayList<Object>(); 如果省略泛型类型 ,可以在列表中放入两种不同类型的对象,例如StringInteger ,隐式它将是List<Object> myList = new ArrayList<Object>(); for example: 例如:

List myList = new ArrayList();
myList.add("superman");
myList.add("antman");
myList.add("ACTION");
myList.add(123);
myList.add("batman");

System.out.println(myList);
if (myList.contains("batman")) {
    System.out.println("found it!");
} else {
    System.out.println("************************* Did not find anything!!1");
}

But it's not a good way to use collections. 但这不是使用集合的好方法。 For sure, it depends of your particular task, but I'd like to advice to use different collections with different types. 当然,这取决于您的特定任务,但我想建议使用不同类型的不同集合。

If you want to achieve exactly what you're asking about, you can use Stream API like this: 如果您想要完全满足您的要求,可以像这样使用Stream API

List<FilterByList> myList = Collections.singletonList(
new FilterByList("superman", "antman", "ACTION", 123, "batman"));

System.out.println(myList);
if (myList.stream().map(FilterByList::getActionHero3).allMatch("batman"::equals)) {
    System.out.println("found it!");
} else {
    System.out.println("************************* Did not find anything!!1");
}

anyMatch method is more preferable to use, if there will be more than one element in the collection: 如果集合中有多个元素,则更可取使用anyMatch方法:

(myList.stream().map(FilterByList::getActionHero3).anyMatch("batman"::equals))

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

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