list2.get(i).setValue(mesData."get"+list2.get(i).getName());
时间: 2024-05-23 20:12:08 浏览: 0
这段代码的作用是从 `mesData` 对象中获取属性名为 `list2.get(i).getName()` 的属性值,并将其设置为 `list2.get(i)` 对应的属性值。
具体来说,`list2` 是一个列表,其中存储了若干个对象属性。`i` 是列表中的索引,代表了要获取的对象属性在列表中的位置。`list2.get(i)` 返回的是列表中第 `i` 个对象属性。对象属性包括属性名和属性值。`getName()` 方法返回属性名,`setValue()` 方法设置属性值。
`mesData."get"+list2.get(i).getName()` 这一部分是动态构造属性名的代码。它将 `list2.get(i).getName()` 获取到的属性名加上前缀 `"get"`,然后通过 `mesData` 对象的属性访问器方法获取对应属性的值。最终,该值会被设置为 `list2.get(i)` 对应的属性值。
相关问题
public static void getAllFiles(File file) { File[] files=file.listFiles(pathname->{ // TODO Auto-generated method stub return pathname.getName().toLowerCase().endsWith("xml")||pathname.isDirectory(); } ); for(File f:files) { if(f.isDirectory()) { getAllFiles(f); } else { fList.add(f); } } }怎么利用SwingWorker类加进度条
要利用SwingWorker类加入进度条,你需要在SwingWorker的子类中实现以下几个方法:
1. doInBackground():在这个方法中执行长时间运行的任务。在你的情况下,可以将获取所有文件的代码放在这个方法中。
2. process():这个方法在Event Dispatch Thread(EDT)上被调用,并且可以用于更新进度条。在你的情况下,你可以将进度条的更新逻辑放在这个方法中。你可以通过publish()方法将进度信息发布到process()方法中,然后在process()方法中更新进度条。
3. done():这个方法在后台任务完成后被调用,在EDT上执行。你可以在这个方法中做一些清理工作或通知用户任务已完成。
以下是一个示例代码,展示了如何使用SwingWorker类和JProgressBar来实现一个带有进度条的文件获取任务:
```java
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
public class FileSearchWorker extends SwingWorker<List<File>, Integer> {
private JProgressBar progressBar;
private List<File> fileList;
public FileSearchWorker(JProgressBar progressBar) {
this.progressBar = progressBar;
this.fileList = new ArrayList<>();
}
@Override
protected List<File> doInBackground() {
getAllFiles(new File("your_directory_path")); // Replace with your desired directory path
return fileList;
}
private void getAllFiles(File file) {
File[] files = file.listFiles(pathname -> {
return pathname.getName().toLowerCase().endsWith("xml") || pathname.isDirectory();
});
for(File f: files) {
if(f.isDirectory()) {
getAllFiles(f);
} else {
fileList.add(f);
int progress = (int) ((fileList.size() / (double) files.length) * 100);
publish(progress); // Publish the progress to process() method
}
}
}
@Override
protected void process(List<Integer> chunks) {
int progress = chunks.get(chunks.size() - 1);
progressBar.setValue(progress); // Update the progress bar
}
@Override
protected void done() {
progressBar.setValue(100); // Set the progress bar to 100% when task is done
}
}
```
在上面的代码中,你需要替换`"your_directory_path"`为你想要搜索文件的目录路径。然后,你可以在你的Swing界面中创建一个JProgressBar实例,并将它传递给FileSearchWorker类的构造函数。当调用execute()方法执行SwingWorker时,它将在后台开始执行任务,并通过process()方法来更新进度条。最后,当任务完成时,done()方法会将进度条设置为100%。
请注意,在上面的示例中,进度以文件数量的比例表示,并假设所有文件的数量已知。如果你的任务是根据其他因素进行的,你需要相应地调整计算进度的逻辑。
希望这个示例能帮助你理解如何使用SwingWorker类和进度条来实现你的需求。
java8写一个list<A>中存在与list<B>的复数key匹配的情况下,将listB中的部分字段进行更新成listA中的某个字段值的共通方法
假设listA和listB中的元素都有一个名为key的属性,并且listB中的元素需要更新的属性为value,可以使用Java 8的Stream和Lambda表达式来实现这个方法。
方法签名如下所示:
```
public static <A, B> void updateValues(List<A> listA, List<B> listB, Function<A, ?> getKey, Function<B, ?> getKeyToMatch, Consumer<B> valueUpdater) {
Map<Object, A> mapA = listA.stream().collect(Collectors.toMap(getKey, Function.identity()));
listB.stream()
.filter(b -> mapA.containsKey(getKeyToMatch.apply(b)))
.forEach(b -> valueUpdater.accept(b));
}
```
使用示例:
```
List<Person> listA = Arrays.asList(new Person("John", 30), new Person("Mary", 25));
List<Score> listB = Arrays.asList(new Score("John", 80), new Score("Tom", 90));
updateValues(listA, listB, Person::getName, Score::getName, b -> b.setValue(mapA.get(b.getName()).getAge()));
// listB中的第一个元素(John)的value属性将被更新为30(John在listA中的年龄)
```
代码解释:
1. 首先,将listA转换为一个Map,以便可以通过key属性来快速查找匹配项。
2. 然后,使用Stream对listB中的元素进行过滤,并使用Function来提取key属性。
3. 如果在mapA中存在匹配的key,则使用Consumer来更新listB中的value属性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)