『壹』 【JAVA】【悬赏100】 有两个 list 我想把他 改为一个map 或者其他的
//得到keys
List<String>keys=newArrayList<String>();
//得到values
List<Integer>values=newArrayList<Integer>();
//Map
Map<String,Integer>rs=newLinkedHashMap<String,Integer>();
//确保以最小的list集合长度为rs的长度,防止NullPointerException异常
intkeyLen=(keys!=null)?keys.size():0;
intvalLen=(values!=null)?values.size():0;
intlen=keyLen
if(len>valLen)
Len=valLen;
//保存到Map中,for循环遍历效率较高
for(inti=0;i<len;i++)
rs.put(keys.get(i),values.get(i));
希望能帮助你
『贰』 java6 支持使用guava 工具类库 将list转换为map吗
假设有某个类如下
class Movie {
private Integer rank;
private String description;
public Movie(Integer rank, String description) {
super();
this.rank = rank;
this.description = description;
}
public Integer getRank() {
return rank;
『叁』 java里有没有专门判断List里有重复的数据
没有专门判断的,可以自己写一段代码
『肆』 如何再java钟使用guava
以前这么用:
Java代码
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
现在Guava这么用:
Java代码
ImmutableList<String> of = ImmutableList.of("a", "b", "c", "d");
ImmutableMap<String,String> map = ImmutableMap.of("key1", "value1", "key2", "value2");
文本文件读取现在Guava这么用
Java代码
File file = new File(getClass().getResource("/test.txt").getFile());
List<String> lines = null;
try {
lines = Files.readLines(file, Charsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
『伍』 guava Splitter 与java 内置的string的split 方法有什么区别
java内置的会忽略空内容,guava Splitter不会忽略,也可以自由选择的忽略。
guava Splitter 可以通过splitToList方法直接将结果转成List。
『陆』 使用Guava将3个List<Map>集合根据某个字段合并成一个List<Map>集合
通过 java8 的流式编程,实现还是比较简单的。
代码比较多,已上传附件。
『柒』 int数组转化成List<Integer>有没有更简便的方法
1、建立Test的java测试类并写出main方法用来测试。
『捌』 java.util.stream map和flatmap的区别
Stream是元素的集合,这点让Stream看起来用些类似Iterator;
可以支持顺序和并行的对原Stream进行汇聚的操作;
大家可以把Stream当成一个高级版本的Iterator。原始版本的Iterator,用户只能一个一个的遍历元素并对其执行某些操作;高级版本的Stream,用户只要给出需要对其包含的元素执行什么操作,比如“过滤掉长度大于10的字符串”、“获取每个字符串的首字母”等,具体这些操作如何应用到每个元素上,就给Stream就好了!(这个秘籍,一般人我不告诉他:))大家看完这些可能对Stream还没有一个直观的认识,莫急,咱们来段代码。
//Lists是Guava中的一个工具类
List<Integer> nums = Lists.newArrayList(1,null,3,4,null,6);
nums.stream().filter(num -> num != null).count();
上面这段代码是获取一个List中,元素不为null的个数。这段代码虽然很简短,但是却是一个很好的入门级别的例子来体现如何使用Stream,正所谓“麻雀虽小五脏俱全”。我们现在开始深入解刨这个例子,完成以后你可能可以基本掌握Stream的用法!
1.1 剖析Stream通用语法
图片就是对于Stream例子的一个解析,可以很清楚的看见:原本一条语句被三种颜色的框分割成了三个部分。红色框中的语句是一个Stream的生命开始的地方,负责创建一个Stream实例;绿色框中的语句是赋予Stream灵魂的地方,把一个Stream转换成另外一个Stream,红框的语句生成的是一个包含所有nums变量的Stream,进过绿框的filter方法以后,重新生成了一个过滤掉原nums列表所有null以后的Stream;蓝色框中的语句是丰收的地方,把Stream的里面包含的内容按照某种算法来汇聚成一个值,例子中是获取Stream中包含的元素个数。如果这样解析以后,还不理解,那就只能动用“核武器”–图形化,一图抵千言!
『玖』 判断一个list集合是否为空,用isEmpty 还是 null的问题
isEmpty() 和(list.size() == 0)都是判断List内容是否为空。
null判断是判断有没有对list集合分配内存空间,而不是list里面内容是否为空。
比如,new一个user对象,判断user的list内容是否为空,出现异常。这是因为,使用isEmpty()和size()的前提是,list是一个空集合,而不是null,否则会抛异常。
所有在判断集合不为空的时候常采用:
if(list!=null && !list.isEmpty()){
//不为空的情况
}else{
//为空的情况
}
(9)guavalist过滤扩展阅读:
List集合的常用方法:
1、voidadd(intindex,Eelement)
在指定位置插入元素,后面的元素都往后移一个元素。
2、booleanaddAll(intindex,Collection<? extendsE>c)
在指定的位置中插入c集合全部的元素,如果集合发生改变,则返回true,否则返回false。
意思就是当插入的集合c没有元素,那么就返回false,如果集合c有元素,插入成功,那么就返回true。
3、Eget(intindex)
返回list集合中指定索引位置的元素。
4、intindexOf(Objecto)
返回list集合中第一次出现o对象的索引位置,如果list集合中没有o对象,那么就返回-1。
5、Eremove(intindex)
删除指定索引的对象。
6、Eset(intindex,Eelement)
在索引为index位置的元素更改为element元素。
『拾』 Google guava和Apache commons哪个好
Guava 的 FAQ 部分有专门解答:
Why did Google build all this, when it could have tried to improve the Apache Commons Collections instead?
The Apache Commons Collections very clearly did not meet our needs. It does not use generics, which is a problem for us as we hate to get compilation warnings from our code. It has also been in a "holding pattern" for a long time. We could see that it would require a pretty major investment from us to fix it up until we were happy to use it, and in the meantime, our own library was already growing organically.
An important difference between the Apache library and ours is that our collections very faithfully adhere to the contracts specified by the JDK interfaces they implement. If you review the Apache documentation, you'll find countless examples of violations. They deserve credit for pointing these out so clearly, but still, deviating from standard collection behavior is risky! You must be careful what you do with such a collection; bugs are always just waiting to happen.
Our collections are fully generified and never violate their contracts (with isolated exceptions, where JDK implementations have set a strong precedent for acceptable violations). This means you can pass one of our collections to any method that expects a Collection and feel pretty confident that things will work exactly as they should.
简单地说:
Apache Commons Collections 3.x 不支持泛型,Guava 支持
Guava 实现了 JDK 的标准接口,而 Apache Commons Collections 3.x 有很多违反标准的地方
Apache Commons Collections 4.x 的发行注记如下:
Majorchangessince3.2.1
(varargs,Iterable)
Removeddeprecatedclasses/
.util.Queue
/Get(seealsopackagesplitmap)
从 4.x 开始,Apache Commons Collections 开始使用 JDK 5 的特性(包括泛型),此外也去除、添加了很多内容
各有千秋, 我主要使用Apache Commons ,辅助使用 Google guava
但是细节方法上还是有区别的, 比如
GoogleGuava Splitter 对比 Apache StringUtils
apache commons的StringUtils提供的常用功能介绍,但是google的guava也提供了一些字符串处理的常见功能,所以,将对两者的字符串分割函数做一次比较详细的对比(结果比较surprise)。
区别
首先看基本的使用方法:
//ApacheStringUtils...
String[]tokens1=StringUtils.split("one,two,three",',');
//GoogleGuavasplitter...
Iteratable<String>tokens2=Splitter.on(','),split("one,two,three");
google提供的方法更加的面向对象一点,因为它要先创建一个Splitter对象,然后使用它来分割字符串,而apache的方法则有点函数式编程的味道,它的方法都是静态的。
这里我更加倾向于采用google的splitter,因为这个对象是可以重用的,且可以在其上附加更多的功能,比如trim,去掉空的元素等,一切都很简单。
SplitterniceCommaSplitter=Splitter.on(',').omitEmptyString().trimResults();
niceCommaSplitter.split("one,,two,three");//"one","two","three"
niceCommaSplitter.split("four,five");//"four","five"
看起来有点用,还有其他区别么?
另外一个需要注意的地方就是Splitter返回的是Iteratable<String>,而StringUtils.split返回的是一个String数组。
大部分使用分隔符的情况是我们需要对字符串按照分隔符进行遍历处理,仅此而已。
下面就是常用的代码性能对比的例子:
finalStringnumberList="One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten";
longstart=System.currentTimeMillis();
for(inti=0;i<1000000;i++){
StringUtils.split(numberList,',');
}
System.out.println(System.currentTimeMillis()-start);
start=System.currentTimeMillis();
for(inti=0;i<1000000;i++){
Splitter.on(',').split(numberList);
}
System.out.println(System.currentTimeMillis()-start);
代码很简单,就是都对同一个字符串进行100万次的分隔操作,看看时间上的区别,结果如下:
983
165
很明显,guava的速度快很多,这个程序如果运行在每天处理大量字符串的服务中,那么性能差异更加明显。我想其中的原因是Splitter返回的是Iterable<String>,而StringUtils.split返回的是一个String[],需要创建新的String对象,导致耗时增加。
如果我们对Splitter对象缓存,那么速度提高更多:
start=System.currentTimeMillis();
Splitters=Splitter.on(',');
for(inti=0;i<1000000;i++){
s.split(numberList);
}
System.out.println(System.currentTimeMillis()-start);
结果为12,神奇吧,呵呵