您是否可以使用仿函数/函数式编程在Java 7中对列表进行分组(并计算每组的元素)?

你可以分组List types = new ArrayList(Arrays.asList(TypeEnum.A, TypeEnum.B, TypeEnum.A)); 到Map countPerType; 在 Java 8 之前使用仿函数(例如Google的Guava , Apache的Commons Functor )? 我试图了解函数式编程,但我不确定这种事情是否真的可行(因为我不只是映射集合值,而是试图聚合)? 在命令式的风格,我会做这样的事情: public Map countByGroup(List types) { Map countedTypes = new HashMap(); for(TypeEnum type : types) { if(countedTypes.containsKey(type)) { countedTypes.put(type, countedTypes.get(type) + 1); } else { countedTypes.put(type, 1); } } return countedTypes; } 编辑:依赖副作用似乎有点不合适 – 或者它是如何做到的……? Procedure count […]

Function接口中的exception处理

我是Java 8中的Function接口的新手。这个问题与使用函数接口抽象公共代码的问题有关。 从中得到想法我写的如下: public void act(Input1 input) throws NonRetriableException, InvalidInputException { Function func = item -> { try { activityManager.update(item); return true; } catch (InterruptedException | JSONException e) { throw new NonRetriableException(e); } catch (LockUnavailableException e) { throw new NonRetriableException(e); } }; try { lockManager.executeWithLock(input.getTaskID(), input, func); } catch (LockUnavailableException e) { log.error(“{}”,e); throw new […]

修改本机查询不能有命名参数绑定?

我在JpaRepository中指定了以下修改本机查询。 public interface PlayerStatisticsRepository extends JpaRepository { @Modifying @Query(value = “INSERT INTO playerstatistics(enteredTheFieldAtInSeconds, leftTheFieldAtInSeconds, hometeam_name, matchday_matchdaynumber, playedFromTheFirstWhistleblow, player_id) VALUES (:enteredTheFieldAtInSeconds, :leftTheFieldAtInSeconds, :#{#match.primaryKey.homeTeam.name}, :#{#match.primaryKey.matchday.matchdayNumber}, :playedFromTheFirstWhistleblow, :#{#player.id})”, nativeQuery = true) void insertIntoPlayerStatistics(int enteredTheFieldAtInSeconds, int leftTheFieldAtInSeconds, boolean playedFromTheFirstWhistleblow, Match match, Player player); } 不幸的是,它以运行时的exception结束,说可能是match参数的绑定不存在,而它显然是。 org.springframework.dao.InvalidDataAccessApiUsageException: No parameter binding found for name match!; nested exception is java.lang.IllegalArgumentException: No parameter […]

hibernate.hbm2ddl.import_files:文件的路径

我需要hibernate读取一个sql文件,并在SessionFactory的实例化过程中做任何需要做的事情,所以我做了以下配置: org.hibernate.dialect.MySQLDialect true update /WEB-INF/resources/sql/quartz.sql quartz.sql文件包含为Quartz Scheduler创建表所需的脚本。 但它似乎无法正常工作; 不会创建要创建的sql文件中定义的表,但会创建由带注释的类定义的表。 这是sql文件的内容: # # Quartz seems to work best with the driver mm.mysql-2.0.7-bin.jar # # PLEASE consider using mysql with innodb tables to avoid locking issues # # In your Quartz properties file, you’ll need to set # org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate # DROP TABLE IF EXISTS QRTZ_FIRED_TRIGGERS; […]

如何在Sikuli中编写java程序?

我使用SIKULI录制了GUI桌面应用程序,如下所示: App.open (“C:\\Program Files\\acd\\bin\\VPNClient.exe”) sleep(1) type (“mganda1”) sleep(1) click( ) //click OK 我想将此脚本转换为Java。 所以我想尝试如下: package com.arcot.test.vpn; import org.sikuli.script.*; public class AuthLogin { public static void main(String[] args) { Screen s = new Screen(); App myApp = new App(“application-identifier”) ; myApp.open (“C:\\Program Files\\acd\\bin\\VPNClient.exe”); //如何在这里模拟类型,睡眠和点击function? 我正在搜索java示例以了解对象关系以及如何使用它来模拟记录的脚本。 如果您有任何人知道帮助我的链接,请提供。 最好的问候,Madhu

Android自定义对话框NullPointerException

我不能为我的生活弄清楚为什么我得到一个NullPointerException。 当用户点击特定图像时,应该弹出一个对话窗口并显示所述图像的更大版本: private OnClickListener coverListener = new OnClickListener() { public void onClick(View v) { showDialog(DIALOG_COVER); } }; DIALOG_COVER设置为= 0。 关联的onCreateDialog如下所示: protected Dialog onCreateDialog(int id) { Dialog dialog; switch(id) { case DIALOG_COVER: dialog = new Dialog(mContext); dialog.setContentView(R.layout.cover_dialog); dialog.setTitle(book.getTitle()); ImageView coverLarge = (ImageView)findViewById(R.id.coverLarge); coverLarge.setImageBitmap(book.getCover()); break; default: dialog = null; } return dialog; } 作为参考,这是cover_dialog.xml: 现在,当单击前面描述的图像时,应用程序立即崩溃并通过LogCat抛出以下错误: 06-08 13:29:17.727: […]

通过从MediaStore.ACTION_IMAGE_CAPTURE中选择图像来创建文件

我允许用户从他们的照片库中选择一张图片: Intent pickIntent = new Intent(); pickIntent.setType(“image/*”); pickIntent.setAction(Intent.ACTION_GET_CONTENT); Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String pickTitle = “Select or take a new Picture”; // Or get from strings.xml Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle); chooserIntent.putExtra ( Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takePhotoIntent } ); startActivityForResult(chooserIntent, SELECT_PICTURE); 在onActivityResult我想从所选图像中创建一个新File 。 这是我在做的事情: @Override public void onActivityResult(int requestCode, int resultCode, Intent data) […]

Java – 错误找不到符号

我不明白为什么我在几个地方得到“错误找不到符号”。 有人可以对这个简单的问题有所了解。 PriceCalculator.java:18:错误:找不到符号—私有JTextFeild priceFeild1; PriceCalculator.java:19:错误:找不到符号—私有JTextFeild priceFeild2; PriceCalculator.java:41:错误:找不到符号— setDefaultCloseOpperation(JFrame.EXIT_ON_CLOSE); PriceCalculator.java:44:错误:找不到符号— buildPanel(); import javax.swing.*; public class PriceCalculator extends JFrame { private JPanel panel; // References the panel private JLabel messageLabel1; // References the whole sale label private JLabel messageLabel2; // References the markup label percentage private JTextFeild priceFeild1; // References the whole sale price private JTextFeild priceFeild2; […]

如何使用构造函数@Autowire bean

我正在尝试定义一个bean和@Autowire org.springframework.jdbc.object.StoredProcedure,它需要2个构造函数。 有没有办法在连接这些bean时传递构造函数参数? 以下是我的代码: @Component(“procedure”) public class ExecuteStoreProcedure extends AbstractImutableDAO{ @Autowired private StoredProcedure procedure; …… } 这里StoredProcedure有一个构造函数来传递jdbctemplate和过程名称,这是动态的。

通过套接字发送图像

我通过Socket(客户端 – 服务器)发送图像有一个小问题,我只接收“UTF”文本而不是图像对象,代码有问题吗? 这段代码只发送UTF文本并在服务器端收到,我使用UTF文本识别服务器端的图像(名称),识别后可以将图像发送给客户端 /* * ServerSide * */ package Interface_class; import configuraciones.procesador; import java.awt.image.BufferedImage; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketTimeoutException; import javax.imageio.ImageIO; /** * * @author TheCoder */ public class img_monitor extends Thread{ ServerSocket serverSocket; Socket server; BufferedImage bimg; byte[] bytes; public img_monitor() { try{ //Opening […]