JAVA使用原生思维逻辑 实现简单模糊查询

需求如下图所示:

Created with Raphaël 2.2.0 用户输入请求 搜索数据 是否是包含数字或者字母? 是否是纯数字? 根据数字查询 搜索结果并且展示 是否是纯字母? 汉字转拼音 根据字母模糊查询 是否包含特殊字符或者汉字? 是否是纯汉字? 返回默认查询所有(不规范传值) yes no yes no yes no yes no yes no

因为用户输入的可能性太多太多 , 小编在这里模拟出简单逻辑业务(复杂逻辑业务还得以后再慢慢补充。。)这里需要提到一点关于汉字转拼音的工具类。

<!-- https://mvnrepository.com/artifact/com.github.open-android/pinyin4j -->
<dependency>
    <groupId>com.github.open-android</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.0</version>
</dependency>

关于简单模糊查询难点就在判断,在JAVA代码中,使用正则表达式判断是最好的,比如说定义一个判断是否包含数字的正则表达式。

//模拟用户输入关键词
String keyword = "xx";
		 if ("".equals(keyWord)) {
    
    
            //初始化
            initAll();
        
        } else {
    
    
	String num_reg = ".*[0-9].*"; //判断是否包含数字
	String char_reg= ".*[a-zA-Z].*"; //判断是否包含字母
	// 编译正则表达式
   boolean cons_num = testReg(num_reg ,keyword); //false
 	  boolean cons_char = testReg(char_reg,keyword);//true

			//判断是否满足数字和字母
				if(cons_num  & cons_char ){
    
    
				//默认查所有
				
				}else{
    
    
				    //如果只有数字
					if(cons_num  )
					{
    
    
					//按数字查
					searchByNoKeyWord(keyWord);
					...
					}
					//如果只有字母
 					if(cons_char)	
 					{
    
    
					//判断商品汉字中是否包含keyword,查询并且标记
					       //重新定义  dataBeanList ==  》 存储所有的数据集合
                    List<A6User.DataBean> dataBeanies = new ArrayList<>();
                    for (A6User.DataBean dataBean : dataBeanList) {
    
    
                        dataBeanies.add(dataBean);
                    }

                    for (A6User.DataBean dataBeans : dataBeanies) {
    
    
                        HanyuPinyinOutputFormat fmt = new HanyuPinyinOutputFormat();
                        String pingying = null;
                        try {
    
    
                        //转拼音
                            pingying = PinyinHelper.toHanyuPinyinString(dataBeans.getPeopleName(), fmt, "");
                        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
    
    
                            badHanyuPinyinOutputFormatCombination.printStackTrace();
                        }
                        if (pingying.contains(keyWord)) {
    
    
                            dataBeans.setPingYingName(pingying);
                            dataBeans.setPingYing(true);
                        }
                    }
					}
					//如果不满足两个条件
					if(!num_reg  & !char_reg)
					{
    
    
						//判断是否是纯汉字
						String alphe_reg = "[\u4e00-\u9fa5]";
						boolean isOnlyAlphe = testReg(alphe_reg,keyword); 
						if(isOnlyAlphe )
						{
    
    
						//按汉字查询
						searchByNoKeyWord(keyWord);
						}
						else
						{
    
    
						//说明非法输入 -- > 默认查所有
						}				
					}
				}
		

		public boolean testReg(String reg , String keyword){
    
    
			return Pattern.compile(reg).matcher(keyword).matches();
		}

根据是否是纯数字,是否是纯字母,是否是纯汉字进行查询的方法 searchByNoKeyWord(keyWord);

    public void searchByNoKeyWord(String keyWord) {
    
    
        List<Map<String, String>> mapArrayList = new ArrayList<>();
        //按关键字查  maps = > 全局数据
        for (int i = 0; i < maps.size(); i++) {
    
    
            boolean isExists = false;
            Set<String> keyset = maps.get(i).keySet();
            //判断是否存在
            for (String s : keyset) {
    
    
                String as = maps.get(i).get(s);
                if (as.contains(keyWord)) {
    
    
                    isExists = true;
                    break;
                }
            }
            if (isExists) {
    
    
                Map map = new HashMap();
                for (String s : keyset) {
    
    
                //将键值对全部封装到map中
                    map.put(s, maps.get(i).get(s));
                }
                mapArrayList.add(map);
            }
        }
    }

简单模糊查询实际上还是得从业务的角度出发,能使实线的过程变得更灵活和方便

猜你喜欢

转载自blog.csdn.net/weixin_43409994/article/details/109702578