spring中List,Set,Map集合的输出(详解)

简介: spring中List,Set,Map集合的输出(详解)

注入依赖:

注入:bean的所有属性都由容器来注入

依赖:bean的所有配置都离不开容器

附:学习spring可以直接看文档

基于setter配置

1,使用list将集合输出(需要的jar包上一章有),主要区分value和ref的使用区别

value:即字面量属性

ref:非字面量属性

接下来看代码,新建两个javabean,建在pojo包下

代码分别为

Student.java

package pojo;
public class Student {
    private String name;
    private Integer id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "name=" + name + "," + "id=" + id;
    }
}

UserQaq

package pojo;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class UserQaq {
    private String name;
    private Integer age;
    private List<Student> list1;
    private List<Integer> list2;
    private Set<Student> set1;
    private Set<String> set2;
    private Map<Integer,String> map1;
    public Map<String, Student> getMap2() {
        return map2;
    }
    public void setMap2(Map<String, Student> map2) {
        this.map2 = map2;
    }
    private Map<String,Student> map2;
    public Map<Integer, String> getMap1() {
        return map1;
    }
    public void setMap1(Map<Integer, String> map1) {
        this.map1 = map1;
    }
    public Set<Student> getSet1() {
        return set1;
    }
    public void setSet1(Set<Student> set1) {
        this.set1 = set1;
    }
    public Set<String> getSet2() {
        return set2;
    }
    public void setSet2(Set<String> set2) {
        this.set2 = set2;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public List<Student> getList1() {
        return list1;
    }
    public void setList1(List<Student> list1) {
        this.list1 = list1;
    }
    public List<Integer> getList2() {
        return list2;
    }
    public void setList2(List<Integer> list2) {
        this.list2 = list2;
    }
    @Override
    public String toString() {
        return "name = " + name + "\n" + "age=" + age + "\n" + "list1 = " + list1 + "\n" + "list2" + list2
                + "\n" + "set1=" + set1 + "\n" + "set2" + set2 + "\n" + "map1=" + map1 + "\n" + "map2" + map2;
    }
}

在容器中配置xml文件,新建一个applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userQaq" class="pojo.UserQaq">
        <property name="name" value="蔡康程"></property>
        <property name="age" value="21"></property>
        <property name="list1">
            <!--非字面量属性ref,如此属性,不能写成字符串的属性-->
            <list>
                <ref bean="student1"></ref>
                <ref bean="student2"></ref>
                <ref bean="student3"></ref>
                <ref bean="student4"></ref>
                <ref bean="student5"></ref>
            </list>
        </property>
        <property name="list2">
            <list>
                <!--字面量属性使用value-->
                <value>1234</value>
                <value>2345</value>
                <value>3456</value>
                <value>4567</value>
            </list>
        </property>
        <property name="set1">
            <set>
                <ref bean="student1"></ref>
                <ref bean="student2"></ref>
                <ref bean="student3"></ref>
                <ref bean="student4"></ref>
                <ref bean="student5"></ref>
            </set>
        </property>
        <property name="set2">
            <set>
                <value>qaq</value>
                <value>qqq</value>
                <value>qwq</value>
                <value>qeq</value>
                <value>qrq</value>
            </set>
        </property>
        <property name="map1">
            <map>
                <entry key="123" value="红楼梦"></entry>
                <entry key="456" value="三国演义"></entry>
            </map>
        </property>
        <property name="map2">
            <map>
                <entry key="123" value-ref="student1"></entry>
                <entry key="456" value-ref="student2"></entry>
                <entry key="123" value-ref="student3"></entry>
                <entry key="456" value-ref="student4"></entry>
                <entry key="123" value-ref="student5"></entry>
            </map>
        </property>
    </bean>
    <bean id="student1" class="pojo.Student">
        <property name="id" value="123"></property>
        <property name="name" value="李海乐"></property>
    </bean>
    <bean id="student2" class="pojo.Student">
        <property name="id" value="133"></property>
        <property name="name" value="钟华金"></property>
    </bean>
    <bean id="student3" class="pojo.Student">
        <property name="id" value="143"></property>
        <property name="name" value="易景亮"></property>
    </bean>
    <bean id="student4" class="pojo.Student">
        <property name="id" value="153"></property>
        <property name="name" value="渣男"></property>
    </bean>
    <bean id="student5" class="pojo.Student">
        <property name="id" value="163"></property>
        <property name="name" value="万翀"></property>
    </bean>
</beans>

接下来写测试类

package tool;
        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        import pojo.UserQaq;
public class TestUserQaq {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserQaq userQaq = (UserQaq) context.getBean("userQaq");
        System.out.println(userQaq);
    }
}

最后可以发现,在idea中出现此标志

测试成功!

目录
打赏
0
0
0
0
3684
分享
相关文章
从HashSet到TreeSet,Java集合框架中的Set接口及其实现类以其“不重复性”要求,彻底改变了处理唯一性数据的方式。
从HashSet到TreeSet,Java集合框架中的Set接口及其实现类以其“不重复性”要求,彻底改变了处理唯一性数据的方式。HashSet基于哈希表实现,提供高效的元素操作;TreeSet则通过红黑树实现元素的自然排序,适合需要有序访问的场景。本文通过示例代码详细介绍了两者的特性和应用场景。
89 6
Redis 集合(Set)
10月更文挑战第17天
70 5
Java Set接口凭借其独特的“不重复”特性,在集合框架中占据重要地位
【10月更文挑战第16天】Java Set接口凭借其独特的“不重复”特性,在集合框架中占据重要地位。本文通过快速去重和高效查找两个案例,展示了Set如何简化数据处理流程,提升代码效率。使用HashSet可轻松实现数据去重,而contains方法则提供了快速查找的功能,彰显了Set在处理大量数据时的优势。
69 2
|
16天前
使用 entrySet 遍历 Map 类集合 KV
使用 entrySet 遍历 Map 类集合 KV
|
4月前
|
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
94 18
你对Collection中Set、List、Map理解?
只会“有序无序”?面试官嫌弃的List、Set、Map回答!
小米,一位热衷于技术分享的程序员,通过与朋友小林的对话,详细解析了Java面试中常见的List、Set、Map三者之间的区别,不仅涵盖了它们的基本特性,还深入探讨了各自的实现原理及应用场景,帮助面试者更好地准备相关问题。
90 20
|
5月前
set集合
HashSet(无序,唯一): 基于 HashMap 实现的,底层采用 HashMap 来保存元素。 LinkedHashSet: LinkedHashSet 是 HashSet 的子类,并且其内部是通过 LinkedHashMap 来实现的。 TreeSet(有序,唯一): 红黑树(自平衡的排序二叉树)。
在 Java 中,如何遍历一个 Set 集合?
【10月更文挑战第30天】开发者可以根据具体的需求和代码风格选择合适的遍历方式。增强for循环简洁直观,适用于大多数简单的遍历场景;迭代器则更加灵活,可在遍历过程中进行更多复杂的操作;而Lambda表达式和`forEach`方法则提供了一种更简洁的函数式编程风格的遍历方式。
判断一个元素是否在 Java 中的 Set 集合中
【10月更文挑战第30天】使用`contains()`方法可以方便快捷地判断一个元素是否在Java中的`Set`集合中,但对于自定义对象,需要注意重写`equals()`方法以确保正确的判断结果,同时根据具体的性能需求选择合适的`Set`实现类。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等