我有一个像这样的字符串:
pet:cat::car:honda::location:Japan::food:sushi
Run Code Online (Sandbox Code Playgroud)
现在:指示键值对,同时::分离对.我想将键值对添加到地图中.
我可以使用以下方法实现:
Map<String, String> map = new HashMap<String, String>();
String test = "pet:cat::car:honda::location:Japan::food:sushi";
String[] test1 = test.split("::");
for (String s : test1) {
String[] t = s.split(":");
map.put(t[0], t[1]);
}
for (String s : map.keySet()) {
System.out.println(s + " is " + map.get(s));
}
Run Code Online (Sandbox Code Playgroud)
但是有一种有效的方法吗?
我觉得代码是低效的,因为我使用了2个String[]对象并且split两次调用了函数.此外,我正在使用t[0],如果没有值t[1],可能会抛出一个ArrayIndexOutOfBoundsException.
在我的春季mvc应用程序中,我有以下对象.我试图devtool在我的应用程序中使用数据视觉.
@Entity
@Data
public class ConsultationRequest {
@Id
@GeneratedValue
private Long id;
private String name;
private String email;
private String purpose;
private String programme;
private int year;
private String language;
private String comments;
@Enumerated(EnumType.STRING)
private ConsultationStatus status;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用jpa来制作实体:
@Repository
public interface ConsultationRequestRepository extends JpaRepository<ConsultationRequest, Long> {
}
Run Code Online (Sandbox Code Playgroud)
问题是当我加载我的应用程序时,我遇到了2个错误:
Unsuccessful: drop sequence hibernate_sequence
[36morg.hibernate.tool.hbm2ddl.SchemaExport Sequence "HIBERNATE_SEQUENCE" not found; SQL statement:
Run Code Online (Sandbox Code Playgroud)
然后,当我打开
http://localhost:8080/h2-console/
Run Code Online (Sandbox Code Playgroud)
我看不到桌子.似乎在启动过程中,表没有制作.
我使用Spring的@ExceptionHandler注释来捕获控制器中的异常.
有些请求将POST数据保存为写入请求体的纯XML字符串,我想读取该数据以便记录异常.问题是,当我在异常处理程序中请求输入流并尝试从中读取时,流返回-1(空).
异常处理程序签名是:
@ExceptionHandler(Throwable.class)
public ModelAndView exception(HttpServletRequest request, HttpServletResponse response, HttpSession session, Throwable arff)
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?有没有办法访问请求正文?
我的控制器:
@Controller
@RequestMapping("/user/**")
public class UserController {
static final Logger LOG = LoggerFactory.getLogger(UserController.class);
@Autowired
IUserService userService;
@RequestMapping("/user")
public ModelAndView getCurrent() {
return new ModelAndView("user","response", userService.getCurrent());
}
@RequestMapping("/user/firstLogin")
public ModelAndView firstLogin(HttpSession session) {
userService.logUser(session.getId());
userService.setOriginalAuthority();
return new ModelAndView("user","response", userService.getCurrent());
}
@RequestMapping("/user/login/failure")
public ModelAndView loginFailed() {
LOG.debug("loginFailed()");
Status status = new Status(-1,"Bad login");
return new ModelAndView("/user/login/failure", "response",status);
}
@RequestMapping("/user/login/unauthorized")
public ModelAndView unauthorized() {
LOG.debug("unauthorized()");
Status …Run Code Online (Sandbox Code Playgroud) 我想在不丢失密钥映射的情况下压扁Map将Integer密钥与列表相关联的密钥String.我很好奇,好像用stream和做这样做是有用的和有用的lambda.
我们从这样的事情开始:
Map<Integer, List<String>> mapFrom = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
我们假设mapFrom填充在某处,看起来像:
1: a,b,c
2: d,e,f
etc.
Run Code Online (Sandbox Code Playgroud)
我们还假设列表中的值是唯一的.
现在,我想"展开"它以获得第二张地图:
a: 1
b: 1
c: 1
d: 2
e: 2
f: 2
etc.
Run Code Online (Sandbox Code Playgroud)
我可以这样做(或非常相似,使用foreach):
Map<String, Integer> mapTo = new HashMap<>();
for (Map.Entry<Integer, List<String>> entry: mapFrom.entrySet()) {
for (String s: entry.getValue()) {
mapTo.put(s, entry.getKey());
}
}
Run Code Online (Sandbox Code Playgroud)
现在让我们假设我想使用lambda而不是嵌套for循环.我可能会这样做:
Map<String, Integer> mapTo = mapFrom.entrySet().stream().map(e -> {
e.getValue().stream().?
// Here I can iterate on …Run Code Online (Sandbox Code Playgroud) 如何在没有if语句的情况下将数字舍入到最近的十位?例如,98到100.
int num = 87;
double t;
double d = 1.0 * num; // d = 87.0
t = d/100;
System.out.println(t);
Run Code Online (Sandbox Code Playgroud) 我ArrayIndexOutOfBoundsException随机出现了.它似乎发生在我notifyDataSetChanged();身上因为错误是如此随机,所以很难准确指出它发生的位置.
有没有人有类似的自定义问题Adaptor?
FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=12; index=12
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:8041)
at android.widget.ListView.layoutChildren(ListView.java:1604)
at android.widget.AbsListView.onLayout(AbsListView.java:2444)
at android.view.View.layout(View.java:15221)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15221)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:15221)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:15221)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:15221)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15221)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:15221)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2260)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2007)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591) …Run Code Online (Sandbox Code Playgroud) 在Java 8中,如果我有一个像这样的对象列表:
MyObject
double a;
double b;
double c;
Run Code Online (Sandbox Code Playgroud)
我想在对象列表中获取每个字段的总数.我能做到的一种方法如下:
double totalA = myListOfObjects.stream().map(e -> e.getA()).reduce(0.0, (x, y) -> x + y);
double totalB = myListOfObjects.stream().map(e -> e.getB()).reduce(0.0, (x, y) -> x + y);
double totalC = myListOfObjects.stream().map(e -> e.getC()).reduce(0.0, (x, y) -> x + y);
Run Code Online (Sandbox Code Playgroud)
但是有没有把它组合成一个通过使用流api的对象列表?如果我只是写了一个for/while循环(见下文)并手动累加了3个总数,那么看起来比上面3行代码更有效率)
for (MyObject obj: myListOfObjects) {
totalA += obj.getA();
totalB += obj.getB();
totalC += obj.getC();
}
Run Code Online (Sandbox Code Playgroud)
谢谢
输入格式
读取一些未知的n行输入,stdin(System.in)直到你达到EOF; 每行输入都包含一个非空字符串.
输出格式
对于每一行,打印行号,后跟一个空格,然后输入作为输入的行内容:
样本输出
Hello world
I am a file
Read me until end-of-file.
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案.问题是我无法继续进行EOF.但输出只是:
Hello world
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public class Solution {
public static void main(String[] args) {
check(1); // call check method
}
static void check(int count) {
Scanner s = new Scanner(System.in);
if(s.hasNext() == true) {
String ns = s.nextLine();
System.out.println(count + " " + ns);
count++;
check(count);
}
}
}
Run Code Online (Sandbox Code Playgroud) 您好,我是 Thymeleaf 的新手,遇到了一个可能微不足道的问题,但 thymeleaf 的行为并不像它应该的那样。只是一点帮助将不胜感激
我不使用spring boot来学习。此外,我对 Spring 也很陌生。可能会错过一两件事。
我有这样的简单 index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Index 2</title>
</head>
<body>
<div th:replace="~{fragments/fragment1 :: fr1}"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
和 fragment1.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
</head>
<body>
<div th:fragment="fr1"><h1>HERE IS FRAGMENTS 1</h1></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
据说它确实解析了模板,但结果根本没有改变。
这是我从浏览器页面源中得到的
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Index 2</title>
</head>
<body>
<div th:replace="~{fragments/fragment1 :: fr1}"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
是的,它与原始 index.html 完全相同。
所以我想这可能与配置有关,但对我来说它看起来很好。在我的另一个学习项目中,它在完全相同的配置下运行良好。
这是配置
/* package and imports */
@Configuration
@EnableWebMvc
@ComponentScan("com.eshop")
public class …Run Code Online (Sandbox Code Playgroud) 我有一个包含 10 个字段的实体:
Class Details{
String item;
String name;
String type;
String origin;
String color;
String quality;
String country;
String quantity;
Boolean availability;
String price;
}
Run Code Online (Sandbox Code Playgroud)
我有一个服务列表的宁静端点。我希望用户能够为每个字段提供搜索过滤器。目前我有每个字段的 QueryParam。然后我使用 java8 流过滤:
List<Detail> details;
details.stream().filter(detail-> detail.getItem()==item).filter(detail-> detail.getName()==name).....collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果我有 50 个其他类要过滤多个字段,有没有办法概括这一点?