我正在寻找一种简洁的方法来将Iterator一个Stream或更具体的转换为"视图"迭代器作为流.
出于性能原因,我想在新列表中避免使用迭代器的副本:
Iterator<String> sourceIterator = Arrays.asList("A", "B", "C").iterator();
Collection<String> copyList = new ArrayList<String>();
sourceIterator.forEachRemaining(copyList::add);
Stream<String> targetStream = copyList.stream();
Run Code Online (Sandbox Code Playgroud)
根据评论中的一些建议,我也尝试使用Stream.generate:
public static void main(String[] args) throws Exception {
Iterator<String> sourceIterator = Arrays.asList("A", "B", "C").iterator();
Stream<String> targetStream = Stream.generate(sourceIterator::next);
targetStream.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到了NoSuchElementException(因为没有调用hasNext)
Exception in thread "main" java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java:364)
at Main$$Lambda$1/1175962212.get(Unknown Source)
at java.util.stream.StreamSpliterators$InfiniteSupplyingSpliterator$OfRef.tryAdvance(StreamSpliterators.java:1351)
at java.util.Spliterator.forEachRemaining(Spliterator.java:326)
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
at Main.main(Main.java:20)
Run Code Online (Sandbox Code Playgroud)
我已经看过StreamSupport和Collections,但我没有发现任何东西.
如何命名您创建的不同类/接口?有时我没有实现信息添加到实现名称 - 如接口FileHandler和类SqlFileHandler.
当发生这种情况时,我通常将该界面命名为"普通"名称,Truck并命名实际的类TruckClass.
在这方面,您如何命名接口和类?
在MDN文章面向对象的Javascript简介中关于继承的部分中,我注意到他们设置了prototype.constructor:
// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;
Run Code Online (Sandbox Code Playgroud)
这有什么重要意义吗?省略它可以吗?
是否存在在Java中命名枚举的约定?
我的偏好是枚举是一种类型.所以,例如,你有一个枚举
Fruit{Apple,Orange,Banana,Pear, ... }
NetworkConnectionType{LAN,Data_3g,Data_4g, ... }
Run Code Online (Sandbox Code Playgroud)
我反对命名它:
FruitEnum
NetworkConnectionTypeEnum
Run Code Online (Sandbox Code Playgroud)
我知道很容易找出哪些文件是枚举,但是你也可以:
NetworkConnectionClass
FruitClass
Run Code Online (Sandbox Code Playgroud)
另外,是否有一个好的文档描述了常量,在哪里声明它们等等?
是否有任何markdown fork允许您引用其他文件,例如包含文件?具体来说,我想创建一个单独的markdown文件,其链接我经常调用但不总是(调用此B.md),然后当我在md文件中通过引用链接我正在写(A.md)时,我想喜欢它从其他文件(B.md)而不是从当前文件(A.md)的末尾拉取链接.
这是代码:
package mscontroller;
import javax.swing.*;
import com.apple.eawt.Application;
public class Main {
public static void main(String[] args)
{
Application app = new Application();
app.setEnabledAboutMenu(true);
AMEListener listener = new AMEListener();
app.addApplicationListener(listener);
JFrame mainFrame = new JFrame("Application Menu Example");
mainFrame.setSize(500, 500);
mainFrame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
Exception in thread "main" java.lang.Error: Unresolved compilation
problems: Access restriction: The type 'Application' is not API
(restriction on required library
'/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar')
Access restriction: The constructor 'Application()' is not API
(restriction on required library
'/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home/jre/lib/rt.jar')
Access restriction: The type …Run Code Online (Sandbox Code Playgroud) 我的要求很简单:2列,右列有固定大小.不幸的是,我无法在stackoverflow和Google上找到有效的解决方案.如果我在自己的上下文中实现,那么每个解决方案都会失败 目前的解决方案是:
div.container {
position: fixed;
float: left;
top: 100px;
width: 100%;
clear: both;
}
#content {
margin-right: 265px;
}
#right {
float: right;
width: 225px;
margin-left: -225px;
}
#right, #content {
height: 1%; /* fixed for IE, although doesn't seem to work */
padding: 20px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
<div id="content">
fooburg content
</div>
<div id="right">
test right
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我用上面的代码得到以下内容:
|----------------------- -------|
| fooburg content | |
|-------------------------------|
| | test right |
|----------------------- -------|
Run Code Online (Sandbox Code Playgroud)
请指教.非常感谢!
我正在尝试使用maven为一个名为"logmanager"的小型家庭项目生成一个可执行jar,就像这样:
我将那里显示的代码段添加到pom.xml中,然后运行mvn assembly:assembly.它在logmanager/target中生成两个jar文件:logmanager-0.1.0.jar和logmanager-0.1.0-jar-with-dependencies.jar.当我双击第一个jar时出现错误:
Could not find the main class: com.gorkwobble.logmanager.LogManager. Program will exit.
Run Code Online (Sandbox Code Playgroud)
当我双击jar-with-dependencies.jar时出现稍微不同的错误:
Failed to load Main-Class manifest attribute from: C:\EclipseProjects\logmanager\target\logmanager-0.1.0-jar-with-dependencies.jar
Run Code Online (Sandbox Code Playgroud)
我复制并粘贴了路径和类名,并检查了POM中的拼写.我的主要类从eclipse启动配置启动.有人可以帮我弄清楚为什么我的jar文件不会运行?另外,为什么有两个罐开始?如果您需要更多信息,请与我们联系.
这是完整的pom.xml,供参考:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gorkwobble</groupId>
<artifactId>logmanager</artifactId>
<name>LogManager</name>
<version>0.1.0</version>
<description>Systematically renames specified log files on a scheduled basis. Designed to help manage MUSHClient logging and prevent long, continuous log files.</description>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<!-- nothing here -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.gorkwobble.logmanager.LogManager</mainClass> …Run Code Online (Sandbox Code Playgroud) 我有非常简单的persistance.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<class>pl.michalmech.eventractor.domain.User</class>
<class>pl.michalmech.eventractor.domain.Address</class>
<class>pl.michalmech.eventractor.domain.City</class>
<class>pl.michalmech.eventractor.domain.Country</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
它的工作原理.
但是当我删除<class>元素时,应用程序看不到实体(所有类都带有注释@Entity).
是否有任何自动机制来扫描@Entity类?
在maven多模块项目中,我希望每个模块始终保持与父模块相同的版本,我通常在模块的pom.xml中执行以下操作:
<parent>
<groupId>com.groupId</groupId>
<artifactId>parentArtifactId</artifactId>
<version>1.1-SNAPSHOT</version>
</parent>
<groupId>com.groupId</groupId>
<artifactId>artifactId</artifactId>
<packaging>jar</packaging>
<version>${project.parent.version}</version>
<name>name</name>
Run Code Online (Sandbox Code Playgroud)
自从我开始使用maven 3.0-alpha-5以来,我收到了以下警告.
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.groupid.artifactId:name:jar:1.1-SNAPSHOT
[WARNING] 'version' contains an expression but should be a constant. @ com.groupid.artifactId:name::${project.parent.version}, /Users/whaley/path/to/project/child/pom.xml
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
Run Code Online (Sandbox Code Playgroud)
我很想知道将模块的版本绑定到父版本的真正问题是,如果有的话?或者这是一个一般警告的情况,当任何表达式,无论是否是project.parent.version,用于版本元素.