I have a lot of trouble displaying the key and the value of a HahMap. My wish is to make a selecteOneMenu and selecteItems with the keys / values. here is my code and my display. I cannot display keys and values separately. thank you in advance
@ManagedBean(name = "autoComplete")
@Component
@SessionScoped
public class AutoComplete extends ManageUIBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(AutoComplete.class);
private Map<String, Object> listChoices = new LinkedHashMap<String, Object>();
private List<String> keyList;
public Map<String, Object> choicesList() {
Calendar mCalendar = Calendar.getInstance();
Calendar mCalendar2 = Calendar.getInstance();
mCalendar2.add(Calendar.MONTH, -6);
Calendar mCalendar3 = Calendar.getInstance();
mCalendar3.add(Calendar.MONTH, -11);
String month = mCalendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
listChoices.put("Le mois en cours ", month);
listChoices.put("Les 6 derniers mois ",
mCalendar2.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()));
listChoices.put("Historique", mCalendar3.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault()));
Map.Entry<String, Object> valuesofMonth= listChoices.entrySet().iterator().next();
for (String key : listChoices.keySet()) {
key = valuesofMonth.getKey();
}
for (Object value : listChoices.values()) {
value = valuesofMonth.getValue();
}
logger.info("List of choice statistique month:"+ valuesofMonth);
for (Map.Entry<String, Object> entry : listChoices.entrySet()) {
String choiceskey= entry.getKey();
Object choicesvalues= entry.getValue();
logger.debug(choiceskey + ":" +choicesvalues);
return listChoices;
}
return null;
}
public List<String> getKeyList() {
return new ArrayList<>( choicesList().keySet());
}
public void setKeyList(List<String> keyList) {
keyList.add( choicesList().keySet().stream().findFirst().get());
this.keyList = keyList;
}
public Map<String, Object> getListChoices() {
return listChoices;
}
public void setListChoices(Map<String, Object> listChoices) {
this.listChoices = listChoices;
}
//--Listener---------------------------------------
public void versionChanged(ValueChangeEvent event) {
String newListChoices = event.getNewValue().toString();
for (Map.Entry<String, Object> entry : listChoices.entrySet()) {
if (entry.getValue().toString().equals(newListChoices)) {
FacesContext.getCurrentInstance().getViewRoot().setLocale((Locale) entry.getValue());
break;
}
}
}
public void submit() {
String selectedLabel = (String) listChoices.get(keyList);
logger.debug("selectedLabel"+ selectedLabel);
}
/**
* @return first and last date of current Month
*/
public Pair<Date, Date> getDateRange() {
Date begining, end;
{
java.util.Calendar calendar = getCalendareForNow();
calendar.set(java.util.Calendar.DAY_OF_MONTH, calendar.getActualMinimum(java.util.Calendar.DAY_OF_MONTH));
setTimeToEndOfDay(calendar);
begining = calendar.getTime();
}
{
java.util.Calendar calendar = getCalendareForNow();
calendar.set(java.util.Calendar.DAY_OF_MONTH, calendar.getActualMaximum(java.util.Calendar.DAY_OF_MONTH));
setTimeToEndOfDay(calendar);
end = calendar.getTime();
}
return Pair.of(begining, end);
}
/**
* @param calendar
*/
private static void setTimeToEndOfDay(java.util.Calendar calendar) {
calendar.set(java.util.Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(java.util.Calendar.SECOND, 0);
calendar.set(java.util.Calendar.MILLISECOND, 0);
}
/**
* @return
*
* For other month we can modify calendar last or preview to add or retrieve the number of months
*/
private java.util.Calendar getCalendareForNow() {
java.util.Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(new Date());
int month= calendar.get(Calendar.MONTH);
return calendar;
}
this is a jsf code:
<p:outputLabel value="#{msg['practis-web.statistiques.mensuel.mois']}" />
<p:selectOneMenu value="#{autoComplete.keyList}">
<f:selectItem itemLabel="#{msg['practis-web.content.creerDemande.selectionner.label']} " itemValue="" noSelectionOption="true" />
<f:selectItems var="entry" value="#{autoComplete.choicesList()}" itemLabel="#{autoComplete.choicesList().keySet()}" itemValue="#{autoComplete.choicesList().values()}" >
</f:selectItems>
</p:selectOneMenu>
and the display I have is:
**In the view le key and values are display, and duplicate
**