写没有lambda的方法

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

Write methods without lambda

问题

以下是已经翻译好的内容:

在课程中,我们学习了关于Spring,并且我想理解这段代码。

我有以下代码:

public class TopicService {

    List<Topic> topics = new ArrayList<>(Arrays.asList(new Topic("1", "Petya", "Java"),
            new Topic("2", "Vasya", "Javascript"),
            new Topic("3", "Fedya", "Php")));

    public Topic getTopic(String id) {
        return topics.stream().filter(t -> t.getId().equals(id)).findFirst().get();
    }

    public void deleteTopic(String id) {
        topics.removeIf(t -> t.getId().equals(id));
    }
}

我还没有学习过并且对于Lambda表达式还不熟悉。帮我把这些方法按照常规格式重写,就像这样:

public void updateTopic(String id, Topic topic) {
    for (int i = 0; i < topics.size(); i++) {
        Topic t = topics.get(i);
        if (t.getId().equals(id)) {
            topics.set(i, topic);
            return;
        }
    }
}
英文:

In the lesson we went through Spring and I want to understand this code

I have code:

 public class  TopicService {

    List&lt;Topic&gt; topics = new ArrayList&lt;&gt;(Arrays.asList(new Topic(&quot;1&quot;, &quot;Petya&quot;, &quot;Java&quot;),
            new Topic(&quot;2&quot;, &quot;Vasya&quot;, &quot;Javascript&quot;),
            new Topic(&quot;3&quot;, &quot;Fedya&quot;, &quot;Php&quot;)));

  

    public Topic getTopic(String id) {
        return topics.stream().filter(t -&gt; t.getId().equals(id)).findFirst().get();
    }
   }

    public void deleteTopic(String id) {
        topics.removeIf(t -&gt; t.getId().equals(id));
    }
}

I have not studied and am not familiar with lambda yet.
Help me write these methods in a regular format like here:

   public void updateTopic(String id, Topic topic) {
        for (int i = 0; i &lt; topics.size(); i++) {
            Topic t = topics.get(i);
            if (t.getId().equals(id)) {
                topics.set(i, topic);
                return;
            }
        }
    }

答案1

得分: 1

流(Stream)只是为集合准备,以便用于接受函数接口作为参数的各种方法(lambda只是函数接口的一种实现)。阅读有关Java Stream方法的内容,以获得理解。

public Topic getTopic(String id) {
    for (Topic topic : topics)
        if (topic.getId().equals(id)) return topic;

    return null;
}

public void deleteTopic(String id) {
    for (Topic topic : topics)
        if (topic.getId().equals(id)) topics.remove(topic);
}

我本来希望你自己弄清楚,但既然你问了...

英文:

Stream simply prepares your collection for various methods that accept a functional interface as argument (lambda is simply an implementation of a functional interface). Read about Java Stream methods to get the understanding.

public Topic getTopic(String id) {
    for (Topic topic : topics)
        if (topic.getId().equals(id)) return topic;

    return null;
}

public void deleteTopic(String id) {
    for (Topic topic : topics)
        if (topic.getId().equals(id)) topics.remove(topic);
}

I wanted you to figure it out yourself, but since you asked...

答案2

得分: 1

getTopic:

public Topic getTopic(String id) {
    for(Topic tp : topics) {
        if (tp.getId().equals(id)){
            return tp;
        }
    }
    return null;
}

deleteTopic:

public void deleteTopic(String id) {
    for(Topic tp : topics) {
        if (tp.getId().equals(id)){
            topics.remove(tp);
        }
    } 
}
英文:

getTopic:

public Topic getTopic(String id) {
    for(Topic tp : topics) {
       if (tp.getId().equals(id)){
          return tp;
       }
    }
    return null;
 }

deleteTopic:

public void deleteTopic(String id) {
   for(Topic tp : topics) {
      if (tp.getId().equals(id)){
         topics.remove(tp);
      }
   } 
}




</details>



# 答案3
**得分**: 0

欢迎来到 StackOverflow :)

假设您正在将您的主题列表(使用 "topics" 而不是 "topic")作为参数传递,您可以执行以下操作:

```java
topics.stream()
  .filter(n -> n.getId().equals("Android"))
  .forEach(n -> topics.set(n.getId(), n));

.stream() 用于操作元素序列。

.filter(condition) 会返回这个 "stream" 中与 "Android" 相等的 id 的元素,然后

.forEach 将对这些元素进行更新。

您可能需要对您的代码进行一些修改以使其正确,但这是基本思路。如果有帮助,请随意接受为答案 写没有lambda的方法

英文:

Welcome to StackOverFlow 写没有lambda的方法

Suppose you are passing your list of topics ("topics" instead of "topic")as a parameter you could do :

topics.stream()
  .filter(n -&gt; n.getId().equals(id)equals(&quot;Android&quot;))
  .forEach(n -&gt; topics.set(n.getId(), n););

.stream()

is uded to manipulate sequences of elements

.filter(condition) 

will give you the elements of this "stream" on which ids are equal to "Android" and then

.forEach

will make your update for these elements.

You'll maybe have to make some modifications to your code to be correct but here is the idea.
Don't hesitate to accept as an answer if it help 写没有lambda的方法

答案4

得分: 0

试试这个 getTopic 方法:

public Topic getTopic(String id) {
    for(Topic tp : topics) {
        if (tp.getId().equals(id)){
            return tp;
        }
    }
    return null;
}

以及这个 deleteTopic 方法:

public void deleteTopic(String id) {
    for(Topic tp : topics) {
        if (tp.getId().equals(id)){
            topics.remove(tp);
            break;
        }
    } 
}
英文:

Try this for getTopic:

     public Topic getTopic(String id) {
        			 for(Topic tp : topics) {
        				 if (tp.getId().equals(id)){
        					 return tp;
        				 }
        			 }
                    return null;
        		    }

and for deleteTopic :

public void deleteTopic(String id) {
		 for(Topic tp : topics) {
			 if (tp.getId().equals(id)){
				 topics.remove(tp);
				 break;
			 }
		 } 
	    }

huangapple
  • 本文由 发表于 2020年7月26日 21:35:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63100860.html
匿名

发表评论

匿名网友

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

确定