在Java中,比较迭代器的值和枚举的最佳方法以匹配值是什么。

huangapple go评论70阅读模式
英文:

Best way to compare an iterator values and an enum for matching values in java

问题

我有一个迭代器,通过一个简单的hasNext()函数循环遍历一系列值。我还有一个包含特定值的枚举。我想要找到枚举和迭代器之间所有匹配的值。

我希望在运行时和空间上都能获得最佳性能来实现这一点。
谢谢。

代码参考(这是一个简单明了的案例,但请随时索取更多代码):

Iterator iterator = user.userTasks.iterator(); // 循环遍历当前用户的任务
while(iterator.hasNext()) {
    UserTask userTask = (UserTask)iterator.next();
    // 希望将上面的userTask与Java枚举的值进行比较,以查找任何匹配项


该枚举具有一个字符串字段,这就是我试图将其与此迭代器的字段进行匹配的内容。因此,是迭代器的一个字段与枚举的一个字段进行匹配。

英文:

I have an iterator that loops through a values with a simple hasNext() function. I also have an enum that contains certain values. I would like to find all the matching values between the enum and the iterator.

I'm trying to do this hopefully with the best performance both runtime and space wise.
Thanks.

For reference to code (it's a simple straightforward case but feel free to ask for more code:

Iterator iterator = user.userTasks.iterator(); //loops through current users tasks
        while(iterator.hasNext()) {
            UserTask userTask = (userTask)iterator.next();
            //want to compare userTask above with values from a java enum for any matches



The enum has a string field and that is what I'm trying to match to this iterators field too. So, a field of the iterator to a field of the enum.

答案1

得分: 1

根据您的描述,您需要将枚举字段与迭代器字段进行匹配。

首先,让我们创建一个带有字符串字段的枚举。

public enum ExampleEnum {
    
    ONE("one"),
    TWO("two"),
    THREE("three"),
    FOUR("four");
    
    private String eName;
	 
    ExampleEnum(String eName) {
        this.eName = eName;
    }
    
    public String geteName(){
        return eName;
    }
    
    public static Stream<ExampleEnum> stream() {
        return Stream.of(ExampleEnum.values()); 
    }
}

在您的情况下,使用一个简单的ArrayList作为迭代器,您可以将其替换为UserTask迭代器。

String[] arr = {"one", "five", "six", "three", "eleven", "two", "four"};
List<String> arrayList = Arrays.asList(arr);
Iterator<String> iterator = arrayList.iterator();

while(iterator.hasNext()){
    String compare = iterator.next();
    ExampleEnum.stream()
        .filter(d -> d.geteName().equals(compare))
        .forEach(System.out::println);
}

我正在使用Java 8,我猜在Java 9中可以更轻松地完成。

英文:

Based on your description, you need to match the enum field with and Iterator field.

First lets create an ENUM with a String field.
<!-- language: java -->
public enum ExampleEnum {

    ONE(&quot;one&quot;),
    TWO(&quot;two&quot;),
    THREE(&quot;three&quot;),
    FOUR(&quot;four&quot;);
    
    private String eName;
	 
    ExampleEnum(String eName) {
        this.eName = eName;
    }
    
    public String geteName(){
        return eName;
    }
    
    public static Stream&lt;ExampleEnum&gt; stream() {
        return Stream.of(ExampleEnum.values()); 
    }
}

Use a simple ArrayList as an Iterator in your case replace it with UserTask Iterator.

<!-- language: java -->

String[] arr = {&quot;one&quot;, &quot;five&quot;, &quot;six&quot;, &quot;three&quot;, &quot;eleven&quot;, &quot;two&quot;, &quot;four&quot;};
List&lt;String&gt; arrayList = Arrays.asList(arr);
Iterator&lt;String&gt; iterator = arrayList.iterator();

while(iterator.hasNext()){
    String compare = iterator.next();
	ExampleEnum.stream()
    .filter(d -&gt; d.geteName().equals(compare))
    .forEach(System.out::println);
}

I'm using Java 8, I guess in Java 9 can be done more easily.

huangapple
  • 本文由 发表于 2020年9月18日 01:16:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63943112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定