如何按字段打印 class 对象的内容?

How to print contents of a class object by field?

我有一个 POJO class,Location 用于使用 Jackson 将 JSON 文件映射到 class。当前的实现可以通过调用 Location 的 toString() 打印出 class 中的每个 Location 对象,但我想知道如何打印,例如,仅打印 id=“2”的位置,即 name= "Desert"

目前,我使用这样的toString方法来打印Location的所有内容:

public String toString() {
        return "Location [location=" + Arrays.toString(location) + ", id=" + id
                + ", description=" + description + ", weight=" + weight
                + ", name=" + name + ", exit=" + Arrays.toString(exit)
                +"]";
    }

有谁知道如何根据字段 ID 打印 Location 对象中的特定位置?

这是我调用 toString() 时存储在位置 class 中的示例:

http://hastebin.com/eruxateduz.vhdl

Location 对象中 Locations 之一的示例:

[Location [location=null, id=1, description=You are in the city of Tiberius. You see a long street with high buildings and a castle.You see an exit to the south., weight=100, name=Tiberius, exit=[Exit [title=Desert, direction=South]]]

这是我用来将 JSON 字段映射到 class:

的 POJO 位置 class
public class Location {

    private Location[] location;

    private int id;

    private String description;

    private String weight;

    private String name;

    private Exit[] exit;

    private boolean visited = false;
    private boolean goalLocation;
    private int approximateDistanceFromGoal = 0;
    private Location parent;




    public Location[] getLocation() {
        return location;
    }

    public void setLocation(Location[] location) {
        this.location = location;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription ()
    {
        return description;
    }

    public void setDescription (String description)
    {
        this.description = description;
    }


    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public Exit[] getExit() {
        return exit;
    }

    public void setExit(Exit[] exit) {
        this.exit = exit;
    }


    public boolean isVisited() {
        return visited;
    }

    public void setVisited(boolean visited) {
        this.visited = visited;
    }

    public boolean isGoalLocation() {
        return goalLocation;
    }

    public void setGoalLocation(boolean goalLocation) {
        this.goalLocation = goalLocation;
    }

    public int getApproximateDistanceFromGoal() {
        return approximateDistanceFromGoal;
    }

    public void setApproximateDistanceFromGoal(int approximateDistanceFromGoal) {
        this.approximateDistanceFromGoal = approximateDistanceFromGoal;
    }

    public Location getParent() {
        return parent;
    }

    public void setParent(Location parent) {
        this.parent = parent;
    }

    @Override
    public String toString() {
        return "Location [location=" + Arrays.toString(location) + ", id=" + id
                + ", description=" + description + ", weight=" + weight
                + ", name=" + name + ", exit=" + Arrays.toString(exit)
                +"]";
    }



}

你可以试试gson,它输入一个对象,输出一个JSON或者在对面。

将对象设为JSON对象后,可以遍历JSON以遍历对象。

Stream.of(location).filters(l -> l.getId() == 2).foreach(System.out::println);

这样行吗?

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.0</version>
</dependency>

除非您想自己做,否则您需要上述依赖项来定义谓词。

public class Location {
   private int id;
   // more stuff here

   private Predicate<Integer> filter;
     public Location() {
          this.filter = TruePredicate.INSTANCE; 
     }
     public Location(int idFilter) {
             this.filter = new EqualPrediate(idFilter);
     }

     public String toString() {
       StringBuilder buffer = new StringBuilder();
          if(filter.apply(this.id)) { 
             buffer.append("Location [location=" + Arrays.toString(location) + ", id=" + id
                + ", description=" + description + ", weight=" + weight
                + ", name=" + name + ", exit=" + Arrays.toString(exit)
                +"]");
          }
       return buffer.toString();
     }

}

此代码是一个简化的Visitor Pattern,其中

'Visitor' -> your predicate

'this' -> 'this.id'

这是有效的,因为您的 toString() 正在调用嵌套 Location 对象的 toString(),这些对象也有用于过滤集的谓词。

如果您无法控制可以传播过滤器的构造,那么您可以采用这种方法:

 public String toString() {
           StringBuilder buffer = new StringBuilder();
           int i = 0;
           for(Location l = this; i < locations.length; l = locations[i++])
              if(filter.apply(l.id) { 
                 buffer.append("Location [location=" + Arrays.toString(location) + ", id=" + id
                    + ", description=" + description + ", weight=" + weight
                    + ", name=" + name + ", exit=" + Arrays.toString(exit)
                    +"]");
              }
           return buffer.toString();
         }