[英]Java -need logic to add and delete from hashmap comparing values rather than matching keys
我的UI中有2個下拉菜單,選擇一個將為從下拉菜單中選擇的特定值配置另一組復選框。每次我在所選值和復選框中的值之間添加一些連接時,它應該保存在某個位置並正確識別元素,以便我可以刪除它,但仍保留之前的值
---- UI ----
Select box
<button style="display: none;" type="submit" id="selected" name="_eventId_getid"></button>
<label for="name" >Product</label>
<form:select path="selectedProduct" disabled="${isDisabled}" onchange="simulateClick('selected');">
<form:option value="0">Select...</form:option>
<form:options items="${createProject.product}" itemLabel="name" itemValue="id" />
</form:select>
checkbox, depending upon slectbox
<button style="display:none;" type="submit" id="selectCopyType" name="_eventId_submit"> </button>
<c:if test="${not empty createProject.elementType}">
<button style="display: none;" type="submit" id="copy" name="_eventId_getId2"></button>
<input class="checkAllCheckbox" id="selectall" name="selectall" type="checkbox"/>
<span style="display:inline; margin-left:auto;">Select All</span>
<!-- id must be the same as listId above -->
<div class="CFbox" id="id" >
<form:checkboxes items="${ProjectCommand.ElementType}" onclick="simulateClick('copy')" path="CopyType" cssClass="case" itemValue="id" itemLabel="name" name="case"/> </div>
</div>
Class Command
private String seelctedProduct;
private List<Integer> CopyType;
private List<Class1> class1
Class Class1
private Integer seq = 0;
private String product;
private String ElementName
public void getSelectedCopy(Command command)
{
Map<Integer,String[]>map= new HashMap<Integer,String[]>();
if (command.CopyType() != null && command.getCopyType().size() > 0)
{
List<Class1> copyList = new ArrayList<Class1>();
Integer Seq = 0;
if (command.getClass1() != null && command.getClass1().size() <= command.getCopyType.size())
{
//Max CfSeq is stored in the variable -1 .
Seq = command.getClass1().get(command.getClass1.size() - 1).getSeq();
}
for ( int copytype : command.getCopyType())
{
if (command.getCopyType().size() >1)
{
if (copyType <= Seq)
{
copyList.add(command.getClass1().get(Seq - 1));
continue;
}
}
cfSeq++;
map.put(Seq,new String[]{command.getseelctedProduct(),"copyType"});
System.out.println("map"+map);
Class1 objclass1= new Class1();
objclass1.setProduct(command.getseelctedProduct());
objclass1.setElementName(copyType + "");
objclass1.setSeq(Seq);
copyList.add(objclass1);
}
command.setClass1(copyList);
}
else
{
command.setClass1(null);
}
}
現在我沒有獲得正確的序列號,我無法從地圖上刪除正確的成員。.希望你們得到了我正在嘗試做的事情! 謝謝你的幫助。我想我需要邏輯來比較值而不是匹配鍵來從哈希圖中添加和刪除,如果您有其他建議可以正確顯示序列而不是每次都清除對象。
如果您的問題確實是有一個值->鍵查找,則應該查看番石榴或Commons集合。
番石榴: http : //www.discursive.com/books/cjcook/reference/collections-sect-key-by-value
共同收藏品比迪地圖: http ://commons.apache.org/collections/api-3.1/org/apache/commons/collections/BidiMap.html
Commons集合既古老又穩定,而番石榴則使用泛型提供了許多新的范例和類型安全性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.