List<Map<String, String>> 二分查找

public class TodayDeal {

	static void main(String[] args) {
		Map<String, String> map = new HashMap<>()
		map.put("code","1")
		Map<String, String> map1 = new HashMap<>()
		map1.put("code","2")
		Map<String, String> map2 = new HashMap<>()
		map2.put("code","3")
        List<Map<String, String>> list = new ArrayList<>()
		list.add(map)
		list.add(map1)
		list.add(map2)
        boolean a = getValue(list, 0, list.size() - 1, "2")
	}
	//list里面的内容从小到大排序,返回true表示存在,false表示不存在,map
	static boolean getValue(List<Map<String, String>> list, int start, int end, String value) {
		int middle = ((int) ((end - start) % 2 == 1 ? ((end - start) / 2 + 1) : (end - start) / 2)) + start
		String com = list.get(middle).get("code")
		if (com == value) {
			return true
		} else if (value < com && start != end) {
			return getBondValue(list, start, middle - 1 >= start ? middle - 1 : middle, value)
		} else if (value > com && start != end) {
			return getBondValue(list, middle + 1 <= end ? middle + 1 : middle, end, value)
		}
		return false
	}
	//list里面的内容从小到大排序,返回true表示存在,false表示不存在,String
	static boolean getValue(List<String> list, int start, int end, String value) {
		int middle = ((int) ((end - start) % 2 == 1 ? ((end - start) / 2 + 1) : (end - start) / 2)) + start
		String com = list.get(middle)
		if (com == value) {
			return true
		} else if (value < com && start != end) {
			return getBondValue(list, start, middle - 1 >= start ? middle - 1 : middle, value)
		} else if (value > com && start != end) {
			return getBondValue(list, middle + 1 <= end ? middle + 1 : middle, end, value)
		}
		return false
	}
}
posted @ 2021-06-18 09:22  EH点点滴滴  阅读(228)  评论(0编辑  收藏  举报