算法有哪些if(typeForum.equals(forums.getType())){ //添加版块 forumsList = getForumsByType(typeGroup);//获取分区 txt = 编辑版块; td = 上级分区; }else if(typeSub.equals(forums.getType())){
时间: 2023-06-16 21:04:53 浏览: 50
这段代码看起来像是 Java 语言中的一段条件语句,其中包含了两个条件分支。具体来说,如果变量 `typeForum` 的值等于变量 `forums` 的类型,那么就会执行第一个条件分支中的代码块,否则就会执行第二个条件分支中的代码块。
在第一个条件分支中,代码会根据变量 `typeGroup` 获取分区,然后将编辑版块的文本赋值给变量 `txt`,将上级分区的文本赋值给变量 `td`。
在第二个条件分支中,代码没有获取分区,而是假定 `forums` 已经是一个子版块,将编辑版块的文本赋值给变量 `txt`,将上级分区的文本赋值给变量 `td`。
相关问题
import java.util.*; /* 这里是定义 People 类的代码,无需关心。 */ public class Main { public static void main(String args[]) { LinkedList<People> peoples = new LinkedList<People>();//创建链表 Scanner sc=new Scanner(System.in); peoples.add(new People("rose",18));//链表初始化 People p=new People(sc.next(),sc.nextInt());//新对象 ;//新对象加到链表头 p=new People(sc.next(),sc.nextInt());//新对象 ;//新对象加到链表尾 String name=sc.next();//输入名字 for (int i=peoples.size()-1;i>=0;i--) {//反向遍历链表 p=peoples.get(i);//取出第 i 个元素 if(p.getName().equals(name))//判断元素名字是否等于输入的名字 ;//若名字相等,删除该元素 } System.out.println(peoples);//输出链表 sc.close(); } }
这段代码存在一些问题,无法正常运行。首先,在新对象加到链表头和链表尾的代码中,分号后应该使用 addFirst() 和 addLast() 方法将新对象加入链表。其次,在删除元素的代码中,分号后应该使用 remove() 方法删除元素。最后,在输出链表的代码中,应该使用 System.out.println(peoples.toString()) 方法输出链表元素。正确的代码如下所示:
```
import java.util.*;
/* 这里是定义 People 类的代码,无需关心。 */
public class Main {
public static void main(String args[]) {
LinkedList<People> peoples = new LinkedList<People>();//创建链表
Scanner sc=new Scanner(System.in);
peoples.add(new People("rose",18));//链表初始化
People p=new People(sc.next(),sc.nextInt());//新对象
peoples.addFirst(p);//新对象加到链表头
p=new People(sc.next(),sc.nextInt());//新对象
peoples.addLast(p);//新对象加到链表尾
String name=sc.next();//输入名字
for (int i=peoples.size()-1;i>=0;i--) {//反向遍历链表
p=peoples.get(i);//取出第 i 个元素
if(p.getName().equals(name)) {//判断元素名字是否等于输入的名字
peoples.remove(p);//若名字相等,删除该元素
}
}
System.out.println(peoples.toString());//输出链表
sc.close();
}
}
```
SingleOutputStreamOperator<List<JSONObject>> applyDataStream = dataStreamSource.keyBy(value -> { String[] splitValues = value.getString("key").toString().split(":"); return splitValues[2]; }).window(TumblingProcessingTimeWindows.of(Time.seconds(2))).apply(new WindowFunction<JSONObject, List<JSONObject>, String, TimeWindow>() { @Override public void apply(String key, TimeWindow timeWindow, Iterable<JSONObject> input, Collector<List<JSONObject>> out) throws Exception { List<JSONObject> output = new ArrayList<>(); input.forEach(s -> { // String topicName = ModelUtil.getConfigValue("ods_kafka_all_data_topicName") + "_" + key; //主题名称 String value = s.getString("value"); TranslateJson translateJson = JSON.parseObject(value, TranslateJson.class); if ("read".equals(translateJson.getType()) || "c".equals(translateJson.getType())) {//新增的数据才同步 使用这种写法优化上面代码
您可以使用 Lambda 表达式来优化上面的代码,示例代码如下:
```
SingleOutputStreamOperator<List<JSONObject>> applyDataStream = dataStreamSource
.keyBy(value -> value.getString("key").toString().split(":")[2])
.window(TumblingProcessingTimeWindows.of(Time.seconds(2)))
.apply((String key, TimeWindow timeWindow, Iterable<JSONObject> input, Collector<List<JSONObject>> out) -> {
List<JSONObject> output = new ArrayList<>();
input.forEach(s -> {
String value = s.getString("value");
TranslateJson translateJson = JSON.parseObject(value, TranslateJson.class);
if ("read".equals(translateJson.getType()) || "c".equals(translateJson.getType())) {
// 新增的数据才同步
// String topicName = ModelUtil.getConfigValue("ods_kafka_all_data_topicName") + "_" + key; //主题名称
output.add(s);
}
});
out.collect(output);
});
```
使用 Lambda 表达式可以使代码更加简洁易读,同时也可以提高代码的执行效率。