闲话篇
经过差不多一年半的开发和学习,在学习、工作过程中遇到了许多问题。也踩过很多坑,同时自己也成长了许多。积跬步以至千里,积怠惰以致深渊。成长的路是漫长的,就像爬山,下山容易上山难。登高而好远,恋低而目短。任何事物都是以小聚多,士兵突击里面有句话:"我看他执拧得像个傻子,对待任何一件事情都好像是最后一根救命稻草一样紧紧抓着,等我手里还只是一把稻草的时候,他怀抱的已经是一棵让我仰望的参天大树 了"。回顾世界历史 知兴衰,回顾自己历史 知得失。世界历史我们只是汪洋大海中的一粟,对于我们只可远 观不可亵玩。但是对于我们自己的历史,我们还是可以做到亡羊补牢犹未迟也。
最近的1年多的时间工作学习,收获和付出是成正比的,随着年龄的增长,各种看的见看不见的烦恼会接踵而来,让你防不胜防。虽没有达到心力憔悴之地,但是引发的感冒浪潮已经是一波接一波了。随着年龄的增长,除了肚子快多了一个游泳圈,头发越剪越短似乎也没有什么了。这是不是一个悲哀的故事。今日重新"操刀"原因有二。其一:对于有形无形的烦恼的一种反抗,虽不知在各种烦恼的淫威之下,能否做到 酒肉穿肠过佛祖心中留(这里好像有点不妥 但是管他的呢)。 其二 心中其实一直有一个不甘平凡的声音在缭绕,这也算是对他的一个回应吧。
读到这里的小伙伴可能已经有点不耐烦了。哈哈!谁没有一颗骚动的心呢!回归正题。
正题篇
-
逆波兰表达式
逆波兰表达式图解.png
如果有这样一个表达式(1+4-(3+2))*4-3/2+2^3-2^(1+1)
如果我们学过数学我们可以很快的计算出来这个最终的结果是=2.5。但是我们如何让计算机去计算这个表达式呢? 这也就衍生出了 逆波兰表达式
- java代码实现
ReversePolishNotationNew类:
- java代码实现
package reversePolishNotation;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
/**
* 逆波兰表达式-改进版
*/
public class ReversePolishNotationNew {
//定义操作符的优先等级 用来区分是先执行还是后执行
private static LinkedList operationList = new LinkedList<>();//运算操作队列
private static LinkedList arithmeticRecorder = new LinkedList<>();//运算记录器
private static final Map<String, Integer> PriorityMap = new HashMap<String, Integer>();
private static LinkedList resultList = new LinkedList<>();
private static String tmp = "";
static {
PriorityMap.put("+",Priority.PLUS.getValue());
PriorityMap.put("-",Priority.REDUCE.getValue());
PriorityMap.put("*",Priority.MULTIPLICATION.getValue());
PriorityMap.put("/",Priority.EXCEPT.getValue());
PriorityMap.put("^",Priority.POWER.getValue());
PriorityMap.put("%",Priority.DIVIDE_EXACTLY.getValue());
}
// 解析 运算符 拆分对应的运算串
/**
* 解析运算符
* @param opers
*/
public static void analyticOperator(String opers){
//将对应的字符串中空格去掉
String opersNew = trimStrs(opers);
String[] splits = opersNew.split("");// 拆分
for (String s : splits){
if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/")||s.equals("%")||s.equals("^")){
if(!"".equals(tmp) && null != tmp)
operationList.add(tmp);
tmp = "";
priorityCompare(s);
}else if(s.equals("(")){
if(!"".equals(tmp) && null != tmp)
operationList.add(tmp);
tmp = "";
arithmeticRecorder.add(s);
}else if(s.equals(")")){
if(!"".equals(tmp) && null != tmp)
operationList.add(tmp);
tmp = "";
while (!"(".equals(arithmeticRecorder.peekLast())){
operationList.add(arithmeticRecorder.pollLast());
}
arithmeticRecorder.removeLast();
}else{
tmp+=s;
}
}
if(!"".equals(tmp) && null != tmp)
operationList.add(tmp);
tmp = "";
while (null != arithmeticRecorder.peekLast()){
operationList.add(arithmeticRecorder.pollLast());
}
}
/**
* 优先级的比较
* @param s
*/
public static void priorityCompare(String s){
if(opertionToVal(s) > opertionToVal((String) arithmeticRecorder.peekLast()) ){
arithmeticRecorder.add(s);
}else{
String str = (String) arithmeticRecorder.pollLast();
if(null != str && !"".equals(str) ){
operationList.add(str);
}
priorityCompare(s);
}
}
/**
* 运算符转对应的值
* @param s
* @return
*/
public static Integer opertionToVal(String s){
return PriorityMap.get(s) != null ? PriorityMap.get(s) : 0;
}
public static void calculationResults(){
for(int i=0;i<operationList.size();i++){
if(operationList.get(i).equals("+") || operationList.get(i).equals("-") || operationList.get(i).equals("*") || operationList.get(i).equals("/")
|| operationList.get(i).equals("%") || operationList.get(i).equals("^")){
Double d1 = 0.0;
Double d2 = 0.0;
if(resultList.peekLast() instanceof Double){
d1 = (Double) resultList.pollLast();
}else{
d1 = Double.parseDouble((String) resultList.pollLast());
}
if(resultList.peekLast() instanceof Double){
d2 = (Double) resultList.pollLast();
}else{
d2 = Double.parseDouble((String) resultList.pollLast());
}
Double operation = operation(d2, d1, (String) operationList.get(i));
resultList.add(operation);
}else{
resultList.add(operationList.get(i));
}
}
}
public static Double operation(Double d1, Double d2, String s) {
Double dou = 0.0;
switch (s) {
case "+":
dou = d1 + d2;
break;
case "-":
dou = d1 - d2;
break;
case "*":
dou = d1 * d2;
break;
case "/":
dou = d1 / d2;
break;
case "^":
dou = Math.pow(d1, d2);
break;
case "%":
dou = d1 % d2;
break;
}
return dou;
}
public static String trimStrs(String strs){
return strs.replaceAll(" +","");
}
public static void main(String[] args) {
String str = "(1+4-(3+2))*4-3/2+2^3-2^(1+1)";
analyticOperator(str);
calculationResults();
}
}
运算符优先级类Priority
package reversePolishNotation;
/**
* 运算优先级
*
*/
public enum Priority {
PLUS(1),//加运算优先级
REDUCE(1),//减运算优先级
MULTIPLICATION(2),//乘运算优先级
EXCEPT(2),//除法运算优先级
POWER(3),//幂运算
DIVIDE_EXACTLY(2);//整除
private int value;
private Priority(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
-MapToJson
不知道大家有没有遇到这样的需求,将一个多层的map结构转化成单层的数据结构但是 数据的描述不改变。
Map多层转单层数据结构.png
java实现
{
"a":{"a1":"1","a2":"2"},
"b":[{"b1":"1","b2":"2"},{"b1":"3","b2":"4"}],
"c":"c1"
}
转换成=>
{
"a.a1":"1",
"a.a2":"2",
"b[0].b1":"1",
"b[0].b2":"2",
"b[1].b1":"3",
"b[1].b2":"4",
"c":"c1"
}
package json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MapToJson {
/**
* 递归 解析map 成对应key-value
* @param map
* @param returnMap
* @param keys
*/
public static void AnalysisMap(Map<String,Object> map, Map<String,String> returnMap, String keys){
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object ob = entry.getValue();
if(ob instanceof String){
if(!"".equalsIgnoreCase(keys) && null != keys){
returnMap.put(keys+"."+entry.getKey(),entry.getValue().toString());
}else {
returnMap.put(entry.getKey(), entry.getValue().toString());
}
}else if(ob instanceof Map){
if(!"".equalsIgnoreCase(keys) && null != keys){
AnalysisMap((Map<String, Object>) ob,returnMap,keys+"."+entry.getKey());
}else {
AnalysisMap((Map<String, Object>) ob,returnMap,entry.getKey());
}
}else if(ob instanceof List){
List<?> listTmp = (List<?>) ob;
for(int i=0;i<listTmp.size();i++){
if(!"".equalsIgnoreCase(keys) && null != keys){
AnalysisMap((Map<String, Object>) listTmp.get(i),returnMap,keys+"."+entry.getKey()+"["+i+"]");
}else {
AnalysisMap((Map<String, Object>) listTmp.get(i),returnMap,entry.getKey()+"["+i+"]");
}
}
}
}
}
public static void main(String[] args) {
Map<String,Object> parMaps = new HashMap<>();
Map<String,Object> parMapsFood = new HashMap<>();
parMapsFood.put("Grain","wheat");
parMapsFood.put("corn","rice");
Map<String,Object> parMapsFood2 = new HashMap<>();
parMapsFood2.put("Grain2","wheat2");
parMapsFood2.put("corn2","rice2");
List<Map<String,Object>> listMaps = new ArrayList<>();
listMaps.add(parMapsFood);
listMaps.add(parMapsFood2);
parMaps.put("Food",listMaps);
parMaps.put("Fruit","apple");
parMaps.put("Car","mercedes");
Map<String,String> returnMaps = new HashMap<>();
AnalysisMap(parMaps,returnMaps,null);
System.out.println(returnMaps.toString());
}
}
返回结果示意:
MapToJson.png
- Json key获取多层数据结构对应的值
这个在以前的文章中有对应的解析可以移步到 Json对象解析
网友评论