小编Sth*_*ita的帖子

Java中的静态块未执行

class Test{
    public static void main(String arg[]){    
        System.out.println("**MAIN METHOD");
        System.out.println(Mno.VAL);//SOP(9090);
        System.out.println(Mno.VAL+100);//SOP(9190);
    }

}

class Mno{
    final static int VAL=9090;
    static{
        System.out.println("**STATIC BLOCK OF Mno\t:"+VAL);
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道static在加载类时执行的块.但是在这种情况下,类中的实例变量Mnofinal因为static块没有执行.

为什么会这样?如果我删除它final,它会工作正常吗?

首先分配哪个内存,static final变量或static块?

如果由于final访问修饰符而没有加载类,那么变量如何获取内存?

java static access-modifiers

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

HashSet与ArrayList的CPU使用率很高

我有104k字符串值,其中89k是唯一的.我想检查一下该列表中是否存在字符串.

这是我的类及其保存所有这些记录的方法.

public class TestClass {
    private static TestClass singletonObj = null;
    private List<String> stringList= null;

    public static synchronized TestClass getInstance() {
        if(singletonObj == null) {
            singletonObj = new TestClass();
        }
        return singletonObj;
    }


    public boolean isValidString(String token) {
        if(stringList == null) {
            init();
        }
        if(stringList != null && token != null && !token.isEmpty())
            return stringList.contains(token.toLowerCase());
        return false;
    }

    private init() {
     stringList = new ArrayList<String>();
     // put all 104k values in this data structure.
    }
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序尝试同时使用此isValidString()方法,每秒约20个请求.这工作正常但是当我尝试将数据结构更改为时 …

java performance arraylist hashset

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

嵌入式 kafka 无法启动 - 错误

我很难解决这个问题。这是我的 Junit,我正在使用 spring 嵌入式 kafka。当我运行我的测试用例时,我遇到了奇怪的问题/异常。

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ActiveProfiles("test")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext
public class WatchListUpdateTest {

 @ClassRule
 public static KafkaEmbedded KAFKA = new KafkaEmbedded(1, true, "abc");

 @BeforeClass
 public static void startKafka() throws Exception {
  String kafkaBootstrapServers = KAFKA.getBrokersAsString();
  System.out.print("[Embedded Kafka Server:{}]" + kafkaBootstrapServers);
  System.setProperty("kafka.consumer.bootstrap.servers", kafkaBootstrapServers);
  System.setProperty("kafka.producer.bootstrap.servers", kafkaBootstrapServers);

 }

 @Autowired
 ApplicationContext applicationContext;

 private KafkaTestHelper helper = new KafkaTestHelper(KAFKA, "abc");

 @Before
 public void setUp() throws Exception {
  helper.start(KAFKA.getPartitionsPerTopic());
 }

 @After
 public void tearDown() throws Exception {
  helper.stop();
 }

 @Test
 public void testIng() …
Run Code Online (Sandbox Code Playgroud)

junit apache-kafka spring-boot embedded-kafka

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

无法将JSP标记的结果指定为c:set的值

我无法弄清楚设置<c:set>另一个JSP标记的结果的值.

这是我的代码:

  <c:set var="desc" value="<c:choose>
  <c:when test="${model.totalHits < 200}">${model.totalHits}</c:when> 
  <c:otherwise>${model.results.size()}</c:otherwise></c:choose> 
  positions at 
  <c:forEach items="${model.metaCompanies}"  var='item' varStatus='status'>  
  ${item} including ${model.metaDescsingleCompany}
  </c:forEach>
  related to ${model.querymetacompany}."/>
Run Code Online (Sandbox Code Playgroud)

获得此例外:

org.apache.jasper.JasperException: Unterminated &lt;c:set tag
Run Code Online (Sandbox Code Playgroud)

难道我做错了什么 ?有没有其他方法可以实现这种情况?

jsp jstl

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

用Java8进行循环优化

如何用一条线实现这一目标?

我目前正在尝试这样做

示例:

{{"id" :"2", values: ["10","11", "12"]} , {"id" : "3", values : ["23"]}}
Run Code Online (Sandbox Code Playgroud)

{{"id" :"2","value": "10"},{"id": "2","value":"11"},
 {"id" :"3","value":"23"} , {"id" : "2", "value":"12"}}
Run Code Online (Sandbox Code Playgroud)

我的java代码是

Map<Integer, List<Integer>> attrMap = new HashMap<>();
//getAllData() & item.getValues() both returns List

getAllData().forEach(item - > {
    item.getValues().forEach(val - > {
        attrMap.computeIfAbsent(item.getId(), (k) - > 
        new ArrayList < > ()).add(val.getValue());
    });
});
Run Code Online (Sandbox Code Playgroud)

我怎么能只做一行?

foreach for-loop java-8

0
推荐指数
1
解决办法
81
查看次数