按特定顺序获取ArrayList中的值

时间:2014-04-28 07:22:02

标签: java sorting collections arraylist

我有两个ArrayLists,如下所示,

 private List<Patienthealthdata> patientHealthDataList = new ArrayList<Patienthealthdata>();
public List<Patienthealthdata> getPatientHealthDataList() {patientHealthDataSet = medicalHistoryService.getLatestMedicalHistoryDataAttributes(patient);Map<Integer, String> heightMap = new LinkedHashMap<Integer, String>();
        List<Patienthealthdata> PatienthealthdataDummy = new ArrayList<Patienthealthdata>();
        PatienthealthdataDummy.addAll(patientHealthDataSet);
        for (int i = 0; i < PatienthealthdataDummy.size(); i++) {
            if (PatienthealthdataDummy.get(i).getHealthdata().getName().contains("Height (ft)")) {
                heightMap.put(1, PatienthealthdataDummy.get(i).getHealthDataValue());if (PatienthealthdataDummy.get(i).getHealthdata().getName().contains("Height (in)")) {
                heightMap.put(2, PatienthealthdataDummy.get(i).getHealthDataValue());}for (Patienthealthdata patientHealthData : patientHealthDataSet) { 
            String valHeightInch = "";
            String valHeightFt = "";
            String newValue = "";
            String groupName = patientHealthData.getHealthdata().getHealthdatagrouplinks().iterator().next().getHealthdatagroup().getName().trim();
            if (groupName.equals(Constants.HELTH_GROUP_BMI)) {
                if (patientHealthData.getHealthdata().getName().contains("Height (ft)")) { for (Entry<Integer, String> entry : heightMap.entrySet()) {if (entry.getKey() == 1) {
                            valHeightFt = entry.getValue();}
                        if (entry.getKey() == 2) {
                            valHeightInch = entry.getValue();
                        }}patientHealthDataList.add(patientHealthData); 
            } //End of If condition 

我想在检索时按照预期的顺序对它们进行排序。任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

从你的标题中

使用迭代器

Iterator<E> iterator()

检索插入顺序中的元素

正如@Keyser先说的是arraylist在哪里?

<强>更新

要对地图进行排序,请使用此

for (Map.Entry<String, String> entry : heightMap.entrySet())

LinkedHashMap按照插入顺序保存按键,而TreeMap则通过比较器或元素的自然可比较顺序进行排序。