排序操作的文档说:
对于有序流,排序是稳定的。对于无序流,没有稳定性保证。
和页面总结说:
一些中间操作,例如 sorted(),可能会强加遇到顺序
有人可以解释为什么sorted操作需要对 Stream 的遇到顺序(我没有看到遇到顺序的存在与排序操作之间的关系)?
这是否意味着以下代码无效(因为 HashSet 不是本质上有序的)?
Set<Integer> mySet = new HashSet<>();
mySet.add(10);
mySet.add(4);
mySet.add(20);
mySet.add(15);
mySet.add(22);
mySet.add(-3);
List<Integer> result = mySet.stream().sorted().collect(Collectors.toList());
System.out.println(result);
Run Code Online (Sandbox Code Playgroud)
当我运行这段代码时,它总是给我相同的输出 [-3, 4, 10, 15, 20, 22]
事件如果我使用.parrallel(),输出保持不变[-3, 4, 10, 15, 20, 22]
mySet.stream().parallel().sorted().collect(Collectors.toList());`
Run Code Online (Sandbox Code Playgroud) 给这个控制器
@GetMapping("/test")
@ResponseBody
public String test() {
if (!false) {
throw new IllegalArgumentException();
}
return "blank";
}
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
@ResponseBody
public String handleException(Exception e) {
return "Exception handler";
}
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
@ResponseBody
public String handleIllegalException(IllegalArgumentException e) {
return "IllegalArgumentException handler";
}
Run Code Online (Sandbox Code Playgroud)
这两个ExceptionHandler都匹配,IllegalArgumentException因为它是Exceptionclass 的子级。
当我到达/test端点时,将handleIllegalException调用该方法。如果抛出NullPointerException,handleException则调用该方法。
spring如何知道应该执行handleIllegalException方法而不是handleException方法?当多个ExceptionHandler匹配一个Exception 时,它如何管理优先级?
(我认为顺序或ExceptionHandler声明很重要,但是即使我handleIllegalException之前声明了handleException,结果也是一样的)
我面临着在 Eclipse 中运行我的(Spring)集成测试的问题。
我有一个这样的测试课:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class ImportCSVBatchIT {
@Test
public void myIT() {
assertEquals(...);
}
}
Run Code Online (Sandbox Code Playgroud)
当我执行 a 时mvn clean install,一切正常,我的测试已运行(我添加了 maven-failsafe-plugin),如果我添加类似的指令,并且assertTrue(false)我重新播放clean install我的构建失败 -> 没关系,我添加了错误的断言。
如果我在 Eclipse 中执行相同的操作,右键单击 -> Run as -> JUnit(在添加错误的断言后不执行操作clean install),则测试不会失败,因为它运行上次 全新安装的版本(当错误的断言时)不在这里)。因此,如果我添加新指令并尝试在 Eclipse 中运行它,我将不会获得测试类的最新版本,直到我运行mvn clean install.
我希望单元测试具有相同的行为:
我尝试将Kotlin与Maven结合使用,所以我遵循了文档
我在pom.xml文件中有此配置
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<kotlin.version>1.2.10</kotlin.version>
</properties>
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
源文件夹是src/main/kotlin和src/test/kotlin
当我mvn clean install收到这个错误时
[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.2.10:compile (compile) on project kotlin-starter: Execution compile of goal org.jetbrains.kotlin:kotlin-maven-plugin:1.2.10:compile failed: A required class was missing while executing …Run Code Online (Sandbox Code Playgroud) React Hooks文档说不要在循环,条件或嵌套函数中调用Hooks.
我知道执行的顺序很重要,所以React可以知道哪个状态对应于哪个useState调用.鉴于此,显然无法在条件内调用钩子.
但是如果我们useState在一个迭代次数不随时间变化的循环中调用,我看不出是什么问题.这是一个例子:
const App = () => {
const inputs = [];
for(let i = 0; i < 10; i++) {
inputs[i] = useState('name' + i);
}
return inputs.map(([value, setValue], index) => (
<div key={index}>
<input value={value} onChange={e => setValue(e.target.value)} />
</div>
));
}
export default App;
Run Code Online (Sandbox Code Playgroud)
上面的代码有问题吗?useState如果在每个渲染上调用此函数,那么在嵌套函数内部调用的问题是什么?
我试图了解 React Portals,所以我查看了文档。
我仍然不明白为什么在他们的例子中(https://codepen.io/gaearon/pen/yzMaBd和https://reactjs.org/docs/portals.html#event-bubbling-through-portals)他们正在创建在构造函数中添加一个额外的 div 并将其传递给createPortal函数的第二个参数。
他们为什么不直接使用这样的modalRoot容器?
const modalRoot = document.getElementById('modal-root');
class Modal extends React.Component {
render() {
return ReactDOM.createPortal(
this.props.children,
modalRoot,
);
}
}
Run Code Online (Sandbox Code Playgroud)
在 codepen 示例中,此注释给出了解释
创建一个 div,我们会将模态渲染到其中。因为每个 Modal 组件都有自己的元素,所以我们可以将多个 modal 组件渲染到 modal 容器中。
在这个stackblitz示例中,我尝试在不使用额外 div 的情况下将多个组件渲染到门户容器中,它似乎工作得很好,React.createPortal已经将子组件附加到容器中,并且当组件卸载时,它只会从容器中移除子组件,并且不会清除容器的所有内容,因此我们不必手动执行此操作。我错过了什么吗?
我有一个与Event实体具有oneToMany关系的Project实体
public class Project {
....
@OneToMany(mappedBy = "dossier", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Event> events;
}
Run Code Online (Sandbox Code Playgroud)
我有一个ProjectService类
@Service
@Transactional
public class ProjectService {
public List<Project> findAll() {
return (List<Project>) projectRepository.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
还有一个ProjectController
@RestController
@RequestMapping(value = "/projects")
public class ProjectController {
@RequestMapping(method= RequestMethod.GET)
public List<Project> getAllProject() {
return projectService.findAll();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的客户端代码中,我看到项目的事件已加载,我不明白为什么.
我预计在DossierService中方法findAll的事务结束时,实体将被分离.显然,我的实体仍然附着,因为在我的控制器中的jackson序列化期间检索事件.
当我在一个变量中定义了一个组件,并且我试图将它作为子道具传递给一个组件时,Objects are not valid as a React child会显示错误。
这样做的正确方法是什么?
function AnotherComponent(){
return "Another";
}
function ChildComponent(props) {
const { children, value, index, ...other } = props;
console.log(children);
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
id={`full-width-tabpanel-${index}`}
aria-labelledby={`full-width-tab-${index}`}
{...other}
>
<Box p={3}>{children}</Box>
</Typography>
);
}
function MainComponent(){
const tabItems = [
{ "component": AnotherComponent}
];
const [value, setValue] = React.useState(0);
return (
<>
{tabItems.map((tabItem,index) => (
<ChildComponent value={value} index={tabItem.index}>
{tabItem.component}
</ChildComponent>
))}
</>
)
}
Run Code Online (Sandbox Code Playgroud) java ×5
reactjs ×3
spring ×3
maven ×2
react-hooks ×2
eclipse ×1
hibernate ×1
java-stream ×1
javascript ×1
junit ×1
kotlin ×1
material-ui ×1
spring-mvc ×1