考虑以下示例,其中我们根据姓氏对人员进行排序:
public class ComparatorsExample {
public static class Person {
private String lastName;
public Person(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return "Person: " + lastName;
}
}
public static void main(String[] args) {
Person p1 = new Person("Jackson");
Person p2 = new Person("Stackoverflowed");
Person p3 = new Person(null);
List<Person> persons = Arrays.asList(p3, p2, p1);
persons.sort(Comparator.comparing(Person::getLastName));
}
}
Run Code Online (Sandbox Code Playgroud)
现在,让我们假设getLastName返回一个可选的:
public Optional<String> getLastName() {
return Optional.ofNullable(lastName);
} …Run Code Online (Sandbox Code Playgroud) 当我想安装 gatsby starter 时,我在终端中遇到了这些错误。任何人都知道如何解决它?
[4/4] Building fresh packages...
[6/13] ? sharp
[-/13] ? waiting...
[-/13] ? waiting...
[12/13] ? sharp
error /Users/anykey/Documents/GitHub/a1000/node_modules/favicons/node_modules/sharp: Command failed.
Exit code: 1
Command: (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy)
Arguments:
Directory: /Users/anykey/Documents/GitHub/a1000/node_modules/favicons/node_modules/sharp
Output:
info sharp Detected globally-installed libvips v8.8.3
info sharp Building from source via node-gyp
gyp info it worked if it ends with ok
gyp info using node-gyp@5.0.3
gyp info using node@10.16.3 | darwin | x64
gyp …Run Code Online (Sandbox Code Playgroud) 我正在使用@zeit/next-css将 css 文件导入到我的组件和页面文件中,但它向我抛出此错误
./styles/navbar.cssnavbar.js在我的组件中导入此 css 文件,我收到此错误
ValidationError: Invalid options object. CSS Loader has been initialised using
an options object that does not match the API schema.
- options has an unknown property 'minimize'. These properties are valid:
object { url?, import?, modules?, sourceMap?, importLoaders?, localsConvention?, onlyLocals? }
Run Code Online (Sandbox Code Playgroud)
我next.config.js的放置在package.json哪里
const withCSS = require("@zeit/next-css");
module.exports = withCSS();
Run Code Online (Sandbox Code Playgroud)
我的包 json
{
"name": "transfer-to",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build", …Run Code Online (Sandbox Code Playgroud) 我有12000行的csv文件。每行都有几个用双引号引起来并用逗号分隔的字段。此字段之一是xml文档,因此该行可能很长。文件大小为174 Mb。
这是文件的示例:
"100000","field1","field30","<root><data>Hello I have a
line break</data></root>","field31"
"100001","field1","field30","<root><data>Hello I have multiple
line
break</data></root>","field31"
Run Code Online (Sandbox Code Playgroud)
此文件的问题在xml字段内,该字段可能具有一个或多个换行符,因此可能会中断解析。此处的目标是读取整个文件并应用正则表达式,它将用空字符串替换双引号内的所有换行符。
以下代码给了我OutOfMemoryError:
String path = "path/to/file.csv";
try {
byte[] content = Files.readAllBytes(Paths.get(path));
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
Run Code Online (Sandbox Code Playgroud)
我还尝试使用BufferedReader和StringBuilder读取文件,在第5000行附近出现OutOfMemoryError:
String path = "path/to/file.csv";
try {
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(path));
String line;
int count = 0;
while ((line = br.readLine()) != null) {
sb.append(line);
System.out.println("Read " + count++);
}
}
catch (Exception e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我使用WrapLayoutextends FlowLayout
现在,我有了这个 GUI:
我想要的是这样的:
我尝试了一些类似的事情:label.setVerticalAlignment(JLabel.TOP);但布局似乎不尊重它。我猜这种行为是继承自FlowLayout?
完整代码:
public class WrapLayoutExample {
public static void main(String[] args) {
SwingUtilities.invokeLater(WrapLayoutExample::runGui);
}
private static void runGui() {
JFrame frame = new JFrame("A");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new WrapLayout());
JLabel l = new JLabel("CCC");
l.setBorder(BorderFactory.createLineBorder(Color.red, 10));
l.setVerticalAlignment(JLabel.TOP);
panel.add(l);
l = new JLabel("BBBB");
l.setBorder(BorderFactory.createLineBorder(Color.red, 20));
panel.add(l);
frame.setLayout(new BorderLayout());
frame.add(new JScrollPane(panel));
frame.pack();
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 a和 a之间@OneToOne使用相同的方法来实现关联。A有一个可选的,但 a有一个必需的(因此我需要在“cars”表内有一个外键指向现有的):@IdCarPersonPersonCarCarPersonPerson
@Entity
@Table(name = "persons")
public class Person {
@Id
@Column(name = "name")
private String name;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "person")
private Car car;
/**
* For hibernate
*/
@SuppressWarnings("unused")
private Person() {
}
public Person(String name) {
super();
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() { …Run Code Online (Sandbox Code Playgroud) 我正在使用 java swing 并尝试仅将数字输入到 JTextField 中。
当输入字符时,我想显示无效消息,并防止将字符输入到 JTextField 中。
idText.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
try
{
int i = Integer.parseInt(idText.getText()+e.getKeyChar());
validText.setText("");
}
catch(NumberFormatException e1) {
e.consume();
validText.setText("Numbers Only!");
}
}
});
Run Code Online (Sandbox Code Playgroud)
由于某种原因, e.consume() 没有按我的预期工作,并且我可以输入字符。
我想创建一个返回参数化类型类的方法。
考虑一个interface:
private static interface MyInterface<T> {
void run(T parameter);
}
Run Code Online (Sandbox Code Playgroud)
和一种实现:
private static class MyInterfaceImplString implements MyInterface<String> {
@Override
public void run(String parameter) { }
}
Run Code Online (Sandbox Code Playgroud)
现在,我想传递MyInterfaceImplString.class给一个方法,这个方法将返回String.class.
我正在打印东西,我可以看到那里的信息,但我无法获得它。或者至少有一种更安全的方式。
public class TypesTest {
public static void main(String[] args) {
Class<?> genericParameter1 = getGenericParamaterType(MyInterfaceImplVoid.class);
System.out.println(genericParameter1); //expect Void here
Class<?> genericParameter2 = getGenericParamaterType(MyInterfaceImplString.class);
System.out.println(genericParameter2); //expect String here
}
private static Class<?> getGenericParamaterType(Class<? extends MyInterface<?>> clazz) {
for (Method m : clazz.getMethods()) {
if ("run".equals(m.getName()) && m.getParameterCount() …Run Code Online (Sandbox Code Playgroud) java ×6
swing ×2
collections ×1
comparator ×1
file ×1
flowlayout ×1
gatsby ×1
generics ×1
guava ×1
hibernate ×1
javascript ×1
jpa ×1
jtextfield ×1
keylistener ×1
lambda ×1
next.js ×1
optional ×1
orm ×1
reactjs ×1
reflection ×1
sharp ×1
spring-boot ×1