IDEA 将项目中的单个文件进行打包并添加依赖
Java对类进行打包使其变成可执行的jar包是完成一个项目最基本的操作。 平时需要打jar包的时候,更多的都是对整个项目打包,并且利用maven的插件可以自动添加依赖。 但有些时候需要对一个包含main方法的类进行打包,并且添加相关的依赖。之前查了很久该怎么操作,但网上大部分都是对整个项目进行打包的教程,几乎没有对单个类打包的教程。摸索了大半天偶然发现了maven项目对单个类打包的操作。
Java对类进行打包使其变成可执行的jar包是完成一个项目最基本的操作。 平时需要打jar包的时候,更多的都是对整个项目打包,并且利用maven的插件可以自动添加依赖。 但有些时候需要对一个包含main方法的类进行打包,并且添加相关的依赖。之前查了很久该怎么操作,但网上大部分都是对整个项目进行打包的教程,几乎没有对单个类打包的教程。摸索了大半天偶然发现了maven项目对单个类打包的操作。
在平时的学习和写代码过程中,经常有些特别小的点,虽然很简单,但是容易忘记或者弄错,在这里记录一下,随时备案。
Java中,字符串是一种引用数据类型,因此不能直接用”==”来做字符串是否相等的匹配,而应该用”.equals”来做相等的比较。
方法一 使用Entry进行键值都需要时的遍历
Map<Integer,Integer map = new Hashmap<Integer,Integer();
for(Map.Entry<Integer,Integer entry:map.entrySet()){
//获取键
entry.getKey();
//获取值
entry.getValue();
}
方法二 在for-each循环中遍历keys或values
Map<Integer, Integer map = new HashMap<Integer, Integer();
//遍历map中的键
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
//遍历map中的值
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
方法三 使用Iterator进行遍历 使用泛型:
Map<Integer, Integer map = new HashMap<Integer, Integer();
Iterator<Map.Entry<Integer, Integer entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
不使用泛型:
Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}
参考链接:https://www.cnblogs.com/fqfanqi/p/6187085.html
获得本类的位置:
URL base = this.getClass().getResource("");
如:/home/popeye/testjava/build/classes/net/
根据本类位置获取别的文件的位置
String path = new File(base.getFile(),"../../../"+name).getCanonicalPath();
得到:/home/popeye/testjava/name
JAVA使用MongoDB
MongoClient mongoClient = new MongoClient("localhost",27017);//连接客户端
MongoDatabase mongoDatabase = mongoClient.getDatabase("");//连接某一数据库
MongoCollection<DBObject collection = mongoDatabase.getCollection("",DBObject.class);//连接某一个集合
集合的遍历 ① collection.find():返回集合中的所有文档(DBObject)
MongoCollection<DBObject collection = mongoDatabase.getCollection("",DBObject.class);
for(DBObject dbObject:colletion.find()){
String str = dbObject.toString();
}
从MongoDB中获取文档内容,并进行接下来的一系列操作的基本思路是:
①取出DBObject对象;
②对DBObject对象进行操作。
!!!如果json中有list,转换成BasicDBList进行操作