简体   繁体   English

Java 使用 stream 将 object 列表过滤为字符串数组

[英]Java filtering the object list to string array with stream

I have a class like this我有一个像这样的 class

public class Host {

  private HTMLToken.TokenType type;
  private StringBuilder content;

  public Host(HTMLToken.TokenType type) {
    this.type = type;
    content = new StringBuilder();
  }


  String contentAsString() {
    return content.toString();
  }

  void addStrig(String x) {
    content.append(x);
  }

  public enum TokenType {
    TAG, TEXT
  }

}

and static function like this和 static function 这样

 public static String[] filterLength(List<Host> hostList, int length) {
    return hostList.stream().filter(str -> str.contentAsString().length() > length)
        .toArray(String[]::new);
  }

The problem is inside of filterLength function I can't return stream as a String array(I want to return content in the Host class) because List holds Host object how can I return as a string in 1 line code?问题在于 filterLength function 我不能将 stream 作为字符串数组返回(我想在 Host 类中返回内容),因为 List 包含 Host ZA8CFDE6331BD59EB2AC96F8911C4B6166Z 中的字符串我可以如何返回字符串?

Transform each object in stream变换 stream 中的每个 object

You are successfully streaming, filtering, and collecting a bunch of Host objects into an array.您正在成功地流式传输、过滤和收集一堆Host对象到一个数组中。 But you seem to be asking how to instead collect a string representation of each Host object.但是您似乎在问如何收集每个Host object 的字符串表示形式。

So, you need to add a step to your streaming operations.因此,您需要在流式操作中添加一个步骤。 For each Host object, we want to generate and collect a String object.对于每个Host object,我们要生成并收集一个String object。

Stream#map

To transform from the objects being streamed to a different kind of object, use Stream#map .要从流式传输的对象转换为不同类型的 object,请使用Stream#map

Here is an analog of your code, using LocalDate .这是您的代码的模拟,使用LocalDate We transform each filtered LocalDate object to a String object by passing a method reference : LocalDate:: toString .我们通过传递方法引用将每个过滤的LocalDate object 转换为String object : LocalDate:: toString

List < LocalDate > dates = new ArrayList<>( 4 ) ;
dates.add( LocalDate.of( 2022 , Month.JANUARY, 11 ) ) ;
dates.add( LocalDate.of( 2022 , Month.JANUARY, 12 ) ) ;
dates.add( LocalDate.of( 2022 , Month.JANUARY, 13 ) ) ;
dates.add( LocalDate.of( 2022 , Month.JANUARY, 14 ) ) ;

String[] results = 
    dates
    .stream()
    .filter( localDate -> localDate.getDayOfMonth() > 12 )
    .map( LocalDate :: toString )
    .toArray( String[] :: new ) ;

System.out.println( "results = " + String.join( ", " , results ) ) ;

See this code run live at IdeOne.com .请参阅在 IdeOne.com 上实时运行的代码

results = 2022-01-13, 2022-01-14结果 = 2022-01-13、2022-01-14

I see you have a contentAsString method.我看到你有一个contentAsString方法。 I will guess that method returns the kind of text you want to collect.我猜该方法会返回您想要收集的文本类型。

So your code should be something like the following, passing method reference Host:: contentAsString to Stream#map .因此,您的代码应该类似于以下内容,将方法引用Host:: contentAsString传递给Stream#map

return 
    hostList
    .stream()
    .filter( str -> str.contentAsString().length() > length )
    .map( host -> Host::contentAsString ) 
    .toArray( String[]::new );

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

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