Java 从列表中提取给定字符串中匹配数最高的项

Java 从列表中提取给定字符串中匹配数最高的项,java,android,sqlite,arraylist,Java,Android,Sqlite,Arraylist,我的应用程序中有2个ArrayList。第一个arraylist是对象类型,它包含一个问题列表。现在,此问题列表有一个名为“”关键字“的字段。这是一个字符串,但可以包含逗号分隔的关键字 现在我有了一个文本字段,用户可以根据这些关键字搜索问题。我面临的问题是,我想根据关键字匹配的数量从问题列表中筛选出问题 例如,用户在搜索文本字段中输入了3个逗号分隔的关键字。我现在想要的是,如果所有3个关键字都与问题列表中的某个值匹配,那么我必须只返回这些元素。这部分很简单,我能做到 但是如果我们在列表中没有得到

我的应用程序中有2个ArrayList。第一个arraylist是对象类型,它包含一个问题列表。现在,此问题列表有一个名为“”关键字“的字段。这是一个字符串,但可以包含逗号分隔的关键字

现在我有了一个文本字段,用户可以根据这些关键字搜索问题。我面临的问题是,我想根据关键字匹配的数量从问题列表中筛选出问题

例如,用户在搜索文本字段中输入了3个逗号分隔的关键字。我现在想要的是,如果所有3个关键字都与问题列表中的某个值匹配,那么我必须只返回这些元素。这部分很简单,我能做到

但是如果我们在列表中没有得到任何精确的匹配,那么我必须找到具有最大关键字匹配的项,即如果逗号分隔字符串中的3个关键字中有2个与列表中的某个项匹配,那么我必须返回该项作为结果

存储在列表中的值:-

a) Hi, Hello, Hola, Bonjour.

b) Hi, Hello

c) Hi
Hi, Hello, Hola
搜索文本中输入的值:-

a) Hi, Hello, Hola, Bonjour.

b) Hi, Hello

c) Hi
Hi, Hello, Hola
现在作为响应,我只需要第一个元素,因为它有3个关键字与用户输入的内容匹配。 我不知道该怎么做。此外,我正在从sqlite数据库获取这个问题列表,所以如果这可以通过一些sql查询完成,那么我也准备好了

这是我当前的filter方法代码

 public ArrayList<QuestionAnswerModal> filter(String keyword,boolean isQuestionSearch) {
    ArrayList<QuestionAnswerModal> arrayList = new ArrayList<>();

    if (!isQuestionSearch) {
        for (QuestionAnswerModal modal : questionAnswerArrayList) {
            if (modal.getKeyword().equalsIgnoreCase(keyword)) {
                arrayList.add(modal);
            }else{
                ArrayList<String> keywords=new ArrayList<>();
                String[]word=modal.getKeyword().split(",");

            }
        }
        if (arrayList.size() > 0) {
        System.out.print("list size "+arrayList.size());


        } else {
              System.out.print("No records found");

        }
        return arrayList;
    }else{
        for (QuestionAnswerModal modal : questionAnswerArrayList) {
            if (modal.getQuestion().equalsIgnoreCase(keyword)) {
                arrayList.add(modal);
            }
        }
        if (arrayList.size() > 0) {
          System.out.print("list size "+arrayList.size());


        } else {
            System.out.print("No records found");
        }
        return arrayList;
    }
}
publicArrayList过滤器(字符串关键字,布尔isQuestionSearch){
ArrayList ArrayList=新的ArrayList();
如果(!isQuestionSearch){
for(问题回答模式:问题回答列表){
if(modal.getKeyword().equalsIgnoreCase(关键字)){
arrayList.add(模态);
}否则{
ArrayList关键字=新建ArrayList();
字符串[]word=modal.getKeyword().split(“,”);
}
}
如果(arrayList.size()>0){
System.out.print(“列表大小”+arrayList.size());
}否则{
系统输出打印(“未找到记录”);
}
返回数组列表;
}否则{
for(问题回答模式:问题回答列表){
if(modal.getQuestion().equalsIgnoreCase(关键字)){
arrayList.add(模态);
}
}
如果(arrayList.size()>0){
System.out.print(“列表大小”+arrayList.size());
}否则{
系统输出打印(“未找到记录”);
}
返回数组列表;
}
}

我把它作为一个练习留给您,让您了解这个解决方案是如何工作的,但您可以随意提问


Java 7解决方案:

import java.util.*;

import static org.apache.commons.lang3.StringUtils.trimToEmpty;

public class MaxMatchFinder {

    public static void main(String[] args) {

        Map<String, Set<String>> tagsByName = new HashMap<>();
        tagsByName.put("a", new HashSet<>(Arrays.asList("Hi", "Hello", "Hola", "Bonjour")));
        tagsByName.put("b", new HashSet<>(Arrays.asList("Hi", "Hello")));
        tagsByName.put("c", new HashSet<>(Arrays.asList("Hi")));

        String searchText = "Hi, Hello, Hola";

        String[] tagsToFind = searchText.split(",");

        Map<String, Integer> matchCountsByEntryName = new HashMap<>();

        for (String tagToFind : tagsToFind) {
            for (String entryName : tagsByName.keySet()) {
                Set<String> tags = tagsByName.get(entryName);
                if (tags.contains(trimToEmpty(tagToFind))) {
                    Integer count = matchCountsByEntryName.get(entryName);
                    Integer incrementedCount = count == null ? 1 : count + 1;
                    matchCountsByEntryName.put(entryName, incrementedCount);
                }
            }
        }

        List<Map.Entry<String, Integer>> sortedEntries = new ArrayList<>(matchCountsByEntryName.entrySet());
        Collections.sort(sortedEntries, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
                return e2.getValue().compareTo(e1.getValue());
            }
        });

        Map.Entry<String, Integer> entryWithMostMatches = sortedEntries.get(0);

        System.out.printf("Of the entries to be searched," +
            " entry \"%s\" contains the most matches (%d).\n",
                entryWithMostMatches.getKey(), entryWithMostMatches.getValue());
    }
}
import java.util.*;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.trimToEmpty;

public class MaxMatchFinder {

    public static void main(String[] args) {

        Map<String, Set<String>> tagsByName = new HashMap<>();
        tagsByName.put("a", new HashSet<>(Arrays.asList("Hi", "Hello", "Hola", "Bonjour")));
        tagsByName.put("b", new HashSet<>(Arrays.asList("Hi", "Hello")));
        tagsByName.put("c", new HashSet<>(Arrays.asList("Hi")));

        String searchText = "Hi, Hello, Hola";

        String[] tagsToFind = searchText.split(",");

        Map<String, Integer> matchCountsByEntryName = new HashMap<>();

        Arrays.stream(tagsToFind)
                .forEach(tagToFind -> {
                    for (String entryName : tagsByName.keySet()) {
                        Set<String> tags = tagsByName.get(entryName);
                        if (tags.contains(trimToEmpty(tagToFind))) {
                            matchCountsByEntryName.compute(entryName, (k, v) -> v == null ? 1 : v + 1);
                        }
                    }
                });

        List<Map.Entry<String, Integer>> sortedEntries = matchCountsByEntryName.entrySet().stream()
                .sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
                .collect(Collectors.toList());

        Map.Entry<String, Integer> entryWithMostMatches = sortedEntries.get(0);

        System.out.printf("Of the entries to be searched," +
            " entry \"%s\" contains the most matches (%d).\n",
                entryWithMostMatches.getKey(), entryWithMostMatches.getValue());
    }
}
import java.util.*;
导入静态org.apache.commons.lang3.StringUtils.trimToEmpty;
公共类MaxMatchFinder{
公共静态void main(字符串[]args){
Map tagsByName=newhashmap();
tagsByName.put(“a”,新的HashSet(Arrays.asList(“Hi”、“Hello”、“Hola”、“Bonjour”));
tagsByName.put(“b”,新的哈希集(Arrays.asList(“Hi”,“Hello”));
tagsByName.put(“c”,新的哈希集(Arrays.asList(“Hi”));
String searchText=“嗨,你好,你好”;
字符串[]tagsToFind=searchText.split(“,”);
Map matchCountsByEntryName=新HashMap();
for(字符串tagToFind:tagsToFind){
对于(字符串入口名:tagsByName.keySet()){
Set tags=tagsByName.get(entryName);
if(tags.contains(trimToEmpty(tagToFind))){
整数计数=matchCountsByEntryName.get(entryName);
整数incrementedCount=count==null?1:count+1;
matchCountsByEntryName.put(entryName,incrementedCount);
}
}
}
List sortedEntries=new ArrayList(matchCountsByEntryName.entrySet());
Collections.sort(sortedntry,新的Comparator(){
@凌驾
公共整数比较(Map.Entry e1,Map.Entry e2){
返回e2.getValue().compareTo(e1.getValue());
}
});
Map.Entry entryWithMostMatches=sortedentry.get(0);
System.out.printf(“要搜索的条目,”+
“条目\%s\”包含的匹配项最多(%d)。\n”,
entryWithMostMatches.getKey(),entryWithMostMatches.getValue());
}
}

Java 8解决方案:

import java.util.*;

import static org.apache.commons.lang3.StringUtils.trimToEmpty;

public class MaxMatchFinder {

    public static void main(String[] args) {

        Map<String, Set<String>> tagsByName = new HashMap<>();
        tagsByName.put("a", new HashSet<>(Arrays.asList("Hi", "Hello", "Hola", "Bonjour")));
        tagsByName.put("b", new HashSet<>(Arrays.asList("Hi", "Hello")));
        tagsByName.put("c", new HashSet<>(Arrays.asList("Hi")));

        String searchText = "Hi, Hello, Hola";

        String[] tagsToFind = searchText.split(",");

        Map<String, Integer> matchCountsByEntryName = new HashMap<>();

        for (String tagToFind : tagsToFind) {
            for (String entryName : tagsByName.keySet()) {
                Set<String> tags = tagsByName.get(entryName);
                if (tags.contains(trimToEmpty(tagToFind))) {
                    Integer count = matchCountsByEntryName.get(entryName);
                    Integer incrementedCount = count == null ? 1 : count + 1;
                    matchCountsByEntryName.put(entryName, incrementedCount);
                }
            }
        }

        List<Map.Entry<String, Integer>> sortedEntries = new ArrayList<>(matchCountsByEntryName.entrySet());
        Collections.sort(sortedEntries, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2) {
                return e2.getValue().compareTo(e1.getValue());
            }
        });

        Map.Entry<String, Integer> entryWithMostMatches = sortedEntries.get(0);

        System.out.printf("Of the entries to be searched," +
            " entry \"%s\" contains the most matches (%d).\n",
                entryWithMostMatches.getKey(), entryWithMostMatches.getValue());
    }
}
import java.util.*;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.trimToEmpty;

public class MaxMatchFinder {

    public static void main(String[] args) {

        Map<String, Set<String>> tagsByName = new HashMap<>();
        tagsByName.put("a", new HashSet<>(Arrays.asList("Hi", "Hello", "Hola", "Bonjour")));
        tagsByName.put("b", new HashSet<>(Arrays.asList("Hi", "Hello")));
        tagsByName.put("c", new HashSet<>(Arrays.asList("Hi")));

        String searchText = "Hi, Hello, Hola";

        String[] tagsToFind = searchText.split(",");

        Map<String, Integer> matchCountsByEntryName = new HashMap<>();

        Arrays.stream(tagsToFind)
                .forEach(tagToFind -> {
                    for (String entryName : tagsByName.keySet()) {
                        Set<String> tags = tagsByName.get(entryName);
                        if (tags.contains(trimToEmpty(tagToFind))) {
                            matchCountsByEntryName.compute(entryName, (k, v) -> v == null ? 1 : v + 1);
                        }
                    }
                });

        List<Map.Entry<String, Integer>> sortedEntries = matchCountsByEntryName.entrySet().stream()
                .sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
                .collect(Collectors.toList());

        Map.Entry<String, Integer> entryWithMostMatches = sortedEntries.get(0);

        System.out.printf("Of the entries to be searched," +
            " entry \"%s\" contains the most matches (%d).\n",
                entryWithMostMatches.getKey(), entryWithMostMatches.getValue());
    }
}
import java.util.*;
导入java.util.stream.collector;
导入静态org.apache.commons.lang3.StringUtils.trimToEmpty;
公共类MaxMatchFinder{
公共静态void main(字符串[]args){
Map tagsByName=newhashmap();
tagsByName.put(“a”,新的HashSet(Arrays.asList(“Hi”、“Hello”、“Hola”、“Bonjour”));
tagsByName.put(“b”,新的哈希集(Arrays.asList(“Hi”,“Hello”));
tagsByName.put(“c”,新的哈希集(Arrays.asList(“Hi”));
String searchText=“嗨,你好,你好”;
字符串[]tagsToFind=searchText.split(“,”);
Map matchCountsByEntryName=新HashMap();
Arrays.stream(tagsToFind)
.forEach(标记查找->{
对于(字符串入口名:tagsByName.keySet()){
Set tags=tagsByName.get(entryName);
if(tags.contains(trimToEmpty(tagToFind))){
compute(entryName,(k,v)->v==null?1:v+1);
}
}
});
List sortedEntries=matchCountsByEntryName.entrySet().stream()
.sorted((e1,e2)->e2.getValue().compareTo(e1.getValue())
.collect(Collectors.toList());
Map.Entry entryWithMostMatches=sortedentry.get(0);
System.out.printf(“要搜索的条目,”+
“条目\%s\”包含的匹配项最多(%d)。\n”,
entryWithMostMatches.getKey(),entryWithMostMatches.getValue());
}
}
船尾