java基础

1.比较两个集合是否相等

/**
	 * 比较两个集合是否相等
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static <T extends Comparable<T>> boolean compare(List<T> a, List<T> b) {
		if (a.size() != b.size())
			return false;
		Collections.sort(a);
		Collections.sort(b);
		for (int i = 0; i < a.size(); i++) {
			if (!a.get(i).equals(b.get(i)))
				return false;
		}
		return true;
	}

2.break和continue的区别

a.continue只终止本次循环
b.break是终止当前for循环(如果有两个for循环,内循环的break是只跳出内循环的for循环)

3.判断集合中的元素是否都相等

//判断idsB中两个元素是否相等
			List<List<String>> idsB=new ArrayList<List<String>>();
			List<String> id1=new ArrayList<String>();
			id1.add("1");
			id1.add("2");
			idsB.add(id1);
			
			List<String> id2=new ArrayList<String>();
			id2.add("2");
			id2.add("1");
			idsB.add(id2);
			
			Set s = new HashSet(idsB);

			System.out.println(s.size());
			//输出为2
			//可以看出这两个元素不等,所以虽然两个子集合中元素大小相等,里面的值也一样,但是顺序不一样,导致这两个不是一个元素
			
			List<String> id3=new ArrayList<String>();

			Set s1 = new HashSet(id3);
			System.out.println(s1.size());
			//输出为0
			//如果id3为空,则s1里没有元素

4.声明一个Stirng的集合

List<String> msg = Lists.newArrayList();

5.set集合的三种遍历方式

  • (1).迭代遍历:

Set<String> set = new HashSet<String>(); Iterator<String> it = set.iterator(); while (it.hasNext()) { String str = it.next(); System.out.println(str); }

  • (2).for循环遍历: for (String str : set) { System.out.println(str); }
  • (3)优点还体现在泛型 假如 set中存放的是Object

Set<Object> set = new HashSet<Object>(); for循环遍历: for (Object obj: set) { if(obj instanceof Integer){ int aa= (Integer)obj; }else if(obj instanceof String){ String aa = (String)obj } ........ }

6.一个类中有一个静态方法,需要在类中使用一个变量时;需要声明一个实体类;

  • eg
@Component//声明一个bean类
public class ApplicationProperties {

    @Value("${socket.connect.timeout:15000}")
    private Integer connectTimeout;

    @Value("${socket.request.timeout:1000}")
    private Integer requestTimeout;

    @Value("${socket.read.timeout:60000}")
    private Integer readTimeout;

    public Integer getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public Integer getRequestTimeout() {
        return requestTimeout;
    }

    public void setRequestTimeout(Integer requestTimeout) {
        this.requestTimeout = requestTimeout;
    }

    public Integer getReadTimeout() {
        return readTimeout;
    }

    public void setReadTimeout(Integer readTimeout) {
        this.readTimeout = readTimeout;
    }
}
@Component

public class test{
private static void test(){
ApplicationProperties applicationProperties=new ApplicationProperties();
Integer applicationPropertiesConnectTimeout= applicationProperties.getConnectTimeout();
int a=applicationPropertiesConnectTimeout;
}
}

7.工具类的使用

//传过来的值topNum若为空,则赋值10
int num = Optional.fromNullable(topNum).or(10);
//工具类的学习
Assert.notNull(CurrentInvokingHolder.getInstance().getCurrentInvoking(),
"获取不到登录信息");

 

 

作者:guanbin —— 纵码万里千山

出处:https://www.cnblogs.com/guanbin-529/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/guanbin-529/p/6501625.html