我有一个String数组的ArrayList。字符串数组看起来像下面的
Expert
10 Expert
07 Expert
Professional
Systems
10 System
07 System
考虑arraylist上面有字符串数组。我想在专业水平的基础上从arraylist中取出每种类型的独特类型。如果我们在上面列出了cosider,那么我希望Type-A的级别为Expert,Type-B的级别为Expert,Type-C的级别为Expert10,如层次结构Expert位于顶部,Expert10和Expert07低于它。
结构就像我有arraylist包含字符串数组。每个String数组记录都具有类型和专业级别。 Arraylist可以有多个具有不同专业水平的相同类型的记录。我想要每种类型的记录,但具有最高的专业水平。如果不同的专业水平,我有一个列表。现在我的困惑是如何使用该专业水平列表来取出或制作另一个具有最高专业水平的每种类型的单一记录的arraylist。
专业水平列表。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height=" fill_parent "
tools:context="name.company.newapp.Recycler.RecyclerView">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_recycler_view" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
答案 0 :(得分:0)
只需循环并查找,使用地图在循环期间为每种类型存储当前最高专家项目:
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);
}
尚未测试代码:)
答案 1 :(得分:0)
在这里,我使用Pattern-Matcher
来提取专家级别。迭代给定的数组,然后创建数组,如果找到相同的类型 - 比较专家级别 - 如果创建的专家级别更小 - 然后用迭代专家替换。
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();
}
输出
Type-A Date Expert
Type-B Date Expert
Type-C Date Expert10
答案 2 :(得分:0)
使用Java 8 Streams,它就像:
一样简单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;
}
};
};
用法:
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;
}
};
};
}