黑马程序员-传统集合

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------

传统集合存在的问题:

1、多线程操作时的同步问题

2、传统集合在迭代过程中不能对集合进行增删操作,利用java5的线程并发库可以解决上述问题。

 

示例代码:

Person

public class Person {

	private String name;
	private int age;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
}

TraditionCollectionTest

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class TraditionCollectionTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<Person> list = new ArrayList<Person>();
		list.add(new Person("zhangsan", 13));
		list.add(new Person("lisi", 15));
		list.add(new Person("zhaoliu", 12));
		Iterator it = list.iterator();
		while (it.hasNext()) {
			Person person = (Person) it.next();
			if (person.getName() == "zhaoliu") {
				list.remove(person);
			} else {
				System.out.println(person.toString());
			}
		}

	}

}

输出结果:

 

现在我们修改:

让他等于李四的时候删除

输出结果:

 

再次修改代码:

让他等于张三的时候删除:

 

根据上面三次的运行结果 ,分析代码:

(为什么会出现上述情况)

查看源代码:

 

ArrayList类中,有这个方法,而ArrayListIterator是其内部类。

@Override public Iterator<E> iterator() {

        return new ArrayListIterator();

ArrayList内部有一个内部类(ArrayListIterator),这个内部类维持了一个变量

 private int expectedModCount = modCount;

在这个内部类的next方法中:

  if (ourList.modCount != expectedModCount) {

                throw new ConcurrentModificationException();

            }

modCount的值会在增加,删除时都会++

就是说在我们程序中 调用list.iterator()时,会创建ArrayListIterator,由于之前我们add了三次,所以expectedModCount会赋值为3,当我们remove时,modCount会变为4,再执行next方法时就会抛异常。

 


---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ---------------------

### Spring Security入门案例与黑马程序员教程 Spring Security 是一个强大的安全框架,用于保护基于 Java 的应用程序。它提供了认证、授权和防止常见攻击的功能。以下是一个简单的入门案例,结合了 Spring Boot 和 Spring Security 的基本配置。 #### 1. 引入依赖 在 `pom.xml` 文件中添加 Spring Security 的启动器依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 引入此依赖后,Spring Security 将自动为项目提供基础的安全保护[^2]。 #### 2. 配置安全规则 创建一个配置类来定义访问规则。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 允许所有用户访问 /public 路径 .anyRequest().authenticated() // 其他路径需要认证 .and() .formLogin() .loginPage("/login") // 自定义登录页面 .permitAll() .and() .logout() .permitAll(); } } ``` 上述代码定义了访问规则:`/public/**` 路径对所有用户开放,而其他路径需要经过身份验证才能访问[^2]。 #### 3. 创建用户认证信息 可以通过内存中的用户数据进行简单认证。例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class AuthConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password(passwordEncoder().encode("password")).roles("USER") .and() .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN"); } } ``` 这里定义了两个用户:`user` 和 `admin`,分别拥有不同的角色权限[^2]。 #### 4. 创建控制器 为了测试安全规则,可以创建一个简单的控制器: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/public/hello") public String publicHello() { return "Hello, this is a public resource!"; } @GetMapping("/private/hello") public String privateHello() { return "Hello, this is a private resource!"; } } ``` 访问 `/public/hello` 不需要认证,而访问 `/private/hello` 则需要认证。 #### 5. 测试应用 运行 Spring Boot 应用程序后,尝试访问以下 URL: - `/public/hello`:可以直接访问。 - `/private/hello`:将被重定向到登录页面,输入用户名和密码后可以访问。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值