首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从列表中提取每种类型的单一记录

从列表中提取每种类型的单一记录
EN

Stack Overflow用户
提问于 2015-11-08 04:43:27
回答 3查看 57关注 0票数 0

我有一个字符串数组的ArrayList。字符串数组如下所示

代码语言:javascript
复制
["Type-A","Date","Expert"]
["Type-A","Date","07 Expert"]
["Type-A","Date","10 Expert"]
["Type-B","Date","Expert"]
["Type-B","Date","10 Expert"]
["Type-C","Date","07 Expert"]
["Type-C","Date","10 Expert"]

考虑一下arraylist有上面的字符串数组。我想在你的专业水平的基础上,从每一种类型中挑选出独特的类型。如果我们查看上面的列表,那么我希望类型-A作为专家级别,B类型以级别作为专家,类型-C类型的层次为Expert10,专家在顶层,Expert10和Expert07较低。

结构类似于数组,其中包含字符串数组。每个字符串数组记录都具有类型专门知识级别。Arraylist可以有多个相同类型的记录,具有不同的专业水平。我想记录每一种类型,但具有最高的专业水平。我有一个清单,如果不同的专业水平。现在,我的困惑是如何使用该专业级别列表来获取或制作另一个具有最高专业水平的每种类型的单一记录的数组列表。

专业水平清单。

代码语言:javascript
复制
Expert 
10 Expert  
07 Expert
Professional
Systems 
10 System 
07 System 
EN

回答 3

Stack Overflow用户

发布于 2015-11-08 05:03:59

只需循环它并找出,在循环期间使用一个映射来存储当前每个类型的最大专家项:

代码语言:javascript
复制
private boolean isMoreExpert(String expertPicked, String expertToCompare){
    //TODO: return true is expertPicked is lower expert than expertToCompare, false otherwise
    return false;
}

private List<String[]> mapToList<Map<String[]> map>{
    //TODO: iterate the map and store items to a list
    return null;
}
private List<String[]> getMostExpertListOfEachType(List<String[]> items){
    Map<String, String[]> tempRecord = new HashMap<>();
    for(String[] item in items){
        //assume first item is the type
        String[] current = tempRecord.get(item[0]);
        //assume the third item is the expert 
        if(current != null ){
             if(isMoreExpert(current[2], item[2])){tempRecord.put(item[0], item);}
        }else{
            tempRecord.put(item[0], item);
        }
    }
    return mapToList(tempRecord);
}

尚未测试代码:)

票数 0
EN

Stack Overflow用户

发布于 2015-11-08 05:16:22

在这里,我使用Pattern-Matcher提取专家级别。迭代给定的数组,然后创建数组,如果找到相同的类型--比较专家级别--如果创建的专家级别较小,则用迭代专家替换。

代码语言:javascript
复制
List<String[]> givenExp = Arrays.asList(
        new String[]{"Type-A", "Date", "Expert"},
        new String[]{"Type-A", "Date", "Expert07"},
        new String[]{"Type-A", "Date", "Expert10"},
        new String[]{"Type-B", "Date", "Expert"},
        new String[]{"Type-B", "Date", "Expert10"},
        new String[]{"Type-C", "Date", "Expert07"},
        new String[]{"Type-C", "Date", "Expert10"});
List<String[]> filteredExp = new ArrayList<>();
Pattern pat = Pattern.compile("(?<=Expert)\\d*"); 
Matcher gmat, fmat;
String gexplvl, fexplvl;
int giexplvl, fiexplvl;
main:
for (String[] gexp : givenExp) {
    for (String[] fexp : filteredExp) {
        if (fexp[0].equals(gexp[0])) {
            gmat = pat.matcher(gexp[2]);
            fmat = pat.matcher(fexp[2]);
            gmat.find();
            fmat.find();
            gexplvl = gmat.group();
            fexplvl = fmat.group();
            if (gexplvl.length() == 0) {
                filteredExp.remove(fexp);
                filteredExp.add(gexp);
            } else {
                if (fexplvl.length() != 0 && Integer.parseInt(fexplvl) < Integer.parseInt(gexplvl)) {
                    filteredExp.remove(fexp);
                    filteredExp.add(gexp);
                }
            }
            continue main;
        }
    }
    filteredExp.add(gexp);
}
for (String[] fexp : filteredExp) {
    for (String val : fexp) {
        System.out.printf("%-10s", val);
    }
    System.out.println();
}

输出:

代码语言:javascript
复制
Type-A    Date      Expert    
Type-B    Date      Expert    
Type-C    Date      Expert10  
票数 0
EN

Stack Overflow用户

发布于 2015-11-08 06:13:54

使用Java 8 ,它非常简单,如:

代码语言:javascript
复制
list.stream().sorted((s1, s2) -> s2[2].compareTo(s1[2])).filter(isMaxExpert())
                        .collect(Collectors.toList());

private static Predicate<String[]> isMaxExpert() {
   final Map<String, String[]> map = new HashMap<>();
        return p -> {
            if (map.get(p[0]) != null) {
                return false;
            } else {
                map.put(p[0], p);
                return true;
            }
     };
};

用法:

代码语言:javascript
复制
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class SelectOne {

    public static void main(String[] args) {

        List<String[]> list = new ArrayList<>();
        list.add(new String[] { "Type-A", "Date", "Expert" });
        list.add(new String[] { "Type-A", "Date", "07 Expert" });
        list.add(new String[] { "Type-A", "Date", "10 Expert" });
        list.add(new String[] { "Type-B", "Date", "Expert" });
        list.add(new String[] { "Type-B", "Date", "10 Expert" });
        list.add(new String[] { "Type-C", "Date", "07 Expert" });
        list.add(new String[] { "Type-C", "Date", "10 Expert" });

        List<String[]> modified = list.stream()
                .sorted((s1, s2) -> s2[2].compareTo(s1[2])).filter(isMaxExpert())
                .collect(Collectors.toList());

        for (String[] strArray : modified) {
            System.out.println(" " + strArray[0] + " " + strArray[1] + " "
                    + strArray[2]);
        }
    }

    private static Predicate<String[]> isMaxExpert() {
        final Map<String, String[]> map = new HashMap<>();
        return p -> {
            if (map.get(p[0]) != null) {
                return false;
            } else {
                map.put(p[0], p);
                return true;
            }
        };
    };
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33590824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档