小编use*_*339的帖子

是否有一种干净(和安全的)方法来乘以Java中的地图值?

我有一个Map<String, Double>,并希望将地图中的所有值乘以2,比方说,但是将空值保持为空.

我显然可以使用for循环来做到这一点,但是想知道是否有更清洁的方法呢?

Map<String, Double> someMap = someMapFunction();
Map<String, Double> adjustedMap = new Hashmap<>();
if (someMap != null) {
    for (Map.Entry<String,Double> pair : someMap.entryset()) {
        if (pair.getValue() == null) {
            adjustedMap.put(pair.getKey(), pair.getValue());
        } else {
            adjustedMap.put(pair.getKey(), pair.getValue()*2)
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

有时候返回的地图someMapFunction是一个不可变的地图,所以这不能用就地做Map.replaceAll.我无法想出一个更清洁的流解决方案.

java java-8 java-stream

48
推荐指数
4
解决办法
4608
查看次数

dependencies.dependency.version'缺少错误

我正在尝试使用apache maven创建一个包.当我运行mvn clean install命令时,它给出了以下错误:

javax.servlet缺少dependencies.dependency.version':servlet-api.jar

我把'servlet-api.jar'放在我项目的资源文件夹中

任何人都可以告诉我应该在哪里放置该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">

 <parent>
   <artifactId>felix-parent</artifactId>
   <groupId>org.apache.felix</groupId>
   <version>2.1</version>
   <relativePath>../pom/pom.xml</relativePath>
 </parent>

 <modelVersion>4.0.0</modelVersion>

 <artifactId>maven-bundle-plugin</artifactId>
 <version>2.4.1-SNAPSHOT</version>
 <packaging>maven-plugin</packaging>

 <name>Maven Bundle Plugin</name>
 <description>
  Provides a maven plugin that supports creating an OSGi bundle
  from the contents of the compilation classpath along with its
  resources and dependencies. Plus a zillion other features.
  The plugin uses the Bnd tool (http://www.aqute.biz/Code/Bnd)
 </description>

 <scm>
  <connection>scm:svn:http://svn.apache.org/repos/asf/felix/trunk/bundleplugin</connection>
  <developerConnection>scm:svn:https://svn.apache.org/repos/asf/felix/trunk/bundleplugin</developerConnection>
  <url>http://svn.apache.org/repos/asf/felix/trunk/bundleplugin</url>
 </scm>

 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.5</source>
     <target>1.5</target>
    </configuration>
   </plugin> …
Run Code Online (Sandbox Code Playgroud)

java maven-2 osgi-bundle

19
推荐指数
2
解决办法
6万
查看次数

找不到媒体类型= application/octet-stream的MessageBodyReader

我正在尝试使用以下代码片段从http://api.openweathermap.org/data/2.5/forecast/daily?lat=35&lon=139&cnt=10&mode=json接收json数据:

private WebTarget getWebTarget() {
    Client client = JerseyClientBuilder.newClient();
    return client.target("http://api.openweathermap.org/")
            .path("data")
            .path("2.5");
}

// new one method
    Response response = getWebTarget()
                        .path("daily")
                        .queryParam("q", String.format("%s,%s", town, countryCode))
                        .queryParam("cnt", 10)
                        .queryParam("mode", "json")
                        .request().accept(MediaType.APPLICATION_JSON_TYPE).get();
    WeatherForecast forecast = response.readEntity(WeatherForecast.class);
Run Code Online (Sandbox Code Playgroud)

但最后一行抛出:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= application/octet-stream的MessageBodyReader,类型=类com.app.weather.providers.org.openweathermap.pojo.WeatherForecast,genericType = class com.app .weather.providers.org.openweathermap.pojo.WeatherForecast.

pom.xml中的Jersey依赖项:

<dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.4</version>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId> …
Run Code Online (Sandbox Code Playgroud)

java rest json jersey maven

14
推荐指数
1
解决办法
4万
查看次数

Android:java.lang.OutOfMemoryError:

我开发了一个在Android上使用大量图像的应用程序.

可绘制文件夹中有很多图像说超过100,我正在开发图像动画应用程序.我用imageview来显示GIF图像.我已经将Split gif图像的概念用于多个PNG格式图像,然后使用它.

每次用户进入应用程序时,我都可以看到内存越来越多,直到用户获得java.lang.OutOfMemoryError.

那么处理许多图像的最佳/正确方法是什么?

以下是我的代码:

dog_animation.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/DogView"
android:orientation="vertical" >

<ImageView
    android:id="@+id/dog_animation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.19" />
Run Code Online (Sandbox Code Playgroud)

dog_animation.xml(Drawable文件夹)

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

<item
    android:drawable="@drawable/image"
    android:duration="50"/>
<item
    android:drawable="@drawable/image1"
    android:duration="50"/>
<item
    android:drawable="@drawable/image2"
    android:duration="50"/>
<item
    android:drawable="@drawable/image3"
    android:duration="50"/>
<item
    android:drawable="@drawable/image4"
    android:duration="50"/>
<item
    android:drawable="@drawable/image5"
    android:duration="50"/>
<item
    android:drawable="@drawable/image6"
    android:duration="50"/>
<item
    android:drawable="@drawable/image7"
    android:duration="50"/>
<item
    android:drawable="@drawable/image8"
    android:duration="50"/>
<item
    android:drawable="@drawable/image9"
    android:duration="50"/>
<item
    android:drawable="@drawable/image10"
    android:duration="50"/>
<item
    android:drawable="@drawable/image11"
    android:duration="50"/>
<item
    android:drawable="@drawable/image12"
    android:duration="50"/>
<item
    android:drawable="@drawable/image13"
    android:duration="50"/>
<item
    android:drawable="@drawable/image14"
    android:duration="50"/>
<item
    android:drawable="@drawable/image15"
    android:duration="50"/>
<item
    android:drawable="@drawable/image16"
    android:duration="50"/>
<item
    android:drawable="@drawable/image17"
    android:duration="50"/> …
Run Code Online (Sandbox Code Playgroud)

java xml animation android android-imageview

13
推荐指数
2
解决办法
4万
查看次数

QueryDsl - 集合表达式中的子查询

我正在使用spring-data-jpa和querydsl(3.2.3)
我有一个场景,我根据用户文件管理器/输入创建一组谓词.所有这些都来了BooleanExpression.

我的简化模型如下:

@Entity
public class Invoice {
    @ManyToOne
    private Supplier supplier;
}

@Entity
public class Supplier {
    private String number;
}

@Entity
public class Company {
    private String number;
    private boolean active
}
Run Code Online (Sandbox Code Playgroud)

现在,我正在努力解决的是这个问题:

SELECT * FROM Invoice WHERE invoice.supplier.number in (SELECT number from Company where active=true)
Run Code Online (Sandbox Code Playgroud)

所以基本上我需要以CollectionExpression类似格式的子查询来获取所有公司的数字并将其设置为in()表达式.

我的spring-data存储库实现CustomQueryDslJpaRepository了反过来扩展JpaRepositoryQueryDslPredicateExecutor.
我希望答案是直截了当的,但我对querydsl很新,到目前为止还没有找到解决方案.

java spring querydsl spring-data

13
推荐指数
1
解决办法
3万
查看次数

如何在Java中将字节数组转换为十六进制格式

我知道您可以使用printf并使用StringBuilder.append(String.format("%x", byte))将值转换为HEX值并在控制台上显示它们.但我希望能够实际格式化字节数组,以便每个字节显示为HEX而不是十进制.

这是我的代码的一部分,我已经说过我说的前两种方式:

if(bytes > 0)
    {
        byteArray = new byte[bytes]; // Set up array to receive these values.

        for(int i=0; i<bytes; i++)
        {
            byteString = hexSubString(hexString, offSet, CHARSPERBYTE, false); // Isolate digits for a single byte.
            Log.d("HEXSTRING", byteString);

            if(byteString.length() > 0)
            {
                byteArray[i] = (byte)Integer.parseInt(byteString, 16); // Parse value into binary data array.
            }
            else
            {
                System.out.println("String is empty!");
            }

            offSet += CHARSPERBYTE; // Set up for next word hex.    
        }

        StringBuilder sb = new …
Run Code Online (Sandbox Code Playgroud)

java format stringbuilder hex bytearray

12
推荐指数
3
解决办法
4万
查看次数

强制JVM在没有页面缓存的情况下执行所有IO(例如O_DIRECT)

我正在做一些用Java编写的应用程序的基准测试.对于实验来说非常重要的是结果不受页面缓存的影响(我正在使用linux)

因此,无论何时打开文件,避免页面缓存的最佳方法是使用O_DIRECT.因此,我改变了jre源代码中的相应代码.

我的方法适用于通过FileOutputStream(例如写作)的所有内容,但它不适用于FileInputStream(例如阅读).

将O_DIRECT添加到open-call时FileInputStream,JVM无法加载任何类:

Error: Could not find or load main class perf.TestDirectIO
Run Code Online (Sandbox Code Playgroud)

这个错误是不是一个类路径的问题,因为我可以通过使用"未受攻击" JVM修复它.

因此打开文件似乎存在问题.

我对如何解决这个问题的任何建议感到非常高兴.

如果有人想做类似的事情,我已经在我的博客中记录了整个黑客.


作为参考,这些是我对JVM代码所做的更改:

jdk/src/share/native/java/io/FileInputStream.c:

 @@ -58,7 +60,8 @@
 JNIEXPORT void JNICALL
 Java_java_io_FileInputStream_open(JNIEnv *env, jobject this, jstring path) {
-    fileOpen(env, this, path, fis_fd, O_RDONLY);
+    fileOpen(env, this, path, fis_fd, O_RDONLY | O_DIRECT); // this is the change that causes all the problems
 }
Run Code Online (Sandbox Code Playgroud)

此更改有效 jdk/src/solaris/native/java/io/FileOutputStream_md.c:

@@ -55,8 +55,10 @@
 JNIEXPORT void JNICALL
 Java_java_io_FileOutputStream_open(JNIEnv *env, …
Run Code Online (Sandbox Code Playgroud)

java linux jvm

11
推荐指数
4
解决办法
4897
查看次数

如何在PHP中使用prompt()等用户输入?

如何prompt('example prompt')在javascript中创建PHP输入?

不像一个表格,像一个prompt().

php text input

9
推荐指数
2
解决办法
6万
查看次数

为什么UIView的exclusiveTouch属性没有阻塞?

我正在按下按钮,启动一个简单UIViewtextField- 让我们称呼它orderSetNameView.我希望将此视图设为模态,但不使用

[UIViewController presentModalViewContoller:animated:].

我似乎可以简单地设定textInputView.exclusiveTouch = YES实现这一目标.

Apple文档说exclusiveTouch:

一个布尔值,指示接收器是否专门处理触摸事件.如果是,则接收器阻止同一窗口中的其他视图接收触摸事件; 否则,它没有.默认值为NO.

我假设"相同的窗口"意味着相同的UIWindow,其中我只有一个.

问题是,当我实例化我的orderSetNameView,将其添加为子视图,并设置exclusiveTouch = YES,触摸事件发生在我的应用程序的所有其他视图中,即其他视图中的触摸事件未按预期阻止.

    // ....

    [self.view addSubview:self.orderSetNameView];
    [self.orderSetNameView openWithAnimationForAnimationStyle:kMK_AnimationStyleScaleFromCenter];
}

// Set as modal
self.orderSetNameView.exclusiveTouch = YES;
Run Code Online (Sandbox Code Playgroud)

不应该orderSetNameView阻止所有其他视图中的触摸事件?我错过了什么?

iphone uiview ipad

8
推荐指数
1
解决办法
8698
查看次数

Size在ArrayList中具有私有访问权限

我编写了一个使用String填充的相当标准的代码,ArrayList但是当我尝试运行它时,我收到以下错误:

error: size has private access in ArrayList.

代码如下:

System.out.println(testedArticles.size);
Run Code Online (Sandbox Code Playgroud)

java private arraylist

8
推荐指数
2
解决办法
1万
查看次数