摘要:實(shí)際需求中如何運(yùn)用實(shí)現(xiàn)過濾重復(fù)這里的重復(fù)不一定是某個(gè)字段一樣的,而且時(shí)間點(diǎn)不重疊這里需要校驗(yàn)不重疊的字段有員工所在部門,出發(fā)點(diǎn),目的點(diǎn),生效日期,失效日期,出發(fā)時(shí)點(diǎn),到達(dá)時(shí)點(diǎn),出發(fā)分鐘,到達(dá)分鐘是需要校驗(yàn)的對(duì)象。
在我們的印象中,Map k,v 映射,一對(duì)一的比較多,下面主要講一對(duì)多的關(guān)系映射,主要需求在于,需要把很多的,雜亂的數(shù)據(jù) 按照不同的類型進(jìn)行分類處理,A,B,C類型的數(shù)據(jù)進(jìn)行分類統(tǒng)計(jì)
比如,計(jì)算字母/單詞出現(xiàn)的次數(shù)
String str = "t/h/is/is/a/car/a/n/d/t/ha/t/is/tr/uck/a/nd/w/h/e/r/e/is/t/he/s/t/uffs";
String [] str = "t/h/is/is/a/car/a/n/d/t/ha/t/is/tr/uck/a/nd/w/h/e/r/e/is/t/he/s/t/uffs".split("/"); Mapmap = new HashMap (); for(String key:str){ //方法1 //檢查map里面是否存在這個(gè)單詞,如果不存在 說明之出現(xiàn)一次 // if(!map.containsKey(key)){ // map.put(key, 1); // }else{ // map.put(key, map.get(key)+1);//存在 就在前面的基礎(chǔ)上面+1 計(jì)算出現(xiàn)的次數(shù) // } //方法2 Integer value = map.get(key); if(value == null){ map.put(key, 1); }else{ map.put(key, value+1); } } Set keySet = map.keySet(); Iterator it = keySet.iterator(); while(it.hasNext()){ String key = it.next(); Integer value = map.get(key); System.out.println(key+"-->"+value); } }
當(dāng)然,這里的value也可以是對(duì)象,對(duì)于不同的場(chǎng)景 進(jìn)行靈活的應(yīng)用。
實(shí)際需求中如何運(yùn)用HashMap實(shí)現(xiàn)過濾重復(fù)
這里的重復(fù)不一定是某個(gè)字段一樣的,而且時(shí)間點(diǎn)不重疊 這里需要校驗(yàn)不重疊的字段 有 "員工所在部門","出發(fā)點(diǎn)","目的點(diǎn)","生效日期","失效日期","出發(fā)時(shí)點(diǎn)","到達(dá)時(shí)點(diǎn)","出發(fā)分鐘","到達(dá)分鐘"
entity 是需要校驗(yàn)的對(duì)象。
final Map> trfsVo = new HashMap > (); //判斷是否重復(fù) if (entity.getDeptCode()!=null && entity.getBeginDeptCode() != null && entity.getEndDeptCode()!=null && entity.getEffectiveDate() != null && entity.getInvalidDate() != null ) { //三個(gè)點(diǎn)重復(fù)就繼續(xù)判斷時(shí)間點(diǎn)是否重疊 String key = StringUtil.append(entity.getDeptCode(),"_",entity.getBeginDeptCode(),"_",entity.getEndDeptCode()); if (trfsVo.containsKey(key)) { List vo = trfsVo.get(key); Calendar now = Calendar.getInstance(); now.set(Calendar.HOUR_OF_DAY, entity.getBeginTimePoint()); now.set(Calendar.MINUTE, entity.getBeginTimeMinute()); long entity_start = now.getTime().getTime(); now.set(Calendar.HOUR_OF_DAY, entity.getEndTimePoint()); now.set(Calendar.MINUTE, entity.getEndTimeMinute()); long entity_end = now.getTime().getTime(); for(TransferNumUnitPrice vor:vo) { //entity代表原來excel里面的數(shù)據(jù),vor 從excel里面過濾處理出來有3個(gè)code重復(fù)的數(shù)據(jù) if ( //過濾生效日期在區(qū)間范圍重疊 (( (vor.getEffectiveDate().compareTo(entity.getEffectiveDate())!=1) && (vor.getInvalidDate().compareTo(entity.getEffectiveDate())!=-1) ) || //過濾失效日期在區(qū)間范圍重疊 ( (vor.getEffectiveDate().compareTo(entity.getInvalidDate())!=1) && (vor.getInvalidDate().compareTo(entity.getInvalidDate())!=-1) ) || //過濾包含重疊 ( (vor.getEffectiveDate().compareTo(entity.getEffectiveDate())==1) && (vor.getInvalidDate().compareTo(entity.getInvalidDate())==-1) )) ) { now.set(Calendar.HOUR_OF_DAY, vor.getBeginTimePoint()); now.set(Calendar.MINUTE, vor.getBeginTimeMinute()); long vorhm_start = now.getTime().getTime(); now.set(Calendar.HOUR_OF_DAY, vor.getEndTimePoint()); now.set(Calendar.MINUTE, vor.getEndTimeMinute()); long vorhm_end = now.getTime().getTime(); if ( //開始時(shí)間在區(qū)間范圍中 (vorhm_start <= entity_start && vorhm_end >= entity_start) || //結(jié)束時(shí)間在區(qū)間范圍中 (vorhm_start <= entity_end && vorhm_end >= entity_end) || // 時(shí)間包含 (vorhm_start > entity_start && vorhm_end < entity_end) ) { //加入異常 比較異常 vo.add(entity); throw new NovatarRuntimeException(getMessageSource().getMessage("transferNumUnitPrice.excelDataConflict")); } } } //加入需要比較的無異常的數(shù)據(jù) 進(jìn)行比較 vo.add(entity); } else { List vo = new LinkedList (); vo.add(entity); trfsVo.put(key, vo); }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/64832.html
摘要:最終能和面試官聊的開心愉快投緣的叫面霸。能夠與很好的集成提供映射標(biāo)簽,支持對(duì)象與數(shù)據(jù)庫的字段關(guān)系映射提供對(duì)象關(guān)系映射標(biāo)簽,支持對(duì)象關(guān)系組件維護(hù)。使用可以有效的防止注入,提高系統(tǒng)安全性。 showImg(https://segmentfault.com/img/bVbsSlt?w=358&h=269); 一、概述 面試,難還是不難?取決于面試者的底蘊(yùn)(氣場(chǎng)+技能)、心態(tài)和認(rèn)知及溝通技巧。...
摘要:相關(guān)閱讀通過項(xiàng)目逐步深入了解一通過項(xiàng)目逐步深入了解二通過項(xiàng)目逐步深入了解三本項(xiàng)目所有代碼及文檔都托管在地址延遲加載什么是延遲加載可以實(shí)現(xiàn)高級(jí)映射使用實(shí)現(xiàn)一對(duì)一及一對(duì)多映射,具備延遲加載功能。一級(jí)緩存是級(jí)別的緩存。 相關(guān)閱讀: 1、通過項(xiàng)目逐步深入了解Mybatis 2、通過項(xiàng)目逐步深入了解Mybatis 3、通過項(xiàng)目逐步深入了解Mybatis 本項(xiàng)目所有代碼及文檔都托管在 Github...
摘要:提供了專門的集合類用來存放這種對(duì)象關(guān)系的對(duì)象,即接口。中的集合,元素是成對(duì)存在的理解為夫妻。中的集合稱為單列集合,中的集合稱為雙列集合。根據(jù)指定的鍵,在集合中獲取對(duì)應(yīng)的值。 day04 【Map】 主要內(nèi)容 Map集合 教學(xué)目標(biāo) [ ] 能夠說出Map集合特點(diǎn) [ ] 使用Map集合添加方法保存數(shù)據(jù) [ ] 使用鍵找值的方式遍歷Map集合 [ ] 使用鍵值對(duì)的方式遍歷Map集合 [ ...
摘要:?jiǎn)尉€程集合本部分將重點(diǎn)介紹非線程安全集合。非線程安全集合框架的最新成員是自起推出的。這是標(biāo)準(zhǔn)的單線程陣營中唯一的有序集合。該功能能有效防止運(yùn)行時(shí)造型。檢查個(gè)集合之間不存在共同的元素。基于自然排序或找出集合中的最大或最小元素。 【編者按】本文作者為擁有十年金融軟件開發(fā)經(jīng)驗(yàn)的 Mikhail Vorontsov,文章主要概覽了所有標(biāo)準(zhǔn) Java 集合類型。文章系國內(nèi) ITOM 管理平臺(tái) O...
閱讀 2172·2021-11-02 14:48
閱讀 2826·2019-08-30 14:19
閱讀 3005·2019-08-30 13:19
閱讀 1362·2019-08-29 16:17
閱讀 3349·2019-08-26 14:05
閱讀 3053·2019-08-26 13:58
閱讀 3137·2019-08-23 18:10
閱讀 1170·2019-08-23 18:04