key-value model

http://stackoverflow.com/questions/2973041/a-keyvaluepair-in-java

I'm looking for a KeyValuePair class in Java.
Since java.util heavily uses interfaces there is no concrete implementation provided, only the Map.Entry interface.

Is there some canonical implementation I can import? It is one of those "plumbers programming" classes I hate to implement 100x times.

share | improve this question
 
2  
I'd recommend against using a generic KeyValuePair class. Much better to define a domain-specific class with more informative accessors (e.g. getProductId(), getProductPrice()). –   Adamski  Jun 4 '10 at 10:04
12  
@Adamski: I disagree. We don't use domain-specific Map or List classes, why should this be any different? A KeyValuePair is just a Map with one entry. –   skaffman  Jun 4 '10 at 10:21
2  
@skaffman: Fair point but I think the concept of a generic Pair is more easily open to abuse than passing Collections around. For example, if someone wants to return two values from a method it's tempting to return a Pair<X,Y> but typically this is indicative of an underlying design problem (e.g. Why aren't X and Y unified somehow anyway in the domain model if they're intrinsically related?) –   Adamski  Jun 4 '10 at 10:31
7  
@Adamski: Because it's not <X,Y>, it's <Key,Value>. He's not asking for an arbitrary Pair type, he's asking for a KeyValuePair type. –   skaffman  Jun 4 '10 at 10:38
1  
Despite this I would much rather pass around a single object instance in my code, potentially with a getKey() method defined; e.g. I prefer: processProduct(Product) to processProduct(KeyValuePair<Integer, Product>) where the Integer is the product ID. I agree that KeyValuePair (i.e. Map.Entry) or even a generic Pair is useful within a method logic but I don't like it to "leak out" as a return type or be supplied as a method parameter; e.g. I never pass around Map.Entry. –   Adamski  Jun 4 '10 at 10:52 

4 Answers

up vote 102 down vote accepted

AbstractMap.SimpleEntry is generic and can be useful.

share | improve this answer
 
6  
...or AbstractMap.SimpleImmutableEntry if appropriate. –   karmakaze  Aug 7 '13 at 3:09
1  
java.util.AbstractMap.SimpleEntry +1 because in SE. –   Thufir  Sep 2 '13 at 9:45

Android programmers could use BasicNameValuePair

share | improve this answer
 
3  
That's not just for Android programmers, it's any java program. –   Cody  Dec 13 '11 at 12:45
1  
@doctor-oreo yes any java programmer could download jar from hc.apache.org but android has those built in –   kreker  Dec 13 '11 at 21:58 
17  
Actually for Android users, a good choice is android.util.Pair. –   chhabrakadabra  Apr 4 '12 at 1:43
 
@GalDude33 why is that? It's added in 5 api level. –   kreker  Jul 16 at 0:05

The Pair class from Commons Lang might help:

Pair<String, String> keyValue = new ImmutablePair("key", "value");

Of course, you would need to include commons-lang.

share | improve this answer
 
 
The url is not working –   Peter Fortuin  Mar 31 '13 at 17:57
2  
url changes from time to time. I googled Pair Commons Lang" and found it. I removed the link to prevent further downvotes :-( –   remipod  Apr 1 '13 at 17:52
1  
android.util.Pair –   Arvin  Jul 18 '13 at 9:02

For android and http requests i use NameValuePair, is from the package org.apache.http



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I do not have access to your system logs or configurations to give you a specific solution. However, here are some general troubleshooting steps that you can try to fix the issue of Redis persistent key-value database failing to start: 1. Check if Redis is already running: Run the following command in the terminal to check if Redis is already running: ``` sudo systemctl status redis ``` If Redis is already running, stop it using the following command: ``` sudo systemctl stop redis ``` 2. Check Redis logs: Run the following command to check the Redis logs: ``` tail -f /var/log/redis/redis-server.log ``` This will show the latest logs from Redis. Look for any error messages or warnings that can help you identify the issue. 3. Check Redis configuration: Check the Redis configuration file (/etc/redis/redis.conf) and make sure that the following settings are correct: ``` daemonize yes pidfile /var/run/redis/redis-server.pid logfile /var/log/redis/redis-server.log dir /var/lib/redis ``` 4. Check Redis data directory permissions: Make sure that the Redis data directory (/var/lib/redis) has the correct permissions. Run the following command to set the correct permissions: ``` sudo chown -R redis:redis /var/lib/redis ``` 5. Check Redis port: Make sure that Redis is listening on the correct port (default is 6379). You can check this in the Redis configuration file (/etc/redis/redis.conf). 6. Restart Redis: Restart Redis using the following command: ``` sudo systemctl start redis ``` If the issue persists, try reinstalling Redis or contacting Redis support for further assistance.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值